phplaravelformsviewpluck

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


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


Solution

  • 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