partho
partho

Reputation: 1084

Laravel DB::raw() not executing query but manual execute works

I am running this query

$query = "update my_cloud set full_dir_url = replace(full_dir_url, '$findWhat', '$replaceWith') where full_dir_url like '$findWhat/%' or where full_dir_url = '$findWhat' ";
DB::raw($query);
echo $query;

Output of echo $query is

update my_cloud set full_dir_url = replace(full_dir_url, '/1st Year/Slide', '/1st Year/Slides') where full_dir_url like '/1st Year/Slide/%' or where full_dir_url = '/1st Year/Slide'

When I run this query in phpmyadmin sql editor, data is updated as expected.

But that DB::raw($query) doesn't update any data. No error log either.

Upvotes: 1

Views: 2189

Answers (2)

partho
partho

Reputation: 1084

Answering my own question:

Issue 1: DB::raw shouldn't work as Renée said. DB::statement or DB::update both worked for me.

Issue 2 or where full_dir_url='$findWhat' -this clause is wrong. I removed where from or where and that worked.

Upvotes: 1

goto-bus-stop
goto-bus-stop

Reputation: 11824

DB::raw doesn't run a query, it wraps the string in an object so that Laravel knows that it should not escape the contents.

Instead you can use the DB::update function:

$query = "update my_cloud set full_dir_url = replace(full_dir_url, '$findWhat', '$replaceWith') where full_dir_url like '$findWhat/%' or where full_dir_url = '$findWhat' ";
DB::update(query);

Upvotes: 3

Related Questions