0
Say guys all right?
Next, I have a little problem related to two models within my project.
First Model I have a relationship hasOne
:
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class User extends Model
{
protected $with = ['phone'];
/**
* Get the phone record associated with the user.
*/
public function phone()
{
return $this->hasOne('App\Phone');
}
}
In the second Model I do the reverse relationship BelongsTo
:
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Phone extends Model
{
protected $with = ['user'];
/**
* Get the user that owns the phone.
*/
public function user()
{
return $this->belongsTo('App\User');
}
}
When I add the protected $with
, put together to return the relationship, is on a loop and does not process.
Who can give me a light for it to return both the normal relationship as well as the reverse?
My return when I take the user route is:
{
"id":1,
"email":"[email protected]",
"phone":{
"id":1,
"phone":"123456",
"user_id":1,
"user":{
"id":1,
"email":"[email protected]",
"phone":{
"id":1,
"phone":"123456",
"user_id":1,
"user":{
"id":1,
"email":"[email protected]",
"phone":{
"id":1,
"phone":"123456",
"user_id":1,
"user":{
"id":1,
"email":"[email protected]",
"phone":{
"id":1,
"phone":"123456",
"user_id":1,
"user":{
... /*infinito*/
}
}
}
}
}
}
}
}
}
Ueh what is the relationship? You are confused if you are asking one and talking about another
– novic
Virginio, I edited the question by placing the return and showing the moment when it is in infinite loop.
– Eduardo Carlos
Take $with first
– novic