O. Shekriladze
O. Shekriladze

Reputation: 1536

Laravel get with polymorphic relationship after save()

I have something like that:

$order = new Order();
$order->name = "lorem"
//some polymorphic relationship (hasOne)
$order->user()->save(new User());
return $order;

the problem is that I want to get order returned with user. I tried something like that and it worked:

$order = new Order();
$order->name = "lorem"
//some polymorphic relationship (hasOne)
$order->user()->save(new User());
$order->user;
return $order;

But I feel that I am doing something wrong and there should be better way to achieve the same result, so how should I do that?

Upvotes: 0

Views: 59

Answers (1)

MyLibary
MyLibary

Reputation: 1771

Laravel Eloquent has 2 methods, load and with, you may choose the ideal one for you (in this case load).

You may use the following code:

$order = new Order();
$order->name = "lorem"
//some polymorphic relationship (hasOne)
$order->user()->save(new User());

return $order->load('user');

Upvotes: 1

Related Questions