Php Laravel如何进行此关系查询
我的当前数据库设置如下: 我有一个用户模型/投票模型/组模型/投票子项模型。 用户和投票之间存在多对多关系,用户和子项之间也存在多对多关系 我想获得一个ll,将在时间戳处创建的投票添加到某个对象,该对象属于一组组ID,其中某个特定用户在user\u vote\u casts表中没有条目 我怎样才能在拉威尔做到这一点 在mysql中,我认为这是正确的:Php Laravel如何进行此关系查询,php,mysql,laravel,join,eloquent,Php,Mysql,Laravel,Join,Eloquent,我的当前数据库设置如下: 我有一个用户模型/投票模型/组模型/投票子项模型。 用户和投票之间存在多对多关系,用户和子项之间也存在多对多关系 我想获得一个ll,将在时间戳处创建的投票添加到某个对象,该对象属于一组组ID,其中某个特定用户在user\u vote\u casts表中没有条目 我怎样才能在拉威尔做到这一点 在mysql中,我认为这是正确的: SELECT * from vote left join user_vote_casts on vote.id = user_vote_cast
SELECT * from vote left join user_vote_casts on vote.id = user_vote_casts.vote_id where user_vote_casts.user_id IS NULL AND vote.id IN (12,3142,512,51,12,1) ;
根据您的SQL查询进行LAVEL查询:
$users = DB::table('vote')
->leftJoin('user_vote_casts', 'vote.id', '=', 'user_vote_casts.vote_id')
->select('vote.*')
->where('user_vote_casts.user_id','')
->whereIN('vote.id',[12,3142,512,51,12,1])
->get();
根据您的SQL查询进行LAVEL查询:
$users = DB::table('vote')
->leftJoin('user_vote_casts', 'vote.id', '=', 'user_vote_casts.vote_id')
->select('vote.*')
->where('user_vote_casts.user_id','')
->whereIN('vote.id',[12,3142,512,51,12,1])
->get();
我最终做的是:
$vote = Vote::leftJoin('user_vote_casts', function ($join) {
$join->on('vote.id', '=', 'user_vote_casts.vote_id');
})->whereNull('user_vote_casts.user_id')
->orderBy('vote.updated_at', 'DESC')
->take(30)->get();
我最终做的是:
$vote = Vote::leftJoin('user_vote_casts', function ($join) {
$join->on('vote.id', '=', 'user_vote_casts.vote_id');
})->whereNull('user_vote_casts.user_id')
->orderBy('vote.updated_at', 'DESC')
->take(30)->get();
到目前为止,你有什么代码?请看编辑我强烈建议你阅读这篇文章:它解释了你需要知道的关于拉拉维尔关系的一切。到目前为止,你有什么代码?请看编辑我强烈建议你阅读这篇文章:它解释了你需要知道的关于拉维尔关系的一切。