samedi 23 février 2019

Laravel eloquent query using two relations

I have next db structure - product can be in many categories, product can be in many markets. Models \App\Product, \App\Market and \App\Category are created with many to many relations - belongsToMany().

enter image description here

In route.web I get category to display products

Route::get('/catalog/{current_category?}', 'CatalogController@index')->name('catalog.index');

Current market I can get from session (user select market when open website)

$market = $request->session()->get('market'); // or Session::get('market');
// $market->id
// $market->slug

In my MarketController@index I want to get all products for category from route and for current market from session. But how can I do it? I can get category products and market products. But how can I get category and market products at the same time?

public function index(Request $request, Category $current_category = null)
{
    if ($current_category) {

        $market_id = $request->session()->get('market')->id;

        $products = $current_category->products;
        // ...
    }
}



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire