user3386779
user3386779

Reputation: 7175

update query to update two fields in laravel

I want to update two fields in laravel 5.3.I have two fields values 'status_id' and 'comment'.my query is working is I update only 'status_id' field

  DB::table('travel_request')
        ->where('id',$id )
        ->update(['status_id' => DB::table('travel_request_status')->where('status', $status)->first()->id]);

I tried to update two fields as follows

DB::table('travel_request')
        ->where('id',$id )
        ->update(['status_id' => DB::table('travel_request_status')->where('status', $status)->first()->id],['comment'=>$comment]);

Upvotes: 1

Views: 4320

Answers (4)

Pradyut Manna
Pradyut Manna

Reputation: 588

Do a small change.

DB::table('travel_request')
->where('id',$id )
->update(['status_id' => DB::table('travel_request_status')->where('status', $status)->first()->id,'comment' => $comment
]);

Upvotes: 1

user4535674
user4535674

Reputation:

you put closure in wrong place

DB::table('travel_request')
        ->where('id',$id )
        ->update(['status_id' => 
                      DB::table('travel_request_status')->where('status', $status)->first()->id
                 ,'comment'=>$comment]);

Upvotes: 1

Mahfuzul Alam
Mahfuzul Alam

Reputation: 3157

All fields should be in one array.

According to Laravel Documentation, The update method expects an array of column and value pairs representing the columns that should be updated.

Try

DB::table('travel_request')
        ->where('id',$id )
        ->update(['status_id' => DB::table('travel_request_status')->where('status', $status)->first()->id, 'comment'=>$comment]);

Upvotes: 1

Alexey Mezenin
Alexey Mezenin

Reputation: 163748

You're passing two arrays, but you should pass one array with two key => value pairs as parameter:

DB::table('travel_request')
    ->where('id', $id)
    ->update([
        'status_id' => DB::table('travel_request_status')->where('status', $status)->first()->id,
        'comment' => $comment
    ]);

Upvotes: 3

Related Questions