3

I am facing some problems with my selectbox, where i will put all available categories into the

In my controller i am using this snip:

 return View::make("stories.add")
        ->with("title","Indsend novelle")
        ->with("categories", Category::all());

In my view i am trying to put all categories into the selectbox with this:

 {{Form::select("category", $categories)}}

I could make this, but that won't work, because Form::select has to be as an array?

@foreach ( $categories as $category )
    {{$category->name}}
@endforeach

What to do?

I have made this and it works, but it looks too ugly and not user-friendly, any suggestions?

  $test = Category::all(); $myArray = array();
    foreach ( $test as $o):
          $myArray[] = $o->name;
    endforeach;

    return View::make("stories.add")
        ->with("title","Indsend novelle")
        ->with("categories", $myArray);

var_dump:

    array(2) {
      [0]=>
      object(Category)#36 (5) {
        ["attributes"]=>
array(4) {
  ["id"]=>
  string(1) "1"
  ["name"]=>
  string(12) "Alderforskel"
  ["created_at"]=>
  string(19) "0000-00-00 00:00:00"
  ["updated_at"]=>
  string(19) "0000-00-00 00:00:00"
}
["original"]=>
array(4) {
  ["id"]=>
  string(1) "1"
  ["name"]=>
  string(12) "Alderforskel"
  ["created_at"]=>
  string(19) "0000-00-00 00:00:00"
  ["updated_at"]=>
  string(19) "0000-00-00 00:00:00"
}
["relationships"]=>
array(0) {
}
["exists"]=>
bool(true)
["includes"]=>
array(0) {
}
}
       [1]=>
   object(Category)#39 (5) {
  ["attributes"]=>
  array(4) {
  ["id"]=>
  string(1) "2"
  ["name"]=>
  string(7) "Bondage"
  ["created_at"]=>
  string(19) "0000-00-00 00:00:00"
  ["updated_at"]=>
  string(19) "0000-00-00 00:00:00"
}
["original"]=>
 array(4) {
  ["id"]=>
  string(1) "2"
  ["name"]=>
  string(7) "Bondage"
  ["created_at"]=>
  string(19) "0000-00-00 00:00:00"
  ["updated_at"]=>
  string(19) "0000-00-00 00:00:00"
}
["relationships"]=>
array(0) {
}
["exists"]=>
bool(true)
["includes"]=>
array(0) {
}
}
}
4
  • Are you using Laravel 3 or Laravel 4? You should only need to tag this with one. Commented Jun 18, 2013 at 7:29
  • In laravel 4, you should be able to use Category::all()->all() to convert the Collection into an array. Commented Jun 18, 2013 at 7:32
  • Call to a member function all() on a non-object Commented Jun 18, 2013 at 7:35
  • Do a var_dump(Category:all());. I suspect it returns an array. Commented Jun 18, 2013 at 7:47

4 Answers 4

9

Use it this way:

$categories = Category::pluck('name', 'id');

return View::make('....', compact('categories'));

And now in the view:

{{ Form::select('selectName', $categories, null); }}

Edit: Found in the docs Query builder # Select Have a look to this

Sign up to request clarification or add additional context in comments.

2 Comments

Hey are you sure that Category::lists('name', 'id'); this is valid? I don't know about lists function on query builder or model.
lists function is deprecated & replaced by pluck() as of Laravel > 5.2 Now you can do Category::pluck('name', 'id');
4

What you need to do is give Form::select() an array of category names and their ids. If you iterate over the categories, you can aggregate these and then pass them to Form::select().

$categories = Categories::all();
$selectCategories = array();

foreach($categories as $category) {
    $selectedCategories[$category->id] = $category->name;
}

return View::make("stories.add")
        ->with("title","Indsend novelle")
        ->with("categories", $selectCategories);

1 Comment

Says that "pluck" doesnt exists.
2

What you need to do is instead of using the with() function with the view put inside the controller function.

$categories = Category::all();

After this you need properly reconstruct the array:

$category = array();
foreach($categories as $cat)
{
  $category[]['id'] = $cat->attributes['id'];
  $category[]['name'] = $cat->attributes['name'];
}

now in the View::make()

return View::make("stories.add",array('title'=> "Indsend novelle","categories", $category));

I hope this can be of some help.

2 Comments

Doesn't work. If more than 1 category it shows ID instead of the name, where the name should be in the <option ..> ie. <select name="category"><option value="id">2</option><option value="name">Bondage</option></select>
sorry kim my mistake I made a change in the foreach loop please make that change in your script and I hope that should do the trick.
0

I like the approach suggested by Israel Ortuño

I would only add a small modification, so that the select starts with an empty "Choose from the List" option by default.

$categories = array(0=>'Choose from the list') + Category::lists('name', 'id');

return View::make('....', compact('categories'));


Now the dropdown looks like this:

<option value="0">Choose from the list</option>
<option value="{whatever-the-category-id}">Category 1</option>
...

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.