mardi 1 mars 2016

Update data with Laravel Collective forms

I have an edit form with Laravel Collective but when clicking the button, the data do not update. Below are my codes.

Form:

{!! 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() !!}

Controller:

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

Routes:

Route::get('/admin/post/edit/{id}', 'Admin\PostController@edit');
Route::patch('/post/update/', [
    'as' => '/post/update/',
    'uses' => 'Admin\PostController@update'
]);

It's a bit different from the Laracast, and it's confusing me. Framework is new to me and the lack of code to do something is confusing.



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire