I have catalog with nested categories and items. Now I have maximum 4 level of depth. Routing for subcategories works well. But when I try go to item page it works only for the items in the max depth category.
It gives me an error showing that route is trying to processed by getCategory function.
at HandleExceptions->handleError('8', 'Trying to get property of non-object', 'D:\OpenServer\domains\myapp.dev\app\Http\Controllers\CatalogController.php', '33', array('cat2' => 'cat2sef', 'cat3' => 'cat3sef', 'cat4' => 'testitem-sef, 'category' => null)) in CatalogController.php line 33
routes.php
Route::get('maincat', 'CatalogController@getIndex');
Route::get('maincat/{cat2}/{cat3?}/{cat4?}', 'CatalogController@getCategory');
Route::get('maincat/{cat2}/{cat3?}/{cat4?}/{item}', 'CatalogController@getItem');
CatalogController.php
class CatalogController extends Controller {
public function __construct()
{
}
public function getIndex()
{
$category = Category::where('sef', '=', 'maincat')->first();
$categories = $category->descendants()
->where('parent_id', $category->id)
->orderBy('id', 'asc')
->paginate(12);
return view('catalog.index')->withCategory($category)
->withCategories($categories);
}
public function getCategory($cat2, $cat3 = null, $cat4 = null)
{
if (!is_null($cat4)) {
$category = Category::where('sef', $cat4)->first();
} elseif (is_null($cat4) && !is_null($cat3)) {
$category = Category::where('sef', $cat3)->first();
} else {
$category = Category::where('sef', $cat2)->first();
}
$categories = Category::where('parent_id', $category->id)->get();
$previous = $category->getPrevSibling();
$next = $category->getNextSibling();
$items = Item::where('category_id', $category->id)
->orWhere('cat2_id', $category->id)->paginate(20);
return view('catalog.cat')->withCategory($category)
->withCategories($categories)
->withPrevious($previous)
->withNext($next)
->withItems($items);
}
public function getItem($cat2, $cat3 = null, $cat4 = null, $item)
{
if (!is_null($cat4)) {
$category = Category::where('sef', $cat4)->first();
} elseif (is_null($cat4) && !is_null($cat3)) {
$category = Category::where('sef', $cat3)->first();
} else {
$category = Category::where('sef', $cat2)->first();
}
$item = Item::where('sef', $item)->first();
$previous = Item::where('id', '<', $item->id)->orderBy('id', 'desc')->first();
$next = Item::where('id', '>', $item->id)->orderBy('id', 'asc')->first();
return view('catalog.item')->withCategory($category)
->withItem($item)
->withPrevious($previous)
->withNext($next);
}
}
What exactly isn't working? I'm guessing you don't have a category with the correct 'sef'.
If this is your route: 'maincat/cat2/cat3/item', then it'll assume $cat4 is item so do you have a category where 'sef' is equal to 'item'?
yeah laravel wouldnt know how to differentitate category/item you give in url e.g
url.com/category1/item or url.com/category1/category2....
to laravel they look the same.. so you probably need to change the way you are serving either by adding another hardcoded stuff so like:
url.com/category/men/item/clothes << item is hardcoded.. which you can then use routing
but to be honest when you are showing ITEMS you shouldnt have something like url.com/category1/category2/categroy3/item..
just do url.com/items/itemid
and then on the items you can have a breadcrumb showing you how the item is located on yoru site..
Sign in to participate in this thread!
The Laravel portal for problem solving, knowledge sharing and community building.
The community