user10960234
user10960234

Reputation:

Illegal offset type laravel 5.7

I want to update 4 fields with a same value, so I have done this:

UpdateModel::where('model_id', $id)->update([['is_1_ans', 'is_2_ans', 'is_3_ans', 'is_4_ans'] => 1]);

but I laravel giving me this error;

Illegal offset type

I know I'm doing wrong but I want to know how to update multiple fields with same value if needed?

Upvotes: 3

Views: 365

Answers (1)

Lionel Chan
Lionel Chan

Reputation: 8059

I don't think this is out of the box, but you can do this to fill the values to the keys:

// The keys you want to update
$keys = ['is_1_ans', 'is_2_ans', 'is_3_ans', 'is_4_ans'];

// We use array_fill_keys to fill "1" to all the keys above
$updates = array_fill_keys($keys, 1);

// This will result in
// $updates = ['is_1_ans' => 1, 'is_2_ans' => 1, 'is_3_ans' => 1, 'is_4_ans' => 1];

// Then you do your usual updates ;)
UpdateModel::where('model_id', $id)->update($updates);

See array_fill_keys documentation.

Upvotes: 4

Related Questions