Mysql 如何在laravel中编写给定的SQL查询

Mysql 如何在laravel中编写给定的SQL查询,mysql,laravel,eloquent,Mysql,Laravel,Eloquent,如何在LARAVEL中编写下面的SQL查询 选择 通知。*, ifnotifications.branchID=0,“全部”, 选择 组名称 从…起 分支机构 哪里 在_setid,notifications.branchID中查找_ 作为brcName 从…起 通知 哪里 id=2 查询: 选择 通知。*, ifnotifications.branchID=0,“全部”, 选择 组名称 从…起 分支机构 哪里 在_setid,notifications.branchID中查找_ 作为brcNa

如何在LARAVEL中编写下面的SQL查询

选择 通知。*, ifnotifications.branchID=0,“全部”, 选择 组名称 从…起 分支机构 哪里 在_setid,notifications.branchID中查找_ 作为brcName 从…起 通知 哪里 id=2 查询:

选择 通知。*, ifnotifications.branchID=0,“全部”, 选择 组名称 从…起 分支机构 哪里 在_setid,notifications.branchID中查找_ 作为brcName 从…起 通知 哪里 id=2 查询生成器:

DB::table('notifications')
->select('notifications.*')
->addSelect(DB::raw("if(notifications.branchID=0, 'All', (
        select
            group_concat(name)
        from
            branches
        where
            find_in_set(id,notifications.branchID)
    )) as brcName"))
->where('id', 2);
雄辩的模型:

类通知扩展了模型 { 公共函数getBrcNameAttribute { 如果$this->branchID==0{ 返回“全部”; } return$this->branchs->select'group\u concatname AS brcName'->first->brcName; } 公共职能部门 { 返回$this->hasMany\App\Models\Branch::class; } } 查询:

选择 通知。*, ifnotifications.branchID=0,“全部”, 选择 组名称 从…起 分支机构 哪里 在_setid,notifications.branchID中查找_ 作为brcName 从…起 通知 哪里 id=2 查询生成器:

DB::table('notifications')
->select('notifications.*')
->addSelect(DB::raw("if(notifications.branchID=0, 'All', (
        select
            group_concat(name)
        from
            branches
        where
            find_in_set(id,notifications.branchID)
    )) as brcName"))
->where('id', 2);
雄辩的模型:

类通知扩展了模型 { 公共函数getBrcNameAttribute { 如果$this->branchID==0{ 返回“全部”; } return$this->branchs->select'group\u concatname AS brcName'->first->brcName; } 公共职能部门 { 返回$this->hasMany\App\Models\Branch::class; } } $id=某个id; $query=DB::select DB::rawSELECT notifications.*,ifnotifications.branchID=0,'All',从id=:id的通知中查找的分支中选择组\u concatname,notifications.branchID作为brcName, 数组'id'=>$id ; $id=某个id; $query=DB::select DB::rawSELECT notifications.*,ifnotifications.branchID=0,'All',从id=:id的通知中查找的分支中选择组\u concatname,notifications.branchID作为brcName, 数组'id'=>$id ;

首先在SQL原始查询中正确地编写它,这样我们就可以将它转换为Elloquint Buery Builder或类似的东西。。。并格式化你的代码。@hicham-o-sfh这个东西会在MySQL上返回完美的结果,所以请提及你的MySQL版本,并将你所有的代码正确格式化。找到解决方案了,伙计,干杯!✔首先在SQL原始查询中正确地编写它,这样我们就可以将它转换为Elloquint Buery Builder或类似的东西。。。并格式化你的代码。@hicham-o-sfh这个东西会在MySQL上返回完美的结果,所以请提及你的MySQL版本,并将你所有的代码正确格式化。找到解决方案了,伙计,干杯!✔抛出错误Facade\Ignition\Exceptions\ViewException Undefined属性:Illumb\Database\MySqlConnection::$brcName如果添加$this->Branchs->选择'group\u concatname AS brcName'->first会发生什么?找到了解决方案bruh,非常感谢您的回答!这个人也工作了!让我们在那根柱子上打两个绿色记号不客气数据错误Facade\Ignition\Exceptions\ViewException Undefined属性:Illumb\Database\MySqlConnection::$brcName如果添加$this->branchs->选择'group\u concatname AS brcName'->first会发生什么?找到了解决方案bruh,非常感谢您的回答!这个人也工作了!让我们在那根柱子上打两个绿色记号不客气DIt是有效的,但它很难被称为Laravel解决方案:DCIAO,让它成为巴迪,这是紧急的巴迪,所以我必须告诉你这是你的代码,不是我的\_ツ_/“这是可行的,但很难称之为Laravel解决方案:DCIAO,让它成为巴迪,这是紧急的巴迪,所以我必须告诉你这是你的代码,不是我的”\_ツ_/¯