使用 Laravel 集体形式更新数据
Update data with Laravel Collective forms
我有一个带有 Laravel 集体的编辑表单,但是当单击该按钮时,数据不会更新。以下是我的代码。
表格:
{!! Form::model($post, ['route' => ['/post/update/', $post->id]]) !!}
{{ method_field('PATCH') }}
<div class="form-group">
<div class="row">
<div class="col-lg-6">
{!! Form::label('title', 'Title') !!}
{!! Form::text('title', null, ['class' => 'form-control']) !!}
</div>
<div class="col-lg-6">
{!! Form::label('category_id', 'Category') !!}
{!! Form::select('category_id', $categories, null, ['class' => 'form-control']) !!}
</div>
</div>
</div>
<div class="form-group">
{!! Form::label('content', 'Content') !!}
{!! Form::textarea('content', null, ['class' => 'form-control', 'rows' => 10]) !!}
</div>
<hr/>
<div class="form-group">
{!! Form::submit('Update', ['class' => 'btn btn-success pull-right']) !!}
</div>
{!! Form::close() !!}
控制器:
public function edit($id)
{
return \View::make('admin/post/edit')->with([
'post' => \DB::table('posts')->find($id),
'categories' => \App\Category::lists('category', 'id')
]);
}
public function update(Request $request, Post $post)
{
$post->update($request->all());
return \Redirect::to('/admin/posts');
}
路线:
Route::get('/admin/post/edit/{id}', 'Admin\PostController@edit');
Route::patch('/post/update/', [
'as' => '/post/update/',
'uses' => 'Admin\PostController@update'
]);
它与 Laracast 有点不同,让我感到困惑。框架对我来说是新的,缺少做某事的代码令人困惑。
我解决了。 Mass Assignment。说明如果使用 update
或 create
该怎么办
所以,update
方法是:
public function update(Request $request, Post $post)
{
$post->title = $request->title;
$post->category_id = $request->category_id;
$post->content = $request->content;
$post->save();
return \Redirect::to('/admin/posts');
}
我有一个带有 Laravel 集体的编辑表单,但是当单击该按钮时,数据不会更新。以下是我的代码。
表格:
{!! Form::model($post, ['route' => ['/post/update/', $post->id]]) !!}
{{ method_field('PATCH') }}
<div class="form-group">
<div class="row">
<div class="col-lg-6">
{!! Form::label('title', 'Title') !!}
{!! Form::text('title', null, ['class' => 'form-control']) !!}
</div>
<div class="col-lg-6">
{!! Form::label('category_id', 'Category') !!}
{!! Form::select('category_id', $categories, null, ['class' => 'form-control']) !!}
</div>
</div>
</div>
<div class="form-group">
{!! Form::label('content', 'Content') !!}
{!! Form::textarea('content', null, ['class' => 'form-control', 'rows' => 10]) !!}
</div>
<hr/>
<div class="form-group">
{!! Form::submit('Update', ['class' => 'btn btn-success pull-right']) !!}
</div>
{!! Form::close() !!}
控制器:
public function edit($id)
{
return \View::make('admin/post/edit')->with([
'post' => \DB::table('posts')->find($id),
'categories' => \App\Category::lists('category', 'id')
]);
}
public function update(Request $request, Post $post)
{
$post->update($request->all());
return \Redirect::to('/admin/posts');
}
路线:
Route::get('/admin/post/edit/{id}', 'Admin\PostController@edit');
Route::patch('/post/update/', [
'as' => '/post/update/',
'uses' => 'Admin\PostController@update'
]);
它与 Laracast 有点不同,让我感到困惑。框架对我来说是新的,缺少做某事的代码令人困惑。
我解决了。 Mass Assignment。说明如果使用 update
或 create
所以,update
方法是:
public function update(Request $request, Post $post)
{
$post->title = $request->title;
$post->category_id = $request->category_id;
$post->content = $request->content;
$post->save();
return \Redirect::to('/admin/posts');
}