Drop-Down Lists from laravelcollective? - laravel

I use dropdownlist from laravelcollective. I am wondering how I can use (Selected Default) with this if I fetch data from a database. Here follows my source code:
{!! Form::select('port',$ports,null,['class'=>'form-control']) !!}
The selected default that I want is for example (Select Port).

First of all create a list in your controller and use as a first element the "Please select a port" text with empty value:
$ports =['' => 'Please Select a port'] + Port::lists('shortName','id')->toArray();
After you pass it to the view use it like this:
{!! Form::select('port_id',$ports,null,['class' => 'form-control']) !!}

The correct is to use attribute placeholder in dropdown.
{!! Form::select('port', $ports, null, ['placeholder' => 'Select a Port', 'class'=>'form-control']) !!}

Related

Default value for laravel collective select

I have a Laravel collective select as follows:
{!! Form::select('phone', $numbers, $title, ['class' => 'phone-select2 col4', 'id' => 'phone']) !!}
I want to pass default value for dropdown as separate variable from $numbers. So, have passed it as $title but it is didn't work due to it is not in the $numbers. So, how can I pass default value for it?
What you're looking for is a placeholder.
https://laravelcollective.com/docs/6.x/html#drop-down-lists
According to the documentation, you can set the default value to null and provide placeholder attribute as part of the fourth argument:
['class' => 'phone-select2 col4', 'id' => 'phone', 'placeholder' => 'Pick an option']

Laravel Blade - add variable inside javascript confirmation

i want the javascript confirmation to include the variable name $button_label. Which means that it should have a confirmation of "Save record ?" or "Update record ?".
I'm having a hard time doing it.
Any thoughts?
#php $button_label = Route::current()->getName() == 'maintenance.state.edit' ? 'Update record' : 'Save record' ; #endphp
{!! Form::submit($button_label, ['class' => 'btn btn-primary btn-lg', 'onclick' => 'return confirm("Proceed ?")']) !!}
Try this one:
"return confirm('{$button_label} ?')"
Also read this page, if you want to learn why i changed the positions of single and double quotes.

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/Eloquent: Issues with date mutation and form model binding

I have a column with datatype of date which is date mutated. I date mutated it so that Laravel will convert it to a Carbon instance and I can use it easily at other places, where I need to convert it into Carbon instance. I am using Model binding in the edit form. As the field is date mutated, on the edit form it appears as "2015-07-29 00:00:00". I need it to be in this format instead: "2015-07-29".
I can't use accessor, as I need it as a Carbon instance at many other places.
I can't explicitly pass value after converting, as I am using the input inside a form partial and I also use it for create.
My workaround is the following:
I am sending a flag while including the view partial in the edit page and using it a condition to have two different code for create and edit.
#if (isset($edit))
{!! Form::text('eta', $order->eta->format('Y-m-d'), ['class' => 'form-control', 'required']) !!}
#else
{!! Form::text('eta', null, ['class' => 'form-control', 'required']) !!}
#endif
Is there a better way?
I think you can avoid from #if and $edit as,
{!! Form::text('eta', ( $order->eta ? $order->eta->format('Y-m-d') : null ) , ['class' => 'form-control', 'required']) !!}

Resources