I'm trying to use the PLUCK method to display two columns of a table when selecting them
$driverItems = Driver::pluck('driverName','id')->toArray();
I'm trying to use the PLUCK method to display two columns of a table when selecting them
$driverItems = Driver::pluck('driverName','id')->toArray();
pluck()
will always create a single collection array (for multiple column pluck, it will be single object). Such as, this query output might look like:
{
"driverName1": 1,
"driverName2": 2,
}
You may try like following:
$driverItems = Driver::select('driverName','id')->get();
This will output like:
[
{
"id": 1,
"driverName": "driverName1"
},
{
"id": 4,
"driverName": "driverName2"
},
]
and for dropdown you can show it like:
<select >
@foreach($driveItems as $item)
<option id="" value="{{$item.id}}"> {{ $item.driverName }} </option>
@endforeach
Using pluck you can populate select
$driverItems = Driver::pluck('driverName','id')->prepend("---Select---","");
and in blade
<select name="driver_id">
@foreach($driverItems as $key=>$value)
<option value="{{$key}}">{{$value}}</option>
@endforeach
</select>
You can do it by pluck()
and use Form Facade in the view file.
use Illuminate\Support\Arr;
$data = Driver::get();
$data= Arr::pluck($data, 'driverName','id');
In view file,to create dropdown use below code:
{{ Form::select('element_name',$data,['class'=>'form-control','id'=>'driver_list','placeholder'=>'-- Select --']) }}
It will create dropdown with place holder.