I'm doing a query with union
in Laravel.
$first = Friendship::selectRaw('id, userid, friendid, friendid as friend')
->where('userid', '=', $idUser)
->where('confirmed_friendship', '=', 1)
->whereHas('user_friends', function($where){
$where->where('suspended', '=', 0)->where('deleted', '=', 0);
});
$content = Friendship::selectRaw('id, userid, friendid, userid as friend')
->where('friendid', '=', $idUser)
->where('confirmed_friendship', '=', 1)
->whereHas('friends', function($where){
$where->where('suspended', '=', 0)->where('deleted', '=', 0);
})
->union($first);
$content = $content->get();
And I would like to make a relationship with another table using the alias I put in SELECT
.
public function user_friends(){
// Gostaria de indicar aqui a coluna que eu renomeei para 'friend'
return $this->belongsTo('App\User', 'friendid');
}
Is it possible to do this?