-1

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 Answers3

0

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
Dharman
  • 30,962
  • 25
  • 85
  • 135
Nazem Mahmud Piash
  • 1,053
  • 1
  • 13
  • 34
0

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>
John Lobo
  • 14,355
  • 2
  • 10
  • 20
0

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.

Yasin Patel
  • 5,624
  • 8
  • 31
  • 53