How can i show in a SELECT two columns of a table using PLUCK

188 views Asked by At

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 need to display the driver name and the Licence

3

There are 3 answers

0
Nazem Mahmud Piash On BEST ANSWER

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
0
John Lobo On

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>
0
Yasin Patel On

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.