0

How can I write below SQL query in Laravel

SELECT
    notifications.*,
    if(notifications.branchID=0, 'All', (
        select
            group_concat(name)
        from
            branches
        where
            find_in_set(id,notifications.branchID)
    )) as brcName
FROM
    notifications
WHERE
    id = 2
4
  • First write it correctly in SQL RAW query so we can "translate" it to elloquent Buery Builder or something like that... and format your code. Commented May 28, 2021 at 10:36
  • @hicham-o-sfh this thing will return perfect result on mysql Commented May 28, 2021 at 10:44
  • so mention your MySQL version, and post all your code formatted correctly please. Commented May 28, 2021 at 10:50
  • 1
    got solution dude, cheers! ✔🥂 solution is DB::select( DB::raw("SELECT notifications.*, if(notifications.branchID=0,'All',(select group_concat(name) FROM branches where find_in_set(id,notifications.branchID))) as brcName FROM notifications WHERE id = :id"), array( 'id' => $id, )); Commented May 28, 2021 at 10:59

2 Answers 2

2

Query:

SELECT
    notifications.*,
    if(notifications.branchID=0, 'All', (
        select
            group_concat(name)
        from
            branches
        where
            find_in_set(id,notifications.branchID)
    )) as brcName
FROM
    notifications
WHERE
    id = 2

Query builder:

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);

Eloquent model:

class Notification extends Model
{
    public function getBrcNameAttribute()
    {
        if ($this->branchID === 0) {
            return 'All';
        }

        return $this->branches()->select('group_concat(name) AS brcName')->first()->brcName;
    }

    public function branches()
    {
        return $this->hasMany(\App\Models\Branch::class);
    }
}
Sign up to request clarification or add additional context in comments.

4 Comments

throwing error Facade\Ignition\Exceptions\ViewException Undefined property: Illuminate\Database\MySqlConnection::$brcName
What happens if you dd($this->branches()->select('group_concat(name) AS brcName')->first())?
found solution bruh, thank alot for answer!
this one also worked man ! let's make 2 green tick on that post. :D
0
$id = some_id;

$query = DB::select(
    DB::raw("SELECT notifications.*, if(notifications.branchID=0,'All',(select group_concat(name) FROM branches where find_in_set(id,notifications.branchID))) as brcName FROM notifications WHERE id = :id"),
    array('id' => $id)
);

2 Comments

It works, but it can hardly be called "Laravel solution" :D
CIAO, let it be buddy it's urgent buddy so i have to

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.