samedi 31 décembre 2016

BelongTo and pivot relationship query

I have these 3 tables

Listings

id

city


Categories

id


Items

id

category_id


Listings Table has ManytoMany Relationship with Categories Table, so there is another pivot table listing_category containing listing_id and category_id.

public function categories()
{
    return $this->belongsToMany('App\Menu', 'listing_category', 'listing_id', 'category_id')->withTimestamps();
}

Items table has BelongTo Relationship with Categories Table.

public function category()
{
    return $this->belongsTo('App\Category', 'category_id', 'id');
}

My goal is to build search with 2 inputs, users can search based on item name and city (which is in listings table)

example, I want to search name of item containing keyword'item A' in city containing keyword 'Los Angeles'.

How is the query to achieve this in laravel way?



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire