php - 使用关系结果的雄辩查询
问题描述
我需要使用关系而不是连接,因为我想在以后使用Images
模型时使用它来操作数据。
因此,例如,我可以执行此操作,而使用左连接无法做到这一点:
$result->images->getMasterImage();
但是,我的主要查询需要检查是否有足够的图像,所以我需要where
根据图像的图像做一个。使用关系引入这些行时可以这样做吗?还是我也必须进行左连接?
$image_categories = [ 1, 2, 3, 4, 5, 6 ];
$main->selectRaw('count(images.id) as image_count');
$main->with([ 'images' => static function ($query) use ($image_categories) {
/** @var \Illuminate\Database\Query\Builder $query */
$query->whereIn('image_category', $image_categories);
}]);
$main->where('image_count', '>', 6);
解决方案
推荐阅读
- tcl - 在 TCL 上的另一个 proc 内部使用 proc
- java - 房间数据库中的数据未显示在真实手机上
- c++ - “const char *”类型的参数与“LPCWSTR”类型的参数不兼容 Visual Studio 2019
- leaflet - Papa Parse - 数组到标记
- flutter - Flutter 中的空 url
- file - wxMaxima:打印多个文件内容的简单/高效方法,以便可以将其复制到一个块中?
- java - Spring Webflux WebSocket Client - 如何处理不同的消息结构?
- c - 在 Ubuntu 中交付 SDL2 项目以进行部署
- r - 使用 read.csv 读取特殊文件
- python - 当我尝试使用 render_template Flask 时,我的网页上没有显示任何内容