0

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

4

1 回答 1

0

我解决了。批量分配。解释如果使用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');
}
于 2016-03-01T16:41:09.933 回答