C
C
Camaro672018-05-27 12:00:56
Laravel
Camaro67, 2018-05-27 12:00:56

Why doesn't mass assignment protection work everywhere?

Hello! I can’t understand what logic is used to protect against mass assignments in Laravel, the documentation doesn’t seem to say anything about this, or I don’t understand something.
Here are three examples:
Example 1:

Order::create([
    'field' => 10,
    'field2' => 15,
]);

Example 2:
Order::whereKey([1, 2])->update([
    'field' => 10,
    'field2' => 15,
]);

Example 3:
Order::find(1)->update([
    'field' => 10,
    'field2' => 15,
]);

As a result, example 1 and 3 do not work (as expected because fields are not allowed for mass assignments), but the second one works fine, why? And under what other options will it not work when using models?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
Victor, 2018-05-27
@Camaro67

Because the second option calls update on the entire selection, and not on a specific model. In this case, protection, events, mutators and other Eloquent goodies will not work.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question