I am unsure why you have an array for a relationship. It is normally a method. Are you using a package for that to work?
Oct 18, 2019
8
Level 1
Using relationship column in a query
I have my category list coming from this relationship
public $belongsToMany = [
'catbuy' => [
'Corymillz\Adverts\Models\Cat',
'table' => 'corymillz_adverts_buyrequest_cat',
'order' => 'cat_title',
'key' => 'buy_id',
'otherKey' => 'cat_id'
]
];
I created a simple filter using ajax to filter by up to 4 categories and location using the code below
function onFilterBuyRequest()
{
$category = post('maincat', []);
$location = post('location');
$buy = Buy::get();
$query = Buy::with('catbuy')->whereIn('cat_title', $category)->where('location', $location)->orderBy('created_at','desc')->get();
$requests = $query;
return [
...
];
}
i keep getting the error
catbuy column does not exist
how to i write the query to get the data stored in catbuy to make up the filter query?
this works when storing data
$buy->catbuy = Input::get('...')
Level 1
well resolved it looping through all categories and adding whereHas()
$query = Buy::with('catbuy');
foreach ($category as $cat) {
$query->whereHas('catbuy', function($q) use ($cat){
$q->where('id', $cat);
});
}
Please or to participate in this conversation.