Matteo
Matteo

Reputation: 105

Laravel 5.5 Update Post not working

edit.blade.php :

{!! Form::model($post, array('method'=>'PATCH','url'=>'/posts/{post}'.$post->id))  !!}

{{ method_field('PATCH') }}

{{csrf_field()}}

<label for="title">Titolo</label>

<input type="text" class="form-control" id="title" name="title" value="{{$post->title}}">

<label for="body">Corpo</label>

<textarea id="body" name="body" class="form-control" value="{{$post->body}}">

      </textarea>

routes/web.php:

Route::get('/', 'PostsController@index')->name('home');
Route::get('/posts/create', 'PostsController@create');
Route::post('/posts', 'PostsController@store');
Route::get('/posts/{post}', 'PostsController@show');
Route::get('/posts/tags/{tag}', 'TagsController@index');
Route::post('/posts/{post}/comments','CommentsController@store');
Route::get('/posts/{id}/edit', 'PostsController@edit');
Route::get('/edit/{post}', 'PostsController@update');
Route::patch('/post/{post}', 'PostsController@update');
Route::get('/register', 'RegistrationController@create');
Route::post('/register', 'RegistrationController@store');
Route::get('/login', 'SessionsController@create');
Route::post('/login', 'SessionsController@store');
Route::get('/logout', 'SessionsController@destroy');

postController:

public function edit( Post $post )
{
    return view('posts.edit', compact('post'));
}


public function update(Request $request, Post $post)
{
    Post::where('id', $post)->update($request->all());

    return redirect('/home');
}

Upvotes: 2

Views: 2342

Answers (3)

Matteo
Matteo

Reputation: 105

new route:

Route::patch('/posts/{id}/edit/{post}', 'UpdateController@update');

{!! Form::model($post, array('method'=>'POST','url'=>'/posts/{id}/edit'. $post->id)) !!}

I think /posts/{id}/edit not working

Upvotes: 0

Sapnesh Naik
Sapnesh Naik

Reputation: 11656

Change:

Post::where('id', $post)->update($request->all());

To:

Post::where('id', $post->id)->update($request->all());

Upvotes: 0

Alexey Mezenin
Alexey Mezenin

Reputation: 163968

You're using model binding here, so change it to:

public function update(Request $request, Post $post){
    $post->update($request->all());
    return redirect('/home');
}

Also, change URL to:

'url' => '/posts/' . $post->id

Also, remove these fields, because {!! Form::model() !!} automatically insert those for you:

{{ method_field('PATCH') }}
{{ csrf_field() }}

Upvotes: 1

Related Questions