Search code examples
laraveldropdown

display fetch data in dropdown menu with "where" value selected in laravel


I am trying to display the currencies in the dropdown menu but I need the data with a value of default=1 to be selected. upon searching, i found a sample and tried to applied it to my controller, here's what I came up,

$currencies = \DB::table('currencies')->where('default', 1)->lists('acronym');

it doesn't work. the error message said

Call to undefined method Illuminate\Database\Query\Builder::lists()

also I read a comment that the list() is already obsolete in laravel.

how can I achieve this?

here's from my create function in controller

public function create()
{
    $currencies = \DB::table('currencies')->where('default', 1)->lists('acronym');
    return view ('orders.create')->with('currencies', $currencies);
}

here's from create blade

{{ Form::select('currency_id', $currencies, Input::old('currency_id'),null, ['class' => 'form-control input-lg','required']) }}

thank you so much in advance!


Solution

  • Try using ->pluck(),

    $currencies = \DB::table('currencies')->pluck('currency_name','id');
    
    // In blade
    {{ Form::select('currency_id', $currencies, null, ['class' => 'form-control input-lg','required']) }}
    

    Read more about pluck here.