Laravel changing function/text of a button depending on value - laravel

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.

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

Related

Laravel Collective drop-down to mysql in Laravel 5.8

I get the error:
SQLSTATE[01000]: Warning: 1265 Data truncated for column 'level'..
My guess is that Form::select should be used somehow differently, how?
// in my migration:
$table->enum('level', ['easy', 'hard']);
// in my controller Store function:
$tablee = new Tablee; // this is view file called Tablee.php
$tablee->level = $request->input('level');
$tablee->save();
// and part of my code in create.blade.php
<div class="form-group">
{{Form::label('level', 'Please choose level')}}
{{Form::select('level', ['easy' => 'easy', 'hard' => 'hard'], ['class' => 'form-control'])}}
</div>
The third argument of Form::select is the selected element.
public function select($name, $list = [], $selected = null, $options = [])
So you should change yours to be
{{ Form::select('level', ['easy' => 'easy', 'hard' => 'hard'], null, ['class' => 'form-control']) }}

Pass user id through select box list laravel

I want to pass the selected user id to controller from select box.How can I achieve it?
{!! route('approve', ['id' => $user->id]) !!}
In select box,
{{ Form::select('Actions', [
'approve' => 'Approve',
'decline' => 'Decline']
) }}
My resulto to be like this,
First of all you'll need an input for selecting users. For example I got a list of users with their username and id here :
$users= \App\Users::pluck('username','id');
after sending it to the view, you can use an input for selecting user :
{!! Form::select('user_id', $users,'0', ['class' => 'form-control']) !!}
That's it. Now you can select the user and get data as $_POST['user_id'] or $_GET['user_id'] in the action. gl.

How to populate combo box dynamically with Laravel (From Database)

sorry guys if this is nothing to you all but I seem not to get it I have a drop down and would love to populate it from the database I am using laravel 5.2 and looked at almost all questions asked on this topic and most of it is on laravel 4. I tried populating my combo from database but kept on getting the same error "ErrorException in 0fe8e1e2379436fb1f6f8c15a481341a7cff00e0.php line 22:
Undefined variable: callsign " here is how I have done it form the controller l inserted this code:
$callsign = \DB::table('drivers')->lists('Code');
return view('spotCheck.create')->with('drivers', $callsign);
from my view here is the code:
{!! Form::label('Code','Select a Driver') !!}
{!! Form::select('Code', $callsign, null, ['class' => 'form-control']) !!}
the question is what I am i not doing write and whats up with the error how does my controller know where to populate drop down
for the controller I had to do this
$drive = DB::table('drivers')->lists('Code');
return view('mypage',compact('drive'));
and on the view did this
{!! Form::label ('Call Sign:',null, ['class'=>"control-label"])!!}
{!! Form::select('call_sign', $drive, null, ['class' => 'form-control' , 'id' => 'sel']) !!}

Laravel having trouble sending decimals

I have a blade template form that takes a price
{!!Form::open(array('method'=>'POST','route'=>array('order.store'))) !!}
<div class="form-group">
{!! Form::label('price', 'Price:', ['class' => 'control-label']) !!}
{!! Form::number('price', null, ['class' => 'form-control']) !!}
</div>
{!! Form::submit('Submit Order', ['class' => 'btn btn-primary']) !!}
{!! Form::close() !!}
The controller takes price and send it to email:
class OrderController extends Controller
{
public function store()
{
$data=Input::all();
//Validation rules
$rules = array (
'price' => 'required|regex:/[\d]{2}.[\d]{2}/',
);
$validator = Validator::make ($data, $rules);
//If everything is correct than run passes.
if ($validator -> passes()){
//Send email using Laravel send function
Mail::send('emails.order_received', $data, function($msg) use ($data)
{
//email 'From' field: Get users email add and name
$msg->from($data['email'] , $data['owner']);
//email 'To' field: change this to emails that you want to be notified.
$msg->to('on#dx.com', 'Rn')->subject('New Order');
});
return Redirect::route('order.index')->with('flash_notice', 'Thank you');
}
else
{
//return contact form with errors
return Redirect::route('order.index')->withErrors($validator)->with('flash_error', 'This is not how one shops');
}
}
}
The table is passed to a email template.
<?php
//get the first name
$item = Input::get('item');
$manufacturer = Input::get ('manufacturer');
$price = Input::get('price');
$quantity = Input::get ('quantity');
$product_code = Input::get ('product_code');
$owner = Input::get ('owner');
$email = Input::get("email");
$created_at = Input::get("date");
?>
When ever I try to add a price i.e. (3.65) the blade form continues to return an error message for an integer. My migration takes price as a decimal(2,2) I can't understand why my form is throwing errors. Any help would be much appreciated.
P.S. Other than the regex rule I have tried float and decimal. Now if I try entering 2 instead of 02.00 with the regex rule it throws an error based on the regex. However if I try adhering to the regex rule it wants an integer (error says between X and Y).
Thanks
M
First, you'll definitely want to use the numeric rule for your validation.
Secondly, you are using the HTML5 input field number which by default only accepts integers, not floats.
If you want it to also accept floats and therefore not trigger the browser built in validation change your code to the following:
{!! Form::number('price', null, ['class' => 'form-control', 'step' => 'any']) !!}
Or, of course, you could just use an text input and do the inline validation yourself.

selected value tag of Form::model doesn't bind property from Model

I'm learning Laravel and have an issue when trying to bind a property of the Model to selected values of the select tag. I tried to leave the 3rd parameter null because I believe Form Model Binding will automatically take care of it but it doesn't work. Here is what I already tried:
{{$article->tag_list}} // show [1,3]
//it doesn't work
{!! Form::select('tag_list[]', $tags, null , ['class' => 'form-control', 'multiple'] ) !!}
-------------
//it doesn't work as well
{!! Form::select('tag_list[]', $tags, $article->tag_list , ['class' => 'form-control', 'multiple'] ) !!}
-----------
//it works
{!! Form::select('tag_list[]', $tags, [1,3] , ['class' => 'form-control', 'multiple'] ) !!}
In the model I have the getTagListAttribute() which works fine.
public function getTagListAttribute(){
return $this->tags->lists('id');
}
With Text Input, the form works fine. Btw, I'm using 5.2.1 version. What am I missing here ?
I found the missing piece. the select function expects an array but getTagListAttribute() return an Collection object.
public function getTagListAttribute(){
return $this->tags->lists('id')->all();
}
or I can do this
public function getTagListAttribute(){
return $this->tags->lists('id')->toArray();
}

Resources