Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Concat in eloquent update

How I can update multiple objects with CONCAT in a single query?

The following code works, but it is not safe:

Table::whereIn('id', $idArray)->where('user_id', $this->userId)
            ->update([
                'title' => DB::raw('CONCAT(title,'.$form->newPart.')')
            ]);

Another method that I have tried is the following, however it does not work:

Table::whereIn('id', $idArray)->where('user_id', $this->userId)
            ->update([
                'title' => DB::raw('CONCAT(title,?)', [$form->newPart])
            ]);
like image 420
Carax85 Avatar asked Nov 14 '25 15:11

Carax85


1 Answers

I think you will just have to call the setBindings method.

    Table::whereIn('id', $idArray)->where('user_id', $this->userId)
        ->update([
            'title' => DB::raw('CONCAT(title, ?)')
        ])
        ->setBindings([$form->newPart]);
like image 120
user1669496 Avatar answered Nov 17 '25 07:11

user1669496



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!