How to generate an array for a dropdown (with ['key' => 'value']) suitable for a dropdown, from a Laravel collection of Eloquent objects?

Laravel: How to generate an array for a dropdown (with ['key' => 'value']) suitable for a dropdown, from a Laravel collection of Eloquent objects?

The answer used to be list(), but now the answer is pluck()

  1. $posts = Post::all();
  2.  
  3.         echo Form::select("post_id", $posts->pluck("title","id"));
  4.  
  5.         // $posts->pluck("title","id") will produce something like this:
  6.         // [
  7.         //   1 => "post title 1",
  8.         //   5 => "Another post",
  9.         // ...
  10.         // ]

The first param is the actual value you want to 'pluck', and the second value is how you want it keyed. For most cases the second param will be 'id'.

You can also use pluck with just 1 param like this:

  1. $collection = collect([
  2.     ['product_id' => 'prod-100', 'name' => 'Desk'],
  3.     ['product_id' => 'prod-200', 'name' => 'Chair'],
  4. ]);
  5.  
  6. $plucked = $collection->pluck('name');
  7.  
  8. $plucked->all();
  9.  
  10. // ['Desk', 'Chair']

But if more than one item with the same key exists, the last matching one will be used. For example:

  1. $collection = collect([
  2.     ['brand' => 'Tesla',  'color' => 'red'],
  3.     ['brand' => 'Pagani', 'color' => 'white'],
  4.     ['brand' => 'Tesla',  'color' => 'black'],
  5.     ['brand' => 'Pagani', 'color' => 'orange'],
  6. ]);
  7.  
  8. $plucked = $collection->pluck('color', 'brand');
  9.  
  10. $plucked->all();
  11.  
  12. // ['Tesla' => 'black', 'Pagani' => 'orange']

For more details see here.

webdevetc profile pic
webdevetc

I am a 29 year old backend web developer from London, mostly focusing on PHP and Laravel lately. This (webdevetc.com) is my blog where I write about some web development topics (PHP, Laravel, Javascript, and some server stuff). contact me here.



More...


Comments and discussion about How to generate an array for a dropdown (with ['key' => 'value']) suitable for a dropdown, from a Laravel collection of Eloquent objects?

Found this interesting? Maybe you want to read some more in this series?

Or see other topics in the Laravel language

Or see other languages/frameworks:
PHP Laravel Composer Apache CentOS and Linux Stuff WordPress General Webdev and Programming Stuff JavaScript
Or see random questions

Remove trailing slash (redirect to remove trailing slash) in .htaccess

How to Extract Query String (from a URL) Into an Associative Array in PHP

Is multiple inheritance supported in PHP?

How to block IP addresses in Apache by using regular expressions (regex) in .htaccess

How to output (echo/print) content in PHP

How to increment a value in the database

What is the spaceship operator (<=>) in PHP?

How to install jpegtran for Linux CentOS

How to use subdomains as parameters in your routes (or in your web.php file)

What is the difference between empty(), isset(), != null, is_null() in PHP