Reputation: 765
I'm using Laravel 5 and I have a button "Enable" that once pressed sets a flag in the DB to 1. This all works fine with my code below:
Blade view
{!! Form::open(['url' => '/cms/user/' . $user->id . '/flag', 'method' => 'PUT']) !!}
{!! Form::hidden('flagged', !$user->flagged) !!}
{!! Form::submit('Flag', ['class' => 'btn btn-success'])!!}
{!! Form::close() !!}
Controller
/**
* Update the specified resource in storage.
*
* @Middleware({"auth"})
*
* @param int $id
* @Put ("cms/user/{user}/flag",as="cms.users.update.flag")
* @return Response
*/
public function updateflag($id)
{
$user = User::find($id);
//dd(Input::get('flagged'));
$user->flagged = Input::get('flagged');
$user->save();
Session::flash('message', 'Successfully flagged idea!');
return redirect()->route('cms.users.index');
}
Now what I'm trying to work out is changing the button once the user flags it. I.e.: The first time the button should say "Enable" (DB value is 0). Then I press the button and it updates the flag in the DB to 1 (which works fine).
But now, after the button is pressed, how do I change the button text to "Disable" so I can then disable the user on pressing the button. Kind of like an on/off switch that I can enable/disable the user but updating the button text and function based on what the flagged value is in the DB.
Upvotes: 0
Views: 2521
Reputation: 2014
Pass a boolean variable to the view (apparently you already have a variable that contains this information):
return redirect()->route('cms.users.index')->with(['flag' => $flag]);
Then in view make an if/else:
@if($flag)
{!! Form::submit('Enable', ['class' => 'btn btn-success', 'disabled' => 'disabled']) !!}
@else
{!! Form::submit('Enable', ['class' => 'btn btn-success']) !!}
@endif
Upvotes: 2