【问题标题】:Update data with Laravel Collective forms使用 Laravel Collective 表单更新数据
【发布时间】:2016-03-02 05:28:29
【问题描述】:

我有一个带有 Laravel Collective 的编辑表单,但是单击按钮时,数据没有更新。以下是我的代码。

表格:

{!! 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 有点不同,这让我很困惑。框架对我来说是新的,缺少代码来做某事令人困惑。

【问题讨论】:

  • 尝试将您的路线更改为:/post/update/{post} 并检查它是否有效
  • 你有什么错误吗?
  • @Laerte Done,但提交时出现以下错误:MassAssignmentException _token
  • @Dees040 不,完全没有错误。
  • 'as' =&gt; '/post/update/' 应该是 'as' =&gt; 'post.update',这是 Laravel 中的常见约定。

标签: laravel-5 updates laravelcollective


【解决方案1】:

我解决了。 Mass Assignment。解释如果使用updatecreate 该怎么办

所以,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');
}

【讨论】:

    猜你喜欢
    • 2020-09-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-04-08
    • 2015-05-30
    • 1970-01-01
    • 2016-04-10
    相关资源
    最近更新 更多