资源控制器概念
Resource Controller Concept
我找不到问题所在。它显示 404|未找到
update.blade.php
@extends('main')
@section('content')
<h1>Update Post</h1>
<form method="POST" action="{{route('posts.update', $post) }}" >
@method('PUT')
@csrf
<input type="text" name="title"><br><br>
<input type="text" name="body"><br><br>
<button type="submit" class="btn btn-primary">Update</button>
</form>
@endsection
PostController.php(资源控制器)
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use App\posts;
use Sessions;
class PostController extends Controller
{
public function index()
{
$post = posts::all();
return view('post.index', compact('post');
}
public function create(Request $req)
{
return view('posts.create');
}
public function store(Request $request)
{
$post = new posts;
$post->title = $request->input('title');
$post->body = $request->input('body');
$post->save();
return redirect('/');
}
public function show($data)
{
$post = posts::findOrFail($data);
return view('posts.read', compact('post','$post'));
}
public function edit(posts $post)
{
return view('posts.edit', compact('post'));
}
public function update(Request $request, $id)
{
$request->validate([
'title'=>'required',
'body'=>'required'
]);
$post = posts::find($id);
$post->title = $request->get('title');
$post->body = $request->get('body');
$post->save();
return redirect('/');
}
}
路线:
Route::resource('posts', 'PostController');
请告诉我这是什么问题。
我得到的建议之一是更改视图文件的名称,即 update.blade.php 到 edit.blade.php。我不知道它有什么帮助
问题是您正在返回视图:view('post.edit')
。
但是你说这个文件叫update.blade.php
.
因此您必须将文件重命名为 edit.blade.php
或 您需要按如下方式更改您的编辑功能,以便 returns 更新blade 文件:
public function edit(posts $post)
{
return view('posts.update', compact('post'));
}
首先你应该改变 edit.blade.php
而不是 update.blade.php
其次你不应该这样调用模型use App/posts;
这是错误的。在您的 PostController
中必须是 use App\Post;
第三,您应该在控制器中更改 edit()
public function edit($id)
{
$post = Post::find($id);
return view('posts.edit', compact('post'));
}
您应该在表单操作中使用 $post->id
而不是 $post
@extends('main')
@section('content')
<h1>Update Post</h1>
<form method="POST" action="{{route('posts.update', $post->id) }}" >
@method('PUT')
@csrf
<input type="text" name="title"><br><br>
<input type="text" name="body"><br><br>
<button type="submit" class="btn btn-primary">Update</button>
</form>
@endsection
然后检查
public function update(Request $request, $id)
{
dd($id);//check id before update
$request->validate([
'title'=>'required',
'body'=>'required'
]);
$post = posts::find($id);
$post->title = $request->get('title');
$post->body = $request->get('body');
$post->save();
return redirect('/');
}
我找不到问题所在。它显示 404|未找到
update.blade.php
@extends('main')
@section('content')
<h1>Update Post</h1>
<form method="POST" action="{{route('posts.update', $post) }}" >
@method('PUT')
@csrf
<input type="text" name="title"><br><br>
<input type="text" name="body"><br><br>
<button type="submit" class="btn btn-primary">Update</button>
</form>
@endsection
PostController.php(资源控制器)
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use App\posts;
use Sessions;
class PostController extends Controller
{
public function index()
{
$post = posts::all();
return view('post.index', compact('post');
}
public function create(Request $req)
{
return view('posts.create');
}
public function store(Request $request)
{
$post = new posts;
$post->title = $request->input('title');
$post->body = $request->input('body');
$post->save();
return redirect('/');
}
public function show($data)
{
$post = posts::findOrFail($data);
return view('posts.read', compact('post','$post'));
}
public function edit(posts $post)
{
return view('posts.edit', compact('post'));
}
public function update(Request $request, $id)
{
$request->validate([
'title'=>'required',
'body'=>'required'
]);
$post = posts::find($id);
$post->title = $request->get('title');
$post->body = $request->get('body');
$post->save();
return redirect('/');
}
}
路线:
Route::resource('posts', 'PostController');
请告诉我这是什么问题。 我得到的建议之一是更改视图文件的名称,即 update.blade.php 到 edit.blade.php。我不知道它有什么帮助
问题是您正在返回视图:view('post.edit')
。
但是你说这个文件叫update.blade.php
.
因此您必须将文件重命名为 edit.blade.php
或 您需要按如下方式更改您的编辑功能,以便 returns 更新blade 文件:
public function edit(posts $post)
{
return view('posts.update', compact('post'));
}
首先你应该改变 edit.blade.php
而不是 update.blade.php
其次你不应该这样调用模型use App/posts;
这是错误的。在您的 PostController
use App\Post;
第三,您应该在控制器中更改 edit()
public function edit($id)
{
$post = Post::find($id);
return view('posts.edit', compact('post'));
}
您应该在表单操作中使用 $post->id
而不是 $post
@extends('main')
@section('content')
<h1>Update Post</h1>
<form method="POST" action="{{route('posts.update', $post->id) }}" >
@method('PUT')
@csrf
<input type="text" name="title"><br><br>
<input type="text" name="body"><br><br>
<button type="submit" class="btn btn-primary">Update</button>
</form>
@endsection
然后检查
public function update(Request $request, $id)
{
dd($id);//check id before update
$request->validate([
'title'=>'required',
'body'=>'required'
]);
$post = posts::find($id);
$post->title = $request->get('title');
$post->body = $request->get('body');
$post->save();
return redirect('/');
}