Support the ongoing development of Laravel.io →
Eloquent Views Packages
Last updated 4 months ago.
0

Try changing this part of your controller

public function getCategory(Category $category){

To

public function getCategory($category){
Last updated 4 months ago.
0

Try this?

public function getCategory(Category $category = null) {
    // ...
}
Last updated 4 months ago.
0

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){
  // ...
}
Last updated 4 months ago.
0

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){
 // ...
}
Last updated 4 months ago.
0

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)}}">
Last updated 4 months ago.
0

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.

Last updated 4 months ago.
0

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 :/

Last updated 4 months ago.
0

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'=>'[email protected]'));

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

Last updated 4 months ago.
0

Sign in to participate in this thread!

Cloudways

Your banner here too?

Moderators

We'd like to thank these amazing companies for supporting us

Your logo here?

The Laravel portal for problem solving, knowledge sharing and community building.

© 2022 Laravel.io - All rights reserved.