-
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
[5.3] Allow Eloquent has one relations to return a default new model #16198
[5.3] Allow Eloquent has one relations to return a default new model #16198
Conversation
Well this is a breaking change I believe since Moreover I think it's better for these nullable relationships to check before you use:
|
a948c08
to
5f1542e
Compare
@themsaid Does it? It should return That way you should be able to do:
Without being worried of profile being In the view you could use:
But this change should be activated if and only if you call |
got it now, not breaking :) |
Can you do |
@taylorotwell yes, but it's not easy, to do it right you require something like this:
The problem is not only that the object is null but that if you create a new one it won't be related to the user (in this example) by default. |
@taylorotwell also having |
I know that I'm not a high profile dude, but IMHO I think this is very bad. Having a default model returned is very confusing, especially to newcomers. This To me having |
@shadoWalker89 I'm not changing the default behavior, if you don't use Also there is a pattern in OOP call Null Object in which you replace null by a null object in that way you eliminate or simplify the conditionals in the code. You can find a video in Laracasts to check why and when it is useful to have a null object instead of null. |
Also notice that with hasMany you get an empty collection, not null. And you need to call isEmpty() to know if the collection is empty or not. |
->setAttribute( | ||
$this->getPlainForeignKey(), $this->getParentKey() | ||
); | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Shouldn't it return null
explicitly if $this->withDefault
evaluates to false?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
if you don't return anything, the method will return null
by default. So there is an implicit return null;
at the end of the method.
How would this work if profile was eager loaded from an array of models? |
@taylorotwell since the |
@taylorotwell ok I made that last change and tested with |
5f1d2e0
to
87ff047
Compare
I did add one feature to this. You can do: ->hasOne('Foo')->withDefault(function () {
return new SomeValue;
}); |
use Illuminate\Database\Eloquent\Collection; | ||
|
||
class HasOne extends HasOneOrMany | ||
{ | ||
/** | ||
* Determine whether getResults should return a default new model instance or not. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This variable doesn't determine anything, rather, the description should be just "If getResults should return a default model instance."
So I've been dealing with this problem: I have a user and I need to get its profile, but the user does not have one, so something like this throws an exception:
$thi->profile->twitter
(Trying to use a property on a non-object whatever whatever...)So I came with this idea:
Now when the result is not found, Eloquent creates one new model instance for me and I can continue with my life...
This is just a proposal / proof of concept.