我正在努力使用 Laravel 的 Eloquent ORM 进行连接查询
我有两个具有多对多关系的类
class Type extends Eloquent
{
public function activities()
{
return $this->has_many_and_belongs_to('Activity');
}
}
class Activity extends Eloquent
{
public function types()
{
return $this->has_many_and_belongs_to('Type');
}
}
在我的数据库中,我有类型、活动和活动类型表
我正在尝试重新创建以下 SQL 查询
SELECT a . *
FROM activities a
JOIN activities_types at ON a.id = at.activities_id
WHERE at.type_id
IN ( 1, 2, 4 )
GROUP BY a.id
我只是没有掌握 ORM 语法。我试过了
$activities = Activity::types().where_in("id", $types)->get();
但我得到一个错误"Non-static method Activity::types() should not be called statically, assuming $this from incompatible context"
感谢您的任何指点