-
Notifications
You must be signed in to change notification settings - Fork 11.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Eloquent firstOrCreate() doesn't work with cast attributes #12238
Comments
Further infomation: The "find" part of |
It would be my first real contribution but i'll try to fix this one and see the impacts, i can't set myself as assignee though since i do not have write access to this! |
I've studied the code mostly the query builder and model/builder part. If you were to always do: MyModel::where(['data' => [1,2,3])
//or
MyModel::where('data', '=', [1,2,3]) Then it would be feasible, but problem is that if you where to start a query from another point in the chain, then it wouldn't be possible because you would be in the context of the query and at that point we do not have the reference to the model (i think, can't be sure) but just the table name. This means we cannot cast asJson the values passed to the where at that point... Mutators and casts are operational at the model level not the query builder level. There is maybe a way to query using a model such as creating a getRawAttributeValue function that returns the already casted / mutated value but then again, because of mutators which may alter many attributes at once, you never know if something else was modified also: $myModel = new MyModel();
$myModel->data = [1,2,3];
$raw = $myModel->getRawAttributeValue('data');
var_dump(MyModel::where('data', $raw)); //Would work I'll wait for another contributor before implementing this as there is probably something better that i'm not aware of. Note: I tried my potential solution but i got a really weird problem i can't seem to diagnose. When i use a cast to json, my end data in the database looks like "[1,, 2,, 3]" and not the standard "[1,2,3]" notation from json_encode. I'm not sure where this comes from, is it because of the different changes it tried to do to fix it but something's weird! |
Also Eloquent firstOrCreate() doesn't work with fillable attributes too |
I've just run into the same issue as @assada using firstOrCreate with a date property. |
Also just ran into this issue where attribute mutators are not taken into account with firstOrNew(). So, watching this thread. |
I think you can close this issue and reopen it if you find it in 5.5 |
The code has not changed in 5.5. I am still curious if this is considered a bug since there was a revert of a fix back in Aug 2016 |
Moved the discussion to laravel/ideas#856, it's very hard to say if this behaviour is wanted or not, so let's first discuss it before we say it's a bug to be fixed or is the current behaviour the one intended. |
This might be related to #5578, but if I have a model that uses
$casts
, then thefirstOrCreate()
always creates a new instance. For example:The text was updated successfully, but these errors were encountered: