I'm trying to setup a polymorphic relationship on multiple database on connection.
Let's say I have
- Database A, Table users (id, email, password, userable_id, userable_type)
- Database B, Table companies (id, name, address, phone)
On each User and Company model, I also set each connection:
/* Database A */
protected $connection = 'database_a';
/* Database B */
protected $connection = 'database_b';
And for the relationship
/* Database A (User model) */
public function userable()
{
return $this->morphTo();
}
/* Database B (Company Model) */
public function user()
{
return $this->morphMany('user','userable');
}
And in my AppServiceProvider.php, I morphMap the user and vendor model:
Relation::morphMap([
'user' => \App\Models\V1\Shared\User::class,
'vendor' => \App\Models\V1\Training\Vendor::class
]);
This code will not work unless I set the userable relationship with connection:
public function userable()
{
return $this->setConnection('database_b')->morphTo();
}
In case, I want to add more user type and I have to use the new database, How do I setConnection to appropriate database without having to use setConnection() on morphTo()?
via Chebli Mohamed
Aucun commentaire:
Enregistrer un commentaire