In my Profile model I setted this relationship
public function lease()
{
return $this->belongsTo(Lease::class, 'lease_id', 'id');
}
And in my Lease model I seeted this way
public function profile()
{
return $this->hasOne(Profile::class, 'lease_id', id);
}
As longs as I know in laravel you could do
$profile = factory(App\Profile::class)->create();
$profile->lease()->get();
And then responds correctly with the model inside of a collection And if I do $profile->lease
Responds correctly directly with the model
It isn't supposed that dynamic propertis execute the query right away like a shortcut of ->lease()->get()? Why it gives different formatted results?
When you are calling get
on a builder you are getting a collection always. When you call first
on a builder like that you will get a model or null. The dynamic property for the relationship, based upon the relationship object, will either query with get
or first
respectively when it loads it. Which is why $model->relationship
is returning you the result you expect.
The relationships that are singular, cause a find
and the ones that are many cause a get
.
Laravel 5.4 - Docs - Eloquent - Relations - Relationship Methods vs Dynamic Properties