Laravel / Eloquent在哪里与所有项目相关? [英] Laravel/Eloquent whereHas with all items in relation?

查看:92
本文介绍了Laravel / Eloquent在哪里与所有项目相关?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

模拟歌曲和作家

我想查询歌曲,这样我只会得到 ALL 其作者具有特定字段的歌曲没错,我的解决方案是什么?

And I want to query songs such that I only get songs where ALL its writers have a certain field that is true, what would my solution be?

Laravel函数具有的功能将获取在该字段中具有至少一位作家的所有歌曲,例如

The Laravel whereHas function will get all songs that have at least one writer with that field, like so.

Song::whereHas('writers', function($query){
    $query->where('writerField', '=', true);
});

但是确保 ALL 作家与一首歌曲的'writerField'设置为true吗?

But what is the pure eloquent way to make sure ALL writer related to a Song has that 'writerField' set to true?

推荐答案

反转约束,并将所需的计数传递给 wherehas 方法:

Invert your constraint, and pass the desired count to the whereHas method:

Song::whereHas('writers', function() {
    $query->where('writerField', false);
}, '=', 0);

这篇关于Laravel / Eloquent在哪里与所有项目相关?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆