How to pass more id and name through laravel () lists

I need to pass more than "name" and "id" to select a field. my model has id, name and price.

So I pass it as:

$parts = Part::all()->lists('name','id);

      

I would like to have options like:

<option value='id' data-price='price'>name</option>

      

My guess is to try to pass an array as the first parameter in the lists () method, but then I don't know if there is a way to use the form helper.

 $parts = Part::all()->lists('["name"=>name, "price"=>price]','id');

      

Any suggestions?

+3


source to share


2 answers


lists()

not to create a selection, it just creates an array from the collection. You have to pass the complete model to the view and then create the selection manually:



<select name="part">
    @foreach($parts as $part)
        <option value="{{ $part->id }}" data-price="{{ $part->price }}">
            {{ $part->name }}
        </option>
    @endforeach
</select>

      

0


source


Try to do something like

$parts = Part::all()->get(array('id', 'name', 'price'))->toArray();

      



which should give you only the columns you want in the associative array :)

+2


source







All Articles