For making an Eloquent
query you need to setup relationship and to create a many-to-many
relationship you need to build the relationship like this in both models:
The Business
model:
class Business extends Eloquent {
//...
public function categories()
{
return $this->belongsToMany('Caregory');
}
}
The Category
model:
class Category extends Eloquent {
//...
public function businesses()
{
return $this->belongsToMany('Business');
}
}
The Eloquent
query (You already have a pivot
table):
$businesses = Business::with(array('categories' => function($q) use ($query3, $query4) {
$q->where('categories.name', 'LIKE', '%'. $query3 .'%')
->where('categories.name', 'LIKE', '%'. $query4 .'%');
}))->where('businesses.location', 'like', '%'. $query1 .'%')
->where('businesses.location', 'like', '%'. $query2 .'%')
->get();
To check the result just use dd($businesses);
and examine the collection so you'll get the idea about how you can loop them in your view
. Basically, $businesses
will contain a collection and each $business
model in the collection will contain another collection of $categories
, so loop could be something like this:
@foreach($businesses as $business)
{{ $business->propertyname }}
@foreach($business->categories as $category)
{{ $category->propertyname }}
@endforeach
@endforeach