Simple Publish/Unpublish buttons on blade view should update database table column on submit with 0
/1
.
There are two buttons:
{!! Form::open(array('route' => array('book.publish', $book->id), 'method' => 'post')) !!}
<button class='btn btn-default'>Publish</button>
{!! Form::close() !!}
{!! Form::open(array('route' => array('book.publish', $book->id), 'method' => 'post')) !!}
<button class='btn btn-danger'>Unpublish</button>
{!! Form::close() !!}
And two routes
Route::post('book/publish/{publish}', 'BookController@publish')->name('book.publish');
Route::post('book/unpublish/{unpublish}', 'BookController@unpublish')->name('book.publish');
And the controller
public function publish($id){
$publish = Books::find($id);
$publish->published = 1;
$publish->save();
return redirect()->route('book');
}
public function unpublish($id){
$publish = Books::find($id);
$publish->published = 0;
$publish->save();
return redirect()->route('book');
}
Can someone help me to write this into one function. The goal is to show only one button depending of what status has the book. If in database is published then button should show unpublished and opposite way.
from Newest questions tagged laravel-5 - Stack Overflow http://ift.tt/2jrwRgt
via IFTTT
Aucun commentaire:
Enregistrer un commentaire