Framework: can not use `push([ ... ])` in column `$cast` as `collection` when null

Created on 23 Jun 2017  路  5Comments  路  Source: laravel/framework

  • Laravel Version: 5.4.19
  • PHP Version: 7.1.4
  • Database Driver & Version: Not required.

Description:

Working with a model that has a column $cast as collection when trying to save an initial value, ie the $zmodel->column = null, I can not ->push([ ... ]) any values.

Instead I need to do

$zmodel->column = $zmodel->column->push(['Adam' => 'Wathan']);

expected

$zmodel->column->push(['Adam' => 'Wathan']);

Most helpful comment

@ninjaparade I wanted to cast attributes to a collection recently too.

I noticed in the documentation (and source diving) that you have to set the property back. There's really no way around it with dynamic attributes, unless I am missing something obvious (happens all the time 馃槃 )

$user = App\User::find(1);
$options = $user->options;
$options['key'] = 'value';
$user->options = $options;
$user->save();

That's the very last code example in the documentation.

For me $zmodel->column = $zmodel->column->push(['Adam' => 'Wathan']); triggers Call to a member function push() on null too as expected. Just following along here, hopefully I can learn more about collection casting.

All 5 comments

@ninjaparade I wanted to cast attributes to a collection recently too.

I noticed in the documentation (and source diving) that you have to set the property back. There's really no way around it with dynamic attributes, unless I am missing something obvious (happens all the time 馃槃 )

$user = App\User::find(1);
$options = $user->options;
$options['key'] = 'value';
$user->options = $options;
$user->save();

That's the very last code example in the documentation.

For me $zmodel->column = $zmodel->column->push(['Adam' => 'Wathan']); triggers Call to a member function push() on null too as expected. Just following along here, hopefully I can learn more about collection casting.

+1 seeing this too. Very confusing at first.

There was conversation about this issue on internal repo https://github.com/laravel/ideas/issues/460

And I created some PRs and got rejected...
https://github.com/laravel/framework/pull/24508
https://github.com/laravel/framework/pull/24527

As a general question to the main developers regarding this issue: Laravel casts the attribute on every access. It would make much more sense to instead cast on Model load/save only. That way, casted attributes would be modifiable and if the dirty check before save is done after casting (on a copy of the attributes array with all attributes casted to how they belong into the database), this would not need much changes in existing Eloquent code. Would this be a welcome change in Laravel or not?

Heya. Thanks for sending this in. Gonna close this in favor for https://github.com/laravel/ideas/issues/460 so please continue the discussion there.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

JamborJan picture JamborJan  路  3Comments

ghost picture ghost  路  3Comments

lzp819739483 picture lzp819739483  路  3Comments

CupOfTea696 picture CupOfTea696  路  3Comments

kerbylav picture kerbylav  路  3Comments