Back

Using an array of Objects in a form drop down


Adam Allen posted 5 months ago

I am getting an array of products from my database and need to know how to list them in a drop down in a form within my blade file.

Currently, the array I'm using looks like this:

$products = [
{
id:1,
name:product_1,
qty:5,
price:3.99
},
{
id:2,
name:product_2,
qty:5,
price:5.99
}
{
id:3,
name:product_3,
qty:20,
price:10.99
}
{
id:4,
name:product_4,
qty:10,
price:3.99
}
]

and my form tags lok like this

{!! Form::select('product', $products); !!}

How can i get it to only display the name from the object and also when i submit to using the ID in stead of the drop down index.

If you need any more info i am happy to supply it :)

Thanks in advance.

Kamil Bielak replied 5 months ago

$products - must be collection then

{!! Form::select('product', $products->pluck('name','id')) !!}
alicetaylor replied 5 months ago

This is also a very good post which I really enjoyed reading. It is not everyday that I have the possibility to see something like this.

Mutilate A Doll 2


Sign in to participate in this thread!



We'd like to thank these amazing companies for supporting us