how to to make eloquent scope with whereHas like sql query below
table Property(id, title, slug, category_id, location_id,image)
table Category(id, name, slug)
table City ( id, name, slug)
The simple sql query that i need
Select * from property
join category on property.category_id=category.id
join city on property.location_id = city.id
where category.name = $query and city.name=$query
I want to make the eloquent scope in the Property Model
This is easy with relationships.
Based on your query, let's say this is your
Propertymodel:Now I can write my Eloquent query like this:
Please note that
Builderis imported fromIlluminate\Database\Eloquent\Builder. You can also addwith('category', 'city')to the query above in order to eager load those relationships.