Solution:
This occurs because the condition in the relation here:
public function orders()
{
if($this->hasRole("seller")) {
return $this->hasMany('App\Models\Order', 'seller_id', 'id');
} else if($this->hasRole("client")) {
return $this->hasMany('App\Models\Order', 'user_id', 'id');
}
}
Imagine users who has neither roles ‘seller’ or ‘client’ then Null
will be returned and that is exactly the issue.
I recommend you to split the method orders
into two methods sellerOrders
and clientOrders
.