framework
framework copied to clipboard
[9.x] Eloquent Accessing Nested Array Value
Fix inconsistence for dealing with nested array on Eloquent.
Currently you can set nested array values, but not access them:
$model = new Model;
$model->{'a->b'} = 'something'; // this works
dump($model->{'a->b'}) // this will return null
This PR fixes this behavior.
@dbpolito I feel like you are missing a json cast or an ArrayObject cast for this attribute. Am i missing something?
You mean here? https://github.com/laravel/framework/blob/9.x/tests/Database/DatabaseEloquentModelTest.php#L2451 add meta
as an array
cast?
We can definitely do that, but it won't impact this test anyway... i did notice that and i didn't add because there are other tests already related to json field, like: https://github.com/laravel/framework/blob/9.x/tests/Database/DatabaseEloquentModelTest.php#L1228
But i can change to use this other model instead: https://github.com/laravel/framework/blob/9.x/tests/Database/DatabaseEloquentModelTest.php#L2792
Just updated my tests, let me know if you also want to update this one https://github.com/laravel/framework/blob/9.x/tests/Database/DatabaseEloquentModelTest.php#L1228-L1245 to use the proper model with cast too...
Can't you just use $model->a->b
?
Thanks for your pull request to Laravel!
Unfortunately, I'm going to delay merging this code for now. To preserve our ability to adequately maintain the framework, we need to be very careful regarding the amount of code we include.
If possible, please consider releasing your code as a package so that the community can still take advantage of your contributions!
If you feel absolutely certain that this code corrects a bug in the framework, please "@" mention me in a follow-up comment with further explanation so that GitHub will send me a notification of your response.
Well this isn't a bug per say but it's an inconsistency which has bitten me...
We can access by $model->a->b
but let's say you're using a dynamic variable:
$path = 'a->b';
$model->{$path} = 'something'; // works
$model->{$path}; // doesn't work
Setting an attribute this way works, but accessing it doesn't work... which creates an inconsistency...
In my case i used this to set stuff several times, and got the assumption that accessing would also work, which didn't.
Maybe we should remove the support to setting attribute this way.
@taylorotwell 👆