jeudi 8 mars 2018

$query->where in many to many relationship

In my controller I setup two filters one for category (has-one relationship) and one for shop (many to many relationship)

public function products($filter = NULL, $key = NULL)
{
    $query = Product::query();
    $query = $query->where('status', '=', 1);
    if ($filter === 'category') {
        $cat = Category::where('slug', '=', $key)->select('id')->firstOrFail();
        $query = $query->where('category_id', '=', $cat->id);
    }
    if ($filtro === 'shop') {
        $shop = Shop::where('slug', '=', $key)->select('id')->firstOrFail();
        // HOW TO ADD HERE A WHERE IN MANY TO MANY RELATIONSHIP*
    }
    $products = $query->whereNotNull('startdate')->with('shops', 'categories')->orderby('startdate')->get();

*HOW TO ADD to the query A WHERE IN MANY TO MANY RELATIONSHIP only if "shop" filter are set?



from Newest questions tagged laravel-5 - Stack Overflow http://ift.tt/2FBIsas
via IFTTT

Aucun commentaire:

Enregistrer un commentaire