My DB is:
|category_id|category_slug |category_name |parent_category_id|
|1 |Main Category1|Main Category1|0 |
|2 |Main Category2|Main Category2|0 |
|3 |Sub Category1 |Sub Category1 |1 |
|4 |Sub Category2 |Sub Category2 |1 |
|5 |Sub Category3 |Sub Category3 |2 |
I am trying to execute the following query in MySQL
SELECT *
FROM categories AS subcategory
JOIN categories AS parent ON parent.category_id = subcategory.parent_category_id
WHERE subcategory.category_display_type = 'sidebar'
AND subcategory.category_visibility = 1
like so:
class SidebarnavComposer
{
public function compose(View $view)
{
$params = DB::table('categories as subs')->join('categories as cats', function($join){
$join->on('subs.parent_category_id', '=', 'cats.category_id')
->where('subs.category_display_type', '=', 'sidebar')
->where('subs.category_visibility', '=', 1);
})->get();
$view->with('subcategories', $params);
}
the problem is that I get different set of results.
When I use the first query directly in MySQL I get the following:
category_id
category_id1
category_title
category_title1
category_slug
category_slug1
.......
when I execute the second query in my laravel application I get the following set of results:
category_id
category_title
category_slug
.......
this basically means that I cannot access all necessary columns.
So what I need to be able to do is to build URI for my subcategories in the following way:
category_slug1/category_slug
where category_slug1 is main category slug and category_slug is subdirectory slug. The problem is that I only get the main category slug and cannot get slug for my subcategory in URI.
I am not sure what I am doing wrong as I am new to laravel and at the moment do not really know much, so please help me if you can