首页 \ 问答 \ 具有回退到闭包的此查询的等效Eloquent版本是什么?(What would be the equivalent Eloquent version of this query with fallback to closure?)

具有回退到闭包的此查询的等效Eloquent版本是什么?(What would be the equivalent Eloquent version of this query with fallback to closure?)

这是一个示例查询。 这是针对MySQL的。

select 
    sub_query.corporations_owned_id, 
    corporations_owned.corp_name, 
    sub_query.counts 
from corporations_owned 
INNER JOIN 
(
    select 
        corporations_owned_id, 
        COUNT(user_id) as counts 
    from corporations_owned_user 
    group by corporations_owned_id
) 
as sub_query 
on sub_query.corporations_owned_id = corporations_owned.corp_id 
where corp_status = 1

我想要做的是:

DB::table('corporations_owned')
-> select (DB::raw("sub_query.corporations_owned_id,  corporations_owned.corp_name, sub_query.counts"))
-> join ($query, callback_function () {
//what should be the equivalent closure here?
})
// rest of the query; 

我对回调函数的期望是:

-> join ($query, function($query) {
   $query -> //Another query in eloquent format only, but not in raw.
        "select 
            corporations_owned_id, 
            COUNT(user_id) as counts 
         from corporations_owned_user 
         group by corporations_owned_id"
})

我想在eloquent中添加一个对join函数的回调,正如我在上面的例子中所示。 我需要做出哪些改变?


This is a example query. This is for MySQL.

select 
    sub_query.corporations_owned_id, 
    corporations_owned.corp_name, 
    sub_query.counts 
from corporations_owned 
INNER JOIN 
(
    select 
        corporations_owned_id, 
        COUNT(user_id) as counts 
    from corporations_owned_user 
    group by corporations_owned_id
) 
as sub_query 
on sub_query.corporations_owned_id = corporations_owned.corp_id 
where corp_status = 1

What I would like is to do is:

DB::table('corporations_owned')
-> select (DB::raw("sub_query.corporations_owned_id,  corporations_owned.corp_name, sub_query.counts"))
-> join ($query, callback_function () {
//what should be the equivalent closure here?
})
// rest of the query; 

My expectancy for the callback function is:

-> join ($query, function($query) {
   $query -> //Another query in eloquent format only, but not in raw.
        "select 
            corporations_owned_id, 
            COUNT(user_id) as counts 
         from corporations_owned_user 
         group by corporations_owned_id"
})

I would like to add a callback to the join function in eloquent as I have shown in the above example. What changes to I need to make?


原文:https://stackoverflow.com/questions/39337093
更新时间:2022-04-20 12:04

最满意答案

由于没有为图像设置源(src),所以它显示白色边框。


As there is no source(src) set for image so its displaying white border.

相关问答

更多