I need to make the relationship between two connections. A user can have many different clients and databases.
class User extends Authenticatable
{
protected $connection = 'mysql';
}
class Client extends Model
{
protected $connection = 'sqlsrv';
}
I created a ClientUser helper table with two columns user_id and client_id, client_type.
In User Model I created the method:
public function clients()
{
return $this->hasMany(ClientUser::class);
}
And in ClientUser Model created the method:
public function client()
{
return $this->morphTo('client');
}
But every time I want to access the clients of a particular user I have to call Model ClientUser first and then Model Client: Exp.:
$user = \App\User::find(1);
$clientUser = $user->clients()->get();
foreach ($clientUser as $item) {
echo $item->client()->first()->Name;
}
I would like to know if there is any way to access Model Client directly from Model User?