Laravel查询所有 [英] Laravel Query where All
问题描述
我想知道如何使用Laravel制作Where All子句我正在尝试检查用户看到的情节是否是该系列的所有情节.
I wondered how to make a Where All clause with Laravel I'm trying to check if the episodes that a user saw are all the episodes of the series.
我正在使用WhereIn子句,但如果看到该系列的一集,我将返回结果.
I'm using the WhereIn clause but i returns the results if i saw one episode of the serie.
$ alleps获得意甲的所有片段$ seriessaw获取用户看到的所有剧集
$alleps get all the episodes of the serie $seriessaw get all the episodes a user saw
谢谢您的回答!
$alleps = DB::table('episodes')
->select('episodes.id as ep_id')
->join('seasonsepisodes', 'episodes.id', '=', 'seasonsepisodes.episode_id')
->join('seriesseasons', 'seasonsepisodes.season_id', '=', 'seriesseasons.season_id')
->where('seriesseasons.series_id', '=', $id);
$seriesSaw = DB::table('usersepisodes')
->select('usersepisodes.episode_id as ep_id')
->where('usersepisodes.user_id', '=', Auth::user()->id)
->whereIn('usersepisodes.episode_id', $alleps)
->get();
推荐答案
我认为您需要设置一个 having
子句,该子句将强制记录仅在与记录数量相同的情况下才返回是情节的数量.
I think you would need to set a having
clause which would force records to only return if there's the same amount of records as there are amount of episodes.
$seriesSaw = DB::table('usersepisodes')
->select('usersepisodes.episode_id as ep_id')
->where('usersepisodes.user_id', '=', Auth::user()->id)
->whereIn('usersepisodes.episode_id', $alleps)
->having(\DB::raw('count(*)'), count($alleps))
->get();
但是您应该注意,如果 userepisodes
表中存在重复项,这可能会中断.
You should note however that this would likely break if there's any possibility of having duplicates in the userepisodes
table.
这篇关于Laravel查询所有的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!