Hello, i have 3 database tables, which structure looks like this
user:
id,
name,
email,
...,
rank:
id,
name,
users_ranks:
id,
user_id ->unique,
rank_id,
And my code for models looks like this:
User:
public function rank()
{
return $this->hasManyThrough('App\Rank', 'App\UserRank', 'user_id', 'id', 'id', 'rank_id');
}
UserRank:
public function rank()
{
return $this->belongsTo('App\Rank', 'rank_id', 'id');
}
public function user()
{
return $this->belongsTo('App\User', 'user_id', 'id');
}
Rank:
public function userRank()
{
return $this->hasMany('App\UserRank', 'rank_id', 'id');
}
public function user()
{
return $this->belongsToMany('App\User', 'user_id', 'id');
}
On Another model with belongsTo i was able to use withDefault on it. What about hasManyThrough, when i want to return some default values if the rank is null while fetching Users like this
User::with('Rank')->get();