I'm trying to filter my results from two tables: trips and activities that are linked by table trips_activities. Activity: Trip:
Trip_Activity:
Their relations are defined as follow: In activity model:
public function trips()
{
return $this->belongsToMany('App\Trip',"trips_activities","activityid","tripsid");
}
In trip model:
public function activities()
{
return $this->belongsToMany('App\Activity',"trips_activities","tripsid","activityid");
}
When i try to run following query:
$trips = $trips->whereHas('activities', function($r) use($activities) {
$r->whereIN('activityid', $activities)->get();
});
It gives following output:
SQLSTATE[42S22]: Column not found: 1054 Unknown column 'trips.id' in 'where clause' (SQL: select * from `activities` inner join `trips_activities` on `activities`.`id` = `trips_activities`.`activityid` where `trips_activities`.`tripsid` = `trips`.`id` and `activityid` in (2))
Your Query
select * from `activities`
inner join `trips_activities` on `activities`.`id` = `trips_activities`.`activityid`
where `trips_activities`.`tripsid` = `trips`.`id` and `activityid` in (2)
Here trips.id means look for trips table and id column. But I do not see any trips table. I believe it should be activities.tripsid
You use the get()
function in wrong place. You need add ->get()
after all your query:
$trips = $trips->whereHas('activities', function($r) use($activities) {
$r->whereIN('activityid', $activities);
})->get();
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.