How to do update query on laravel fluent using db raw

0 votes

How do you properly do an update using DB:raw?

I want to update the favorite_contents.type with the value of contents.type, but it doesn't do anything, the static setting of 1 to favorite_contents.expired is working.

This is my code which still doesn't update the type even when the DB::raw was used:

$table = 'favorite_contents';
$contents = DB::table($table)
            ->join('contents', function($join) use($table){
                $join->on("$table.content_id", '=', 'contents.id');
            })
            ->whereIn("$table.content_id",$ids)
            ->update(array(
                    "$table.expired" => 1
            ));

DB::raw("UPDATE favorite_contents, contents SET favorite_contents.type = contents.type where favorite_contents.content_id = contents.id");

This is the first code that doesn't update before I resorted to the above code that doesn't work as well:

$table = 'favorite_contents';
$contents = DB::table($table)
        ->join('contents', function($join) use($table){
            $join->on("$table.content_id", '=', 'contents.id');
        })
        ->whereIn("$table.content_id",$ids)
        ->update(array(
                "$table.expired" => 1,
                "$table.type" => "contents.type"
        )); 

This is working when done on an sql editor:

Sep 24, 2020 in Laravel by kartik
• 37,530 points
3,898 views

1 answer to this question.

0 votes

Hello @kartik,

Code row updates like this:

...->update( array(
    'column' => DB::raw( 'column * 2' )
) );

Hope it helps!!
Thank you!!

answered Sep 24, 2020 by Niroj
• 82,820 points

Related Questions In Laravel

0 votes
1 answer

How to alias a table in Laravel Eloquent queries using Query Builder?

Hello @kartik, You can use less code, writing ...READ MORE

answered Nov 11, 2020 in Laravel by Niroj
• 82,820 points
4,702 views
0 votes
1 answer

How to make a new page with routing using Laravel?

Hey @kartik, First you have to go to ...READ MORE

answered Mar 18, 2020 in Laravel by Niroj
• 82,820 points
2,962 views
+1 vote
1 answer

How to make anchor tag with routing using Laravel?

Hey @kartik, First you have to go to ...READ MORE

answered Mar 18, 2020 in Laravel by Niroj
• 82,820 points
9,850 views
0 votes
1 answer

How to enable query log in Laravel?

Hello, You can use simple function that is ...READ MORE

answered Mar 23, 2020 in Laravel by Niroj
• 82,820 points
571 views
0 votes
1 answer

jQuery AJAX fires error callback on window unload - how do I filter out unload and only catch real errors?

Hello, In the error callback or $.ajax you have three ...READ MORE

answered Apr 27, 2020 in Java-Script by Niroj
• 82,820 points
1,826 views
0 votes
1 answer

How do I pass command line arguments to a Node.js program?

Hello @kartik, If your script is called myScript.js ...READ MORE

answered May 5, 2020 in Java-Script by Niroj
• 82,820 points
1,539 views
0 votes
1 answer

Error:Issue when trying to use IN() in wordpress database

Hello @kartik, Try this code : // Create an ...READ MORE

answered May 8, 2020 in PHP by Niroj
• 82,820 points
225 views
+2 votes
1 answer

How do I debug Node.js applications?

Hello @kartik, Use node-inspector  from any browser supporting WebSocket. Breakpoints, ...READ MORE

answered Jul 8, 2020 in Node-js by Niroj
• 82,820 points
239 views
0 votes
1 answer

How to get the id when you're validating in the model Validation unique on update using laravel?

Hello @kartik, in Laravel's inbuilt auth system, the ...READ MORE

answered Sep 11, 2020 in Laravel by Niroj
• 82,820 points
2,819 views
0 votes
1 answer

How to update a pivot table using Eloquent in laravel 5?

Hello @kartik, The code below solved my problem: $messages ...READ MORE

answered Sep 24, 2020 in Laravel by Niroj
• 82,820 points
2,034 views