首页 > 解决方案 > Laravel doesnthave() 生成一个查询,不会转义 \

问题描述

我有一个users表,它与businesses表有关系,我想要的是搜索还没有业务但他们的角色是business(尚未创建业务资料的用户)的用户。\以下是我的查询,但是在查询 where 子句中转义时出现了一个奇怪的问题。

$user = User::client()->latest()->search()->doesntHave('business')->select(DB::raw("CONCAT('first_name', ' ', 'last_name') AS name"),'id')->get();

以下是我的用户模型功能。

public function scopeClients($query)
{
    return $query->role('Business');
}

public function scopeSearch($query)
{
    foreach (request()->all() as $key => $value) {
        if ($key == 'full_name') {
            $query->whereRaw("UPPER(CONCAT(`first_name`, ' ', `last_name`)) LIKE ?", ['%' . strtoupper($value) . '%']);
        }
    }

    return $query;
}

但它生成的查询是这样的:

select CONCAT('first_name', ' ', 'last_name') AS name, `id` 
from `users` where exists (select * from `roles` inner join `model_has_roles` on `roles`.`id` = `model_has_roles`.`role_id` 
where `users`.`id` = `model_has_roles`.`model_id` and `model_has_roles`.`model_type` = 'App\User' and (`roles`.`id` = 2)) and not exists (
select * from `businesses` where `users`.`id` = `businesses`.`owner_id` and `businesses`.`deleted_at` is null
) and UPPER(CONCAT(`first_name`, ' ', `last_name`)) LIKE '%AHMAD%' order by `created_at` desc

在上面的查询中,唯一的问题是\App\User字符串中转义

标签: mysqleloquentlaravel-5.8mysql-escape-string

解决方案


推荐阅读