如何搜索具有完全相同关系值的集合

发布于 2025-02-04 13:11:25 字数 292 浏览 2 评论 0原文

我有这3张表服装,服装,appareloutfit (下面的表结构示例)。我想检查使用AppareloutFit Pivot表中的apparel_ids数组是否存在服装。 Apparel_ids的数组应与服装的相关服装_ID完全相同,不再是。我应该怎么做?

-- Apparel --
id | name


-- Outfit --
id | is_favorite


-- ApparelOutfit -- (pivot table)
apparel_id | outfit_id

I have these 3 tables Apparel, Outfit, ApparelOutfit (sample of table structure below). I want to check if an outfit exists using an array of apparel_ids in the ApparelOutfit pivot table. The array of apparel_ids should be exactly the same as the related apparel_ids of the outfit, no more no less. How should I do that?

-- Apparel --
id | name


-- Outfit --
id | is_favorite


-- ApparelOutfit -- (pivot table)
apparel_id | outfit_id

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

他夏了夏天 2025-02-11 13:11:25

根据您的输入通过评论

int服装模型我有这个。 public Function Apparels(){return $ this-> allastomany(apparel :: class) - > withTimestamps() - > as('appaperel_outfit') - > orderbypivot('apapeel_id''); }

您可以尝试

EG:


OUTFIT1具有[1,2,3]
的apaperel_ids
OutFit2具有[1,2,3,4]
OutFit3具有[2,3,4]

//Say you have the following apparel ids as request data
$apparelIds = [1,2,3];

$outfits = Outfit::query()
    ->whereHas(
        'apparels', 
        fn($query) => $query->whereIn('id', $apparelIds), 
        '=', 
        count($apparelIds)
    )
    /** If you want to get the records */
    ->get();
    /** If you just want how many outfits exist for the given $apparelIds
     * ->count();
     */

上述查询将获取 Offit1 Offit2

对于严格的确切匹配,

例如:


OUTFIT1具有[1,2,3]
的apaperel_ids
OutFit2具有[1,2,3,4]
OutFit3具有[2,3,4]

//Say you have the following apparel ids as request data
$apparelIds = [1,2,3];

$outfits = Outfit::query()
    ->whereHas(
        'apparels', 
        fn($query) => $query->whereIn('id', $apparelIds)
    )
    ->whereDoesntHave(
        'apparels', 
        fn($query) => $query->whereNotIn('id', $apparelIds)
    )
    /** If you want to get the records */
    ->get();
    /** If you just want how many outfits exist for the given $apparelIds
     * ->count();
     */

The above query will fetch only outfit1

Based on your input via comment

int OUtfit model i have this. public function apparels() { return $this->belongsToMany(Apparel::class) ->withTimestamps() ->as('apparel_outfit') ->orderByPivot('apparel_id'); }

You can try

Eg:

say
outfit1 has apparel_ids of [1,2,3]
outfit2 has apparel_ids of [1,2,3,4]
outfit3 has apparel_ids of[2,3,4]

//Say you have the following apparel ids as request data
$apparelIds = [1,2,3];

$outfits = Outfit::query()
    ->whereHas(
        'apparels', 
        fn($query) => $query->whereIn('id', $apparelIds), 
        '=', 
        count($apparelIds)
    )
    /** If you want to get the records */
    ->get();
    /** If you just want how many outfits exist for the given $apparelIds
     * ->count();
     */

The above query will fetch outfit1 and outfit2

For a strictly exact match

Eg:

say
outfit1 has apparel_ids of [1,2,3]
outfit2 has apparel_ids of [1,2,3,4]
outfit3 has apparel_ids of[2,3,4]

//Say you have the following apparel ids as request data
$apparelIds = [1,2,3];

$outfits = Outfit::query()
    ->whereHas(
        'apparels', 
        fn($query) => $query->whereIn('id', $apparelIds)
    )
    ->whereDoesntHave(
        'apparels', 
        fn($query) => $query->whereNotIn('id', $apparelIds)
    )
    /** If you want to get the records */
    ->get();
    /** If you just want how many outfits exist for the given $apparelIds
     * ->count();
     */

The above query will fetch only outfit1

Laravel Docs - Eloquent Relationships - Querying Relationship Existence

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文