VLS
VLS

Reputation: 445

Two function into one

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.

Upvotes: 0

Views: 175

Answers (4)

Paras
Paras

Reputation: 9465

Just change your view to:

@if($book->published === 1)
{!! Form::open(array('route' => array('book.publish', $book->id), 'method' => 'post')) !!}
<button class='btn btn-default'>Publish</button>

@else
{!! Form::open(array('route' => array('book.unpublish', $book->id), 'method' => 'post')) !!}
<button class='btn btn-danger'>Unpublish</button>
@endif
{!! Form::close() !!}

And routes:

Route::post('book/publish/{publish}', 'BookController@publish')->name('book.publish');
Route::post('book/unpublish/{unpublish}', 'BookController@unpublish')->name('book.unpublish');

And the controller:

public function publish($id){

    Books::where('id', $id)->update(['published' => 1]);      
    return redirect()->route('book');

}

public function unpublish($id){

    Books::where('id', $id)->update(['published' => 0]);       
    return redirect()->route('book');
}

Why this answer is better than the other ones posted?

  1. Better Query Performance: This answer invokes just 1 query fired in controller whereas other answers invoke at least 2 queries (one to find the book, one to save)
  2. Better User Experience: I recommend against the togglePublish method in controller. The reason is that if two different users publish or unpublish the book at the same time, while both will believe the desired action is performed but what would really happen is it would be done and then undone!

Upvotes: 1

Alexey Mezenin
Alexey Mezenin

Reputation: 163768

If you want to just change status, you could use ternary operator:

$publish->published = $publish->published == 1 ? 0 : 1;

Or you could try this:

$publish->published = !$publish->published;

Upvotes: 1

Loek
Loek

Reputation: 4135

Controller

public function togglePublish($id)
{
    $publish = Books::find($id);

    if ($publish->published === 1) {
        $publish->published = 0;
    } else {
        $publish->published = 1;
    }

    $publish->save();
    return redirect()->route('book');
}

View

@if($book->published === 1)
    // Show button to unpublish
@else
    // Show button to publish
@endif

Route

Route::post('book/publish/{publish}', 'BookController@togglePublish')->name('book.publish');

Note

In the controller and view:

if ($publish->published === 1)

evaluates exactly the same as

if ($publish->published)

But I think it's clearer to write it explicitly.

Upvotes: 3

fico7489
fico7489

Reputation: 8560

Controller:

public function publish($id, $status){
    $publish = Books::find($id);

    $publish->published = $status;
    $publish->save();        

    return redirect()->route('book');
}

Routes:

Route::post('book/publish/{publish}/{status}', 'BookController@publish')->name('book.publish');

Template:

{!! Form::open(array('route' => array('book.publish', $book->id, 1), 'method' => 'post')) !!}
    <button class='btn btn-default'>Publish</button>
{!! Form::close() !!}

{!! Form::open(array('route' => array('book.publish', $book->id, 0), 'method' => 'post')) !!}
    <button class='btn btn-danger'>Unpublish</button>
{!! Form::close() !!}

Upvotes: 1

Related Questions