Reputation: 1748
Have a problem here :)
$ls = '<select size="12">';
foreach(Category::all() as $k){
$ls .= '<option>'.$k['name'].'</option>';
}
$ls.= '</select>';
return $ls;
This returns all data from DB but I want to retrive only data where parent = 1 so when I change
foreach(Category::all() as $k)
to
foreach(Category::where('parent','=',1) as $k)
it gets nothing to return. Help please, where is the problem here?
Upvotes: 0
Views: 100
Reputation: 146219
You should use something like this:
$categories = Category::where('parent', 1)->get(); // '=' could be omitted
foreach($categories as $category){
//...
}
You have to call the get()
method to get the query result. Also, avoid the doing code in your view
. You can just pass the result to your view using something like this from the controller:
$categories = Category::where('parent', 1)->get();
return View::make('viewname')->with('categories', $categories);
Then loop in the View:
// Blade
@foreach($categories as $category)
{{ ... }}
@endforeach
Remove the @
if you are not using Blade
. But, you can also generate the select
even without a loop, for example:
echo Form::select('categories', $categories, Input::old('categories'));
For Blade
just use:
{{ Form::select('categories', $categories, Input::old('categories')) }}
Check the documentation and read the documentation properly, you need to.
view
file, you are doing it wrong.Upvotes: 1