2 回答

TA贡献1836条经验 获得超4个赞
在Laravel Eloquent中,这就是“查询关系存在”的 工作原理
如果不在其他地方使用,则不需要$ spaceList变量
$query = Interest::whereHas('spaces', function($query) use ($user_id) {
$query->where('user_id', '=', $user_id);
});
请注意,要完成这项工作,您需要在兴趣模块中声明空格一对多关系
应该是这样的,更多详细信息请参见此处的文档
namespace App;
use Illuminate\Database\Eloquent\Model;
class Interest extends Model
{
public function spaces()
{
// space_id is the column name in your space database table
// id the the foreign-key target, generally is the primary-key of space table
return $this->hasMany('App\Space', 'space_id', 'id');
}
}

TA贡献1982条经验 获得超2个赞
您应该使用whereIn
而不是where
$spaceList = Space::where('user_id', $user_id)->pluck('space_id')->toArray(); $query = Interest::whereIn('space_id', $spaceList);
- 2 回答
- 0 关注
- 141 浏览
添加回答
举报