mardi 28 février 2017

Join the same MySQL table using eloquent in laravel

I have a DB table with all of my categories, subcategories and parent_category_id column.

What I am trying to do is to join the same table while creating the subcategory navigation to include category slug in URL like so:

/category_slug/subcategory_slug

here is my composer:

public function compose(View $view)
{
    $view->with('categories', \App\Category::join('categories', 'categories.category_id', '=', 'categories.parent_category_id')
        ->where('category_display_type', '=', 'sidebar')
        ->where('category_visibility', '=', 1)->get());
}

the error that I get is:

SQLSTATE[42000]: Syntax error or access violation: 1066 Not unique table/alias: 'categories' (SQL: select * from `categories` inner join `categories` on `categories`.`category_id` = `categories`.`parent_category_id` where `category_display_type` = sidebar and `category_visibility` = 1)  

Please help

I cannot answer my question just yet, but what I have done so far is the following that is now accessing the category_slug, but ignoring the subcategory_slug

public function compose(View $view)
{
    $params = \DB::table('categories as subs')->join('categories as cats', function($join){
            $join->on('cats.category_id', '=', 'subs.parent_category_id')
            ->where('subs.category_display_type', '=', 'sidebar')
            ->where('subs.category_visibility', '=', 1);
})->get();

    $view->with('categories', $params);

}

Do I have to do a double foreach in my view.blade.php file and if yes then how?



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire