如何将SQL转换为Laravel Eloquent

nwnhqdif  于 2022-12-24  发布在  其他
关注(0)|答案(1)|浏览(131)

有具体的SQL查询需要转换成Laravel雄辩,我已经转换得到错误,大家帮帮我吧?
我的sql

select 
    `d1`.*, `a1`.`user_uuid` as `activity_user` 
from 
    `directors` as `d1` 
left join 
    `activities` as `a1` 
on `a1`.`entity_uuid` = `d1`.`uuid` 
and `a1`.`created_at` = (
    select 
        max(`created_at`) 
    from
        `activities` as `a2`
    where `a2`.`entity_uuid`=`d1`.`uuid`
)
where `a1`.`user_uuid`='4f858f46-2d7e-41c8-a185-c9c4a4b506d1'
group by `a1`.`entity_uuid` 
order by `d1`.`updated_at` 
desc limit 100;

还有我的拉拉威尔

DB::table('directors')
->select('directors.*')
->leftJoin('activities',function($join) {
    $join->on('a1.entity_uuid','=','directors.uuid')
    ->on('a1.created_at','=',function($query) {
        $query->from('activities')
            ->select(DB::raw('max'('activities.created_at'))
            ->where('activities.entity_uuid','=',DB::raw('d1.uuid'));
    });
}
->where('activities.user_uuid','=','4f858f46-2d7e-41c8-a185-c9c4a4b506d1')
->groupBy('activities.entity_uuid')
->orderBy('directors.updated_at','desc')
->get();

我得到错误

TypeError: stripos(): Argument #1 ($haystack) must be of type string, Closure given in file C:\xampp\htdocs\crmMembers\backend\vendor\laravel\framework\src\Illuminate\Database\Grammar.php on line 62
deyfvvtc

deyfvvtc1#

谢谢大家,解决了
只需将double -〉更改为-〉where,即可完美工作

->where('a1.created_at', '=', function($q){$q->from('activities as a2')
                                                ->select(DB::raw('max(`a2`.`created_at`)'))
                                                ->where('a2.entity_uuidm', '=', DB::raw('`d1`.`uuid`'));
                                        });

相关问题