2

I'm using LaravelCollective because of the benefits that Form Model Binding offers.

My problem is that I need to fill a select with Categories from the Database so I'm making something like this:

{!! Form::select('size', Category::pluck('name'), null, ['placeholder' => 'Pick a category...']) !!}

The problem with this is that the first option generated has value="0".

I'm looking for a Eloquent way to generate an array ['key' => 'value'] where key is the Id and value is the name. I know I could make a method, get all the categories and generate the Array myself but this isnt what I'm looking for.

Alan
  • 2,559
  • 4
  • 32
  • 53

1 Answers1

7

Try Category::pluck('name', 'id')

Skysplit
  • 1,875
  • 12
  • 16
  • @JarekTkaczyk it worked. Add the answer so I can vote it. Why do I have to write name before id? – Alan Aug 25 '16 at 14:41
  • 1
    @Alan That's how `pluck` works - it get's a collection of values (1st param) with optional keys (2nd param). If 2nd is not provided, then it will have simple array keys, starting with `0` – Jarek Tkaczyk Aug 25 '16 at 14:43
  • @Alan pluck signature is `pluck($column, $key = null)` – Skysplit Aug 25 '16 at 14:43
  • I would recommend putting the `Category::pluck('name', 'id')` in your controller, then referencing it in your view. – Debbie V Sep 27 '18 at 15:42