Try changing this part of your controller
public function getCategory(Category $category){
To
public function getCategory($category){
Try this?
public function getCategory(Category $category = null) {
// ...
}
No, get rid of the word Category in front of $category.
You aren't passing in an object, the way your route is set up you should be passing a string, categoryid, through the URL.
Just this
public function getCategory($category){
// ...
}
I am not sure whether he is using Laravel 5.0, if that's the case, it is possible to do method DI in Laravel 5.0 See. https://laracasts.com/series/whats-new-in-laravel-5/episodes/2
But yea, if remove the type hint solve the problem, that should be okay as well. :D
IanSirkit said:
No, get rid of the word Category in front of $category.
You aren't passing in an object, the way your route is set up you should be passing a string, category_title, through the URL.
Just this
public function getCategory($category){ // ... }
Oh one more thing you can do. Since you are using a named route take advantage of it.
Instead of HTML::link use HTML::route, that way later if you change the path your links will update automatically.
So this:
{{HTML::link('/store/category/'.$cat, $cat->name)}}
Becomes this:
<a href="{{HTML::route('storeCategory', $cat->name)}}">
IanSirkit said:
Oh one more thing you can do. Since you are using a named route take advantage of it.
Instead of HTML::link use HTML::route, that way later if you change the path your links will update automatically.
So this:
{{HTML::link('/store/category/'.$cat, $cat->name)}}
Becomes this:
<a href="{{HTML::route('storeCategory', $cat->name)}}">
The route
helper is enough and shorter. I would go with that instead.
Thank you for your answers, but nothing seems to work. It still asks for an object. I've also added the Model function 'scopeCategorized' which I use inside my controller. I feel like a complete idiot, I just can't figure this out :/
Waiting for more suggestions please...
Edit:
Managed to figure this out :)
Here is the solution if anyone else got stuck with baum package.
View:
{ HTML::linkRoute('storeCategory',$cat->name, array($cat->id)) }}
Controller:
public function getCategory($id){
$category = Category::find($id);
return View::make('store.category')
->with('products', Product::categorized($category)->get())
->with('category', Category::find($id));
}
Route:
Route::get('store/category/{id}', array('as' =>'storeCategory','uses'=>'StoreController@getCategory'));
Also change scopCategorize function to:
public function scopeCategorized($query, Category $category=null) {
if ( is_null($category) ) return $query->with('category');
$categoryIds = $category->getDescendantsAndSelf()->lists('id');
return $query->with('category')
->join('products_categories', 'products_categories.product_id', '=', 'products.id')
->whereIn('products_categories.category_id', $categoryIds);
}
Everything works now, thank you for you for your answers
Sign in to participate in this thread!
The Laravel portal for problem solving, knowledge sharing and community building.
The community