繁体   English   中英

laravel eloquent ->whereHas - 写你自己的存在(子查询)

[英]laravel eloquent ->whereHas - write your own exists( subquery )

Laravel Eloquent ->whereHas()使用exists()子查询 - https://dev.mysql.com/doc/refman/8.0/en/exists-and-not-exists-subqueries.html - 为了返回您的结果.

我想编写我自己的子查询,但我不知道如何告诉 Eloquent -> 它在哪里。

如果我做:

$query->where( DB::raw(' exists( subquery ) ')

Laravel 将子查询写为:

where exists( subquery ) is null

所以我只是想知道什么$query->method()可用于将 exists() 子查询添加到“where”语句中。 子查询与 laravel 生成的类型相同,但写出:

... and exists ( select * from `tbl` inner join `assets` on `custom_assets`.`id` = `tbl`.`asset_id` where `assets`.`deleted_at` is null and `users`.`id` = `assets`.`client_id` and `field_id` = ? and (`value` = ? and `assets`.`deleted_at` is null )

使用whereRaw()

$query->whereRaw('exists( subquery )')

在这里阅读 WhereHas 描述

您可以在那里找到此代码示例。 您还可以在 whereHas 中为您的自定义查询添加一个闭包。

// Retrieve all posts with at least one comment containing words like foo%
$posts = App\Post::whereHas('comments', function ($query) {
    $query->where('content', 'like', 'foo%');
})->get();

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM