Controller in function
public function newStatus($id, Post $stat)
{
post::where('id', $id)->update(['status' => $stat, 'id' => $id]);
return redirect('/postview')->with('M_status', 'Your Status has been updated!');
}
web.php route
Route::get('/statusUpdate/{id}/{status}', [AdminsController::class, 'newStatus']);
blade
@if ($post->status == 'publish')
<a href="/statusUpdate/{{$post->id}}/{{$post->status = "publish"}}" class="btn btn-info btn-xs navbar-btn">Draft</a>
@else
<a href="/statusUpdate/{{$post->id}}/{{$post->status = "draft"}}" class="btn btn-primary btn-xs navbar-btn">Publish</a>
@endif
My Answer:
In your controller function, you are updating the 'status' field in the post table with the entire Post object $stat instead of just the status value. You should update only the 'status' field with the status value like this:
php
public function newStatus($id, $status)
{
post::where('id', $id)->update(['status' => $status]);
return redirect('/postview')->with('M_status', 'Your Status has been updated!');
}
Also, in your blade file, you are assigning the status value instead of comparing it. Change the lines in the blade file to compare the status value like this:
html
@if ($post->status == 'publish')
Draft
@else
Publish
@endif
With these changes, your status update functionality should work correctly.
Rate this post
5 of 5 based on 8306 votesComments