我正在从Lacast学习Laravel,并试图创建一个CRUD应用程序。我已经正确实现了index,show,create
和store
,但是当我使用edit
形式时尝试提交引发BadMethodCallException
的数据。
这是我的路线
Route::get('/projects','ProjectsController@index');
Route::get('/projects/{id}','ProjectsController@show')->where('id','[0-9]+');
Route::get('/create','ProjectsController@create');
Route::post('/projects','ProjectsController@store');
Route::get('/projects/{id}/edit','ProjectsController@edit')->where('id','[0-9]+');
Route::put('/projects/{id}','ProjectsController@update')->where('id','[0-9]+');
Route::delete('/projects/{id}','ProjectsController@destroy');
这是edit
形式:
@extends('template');
@section('content')
<h2>Create new project</h2>
<p>/projects/{{ $project->id }}</p>
<form method="POST" action="/projects/{{ $project->id }}">
{{ method_field('PUT') }}
{{ csrf_field() }}
<div>
<input value="{{ $project->title }}" type="text" name="title" id="title" placeholder="Project title">
</div>
<div>
<textarea name="description" placeholder="Enter the project description">{{ $project->description }}</textarea>
</div>
<div>
<button type="submit">Update project</button>
</div>
</form>
@endsection
控制器代码:
public function edit($id){
$project= Project::find($id);
return view('projects.edit',compact('project'));
}
public function update($id){
$project= Project::find($id);
$project->title=request('title');
$project->description('description');
$project->save();
return redirect('/projects');
}
edit form
的显示与来自数据库的数据一样,提交后得到以下错误页面:
以讲师的示例为例,代码运行良好,并且在我的表单和控制器中使用PUT
之前,我都像讲师一样使用PATCH
,但结果始终相同。
以下是讲师视频链接:Faking PATCH and DELETE Requests
答案 0 :(得分:1)
错误非常简单。
public function update($id){
$project= Project::find($id);
$project->title=request('title');
$project->description('description'); // You don't have a method description()
$project->save();
return redirect('/projects');
}
这可能就是您要做的:
public function update($id)
{
$project= Project::find($id);
$project->title = request('title');
$project->description = request('description');
$project->save();
return redirect('/projects');
}