G
G
GrimJack2017-08-17 22:53:33
Laravel
GrimJack, 2017-08-17 22:53:33

Why is the json field not working?

There is a model with the specified field

protected $casts = [
        'properties' => 'object'
    ];

In the database in the users table there is a json field properties
I can't figure out how to store data in it and work with it.
For example, set the role:
$user = User::find(1);
        $user->properties->role = 'admin';
        $user->save();

Will lead to
Indirect modification of overloaded property App\User::$properties has no effect
If changed to array and properties['role'] then the effect is the same.
How to update and fill this field during insert/create?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
M
miki131, 2017-08-17
@miki131

$user->propertiesit is magic __get, which returns a new object, so changes to it won't affect the model itself.
Source

$user = User::find(1);

$temp = $user->properties;
$temp->role = 'admin';

$user->properties = $temp;
$user->save();

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question