Laravel: how to set connection for many to many relationship?

最后都变了- 提交于 2019-12-23 02:29:07

问题


I have the models

class User extends Model
{
    protected $connection = 'mysql';

    public function areas()
    {
        return $this->belongsToMany(Area::class, 'user_areas');
    }
}


class Area extends Model
{    
    protected $connection = 'oracle';
}

the user_areas table is searched on oracle, but this are on mysql.

How I can indicate the connection for the pivot table?


I found this partial solution and it has worked

class User extends Model
{
    protected $connection = 'mysql'

    public function areas()
    {
        $instance = new Area;
        $instance->setConnection('mysql');
        return new BelongsToMany($instance->newQuery(), $this, 'user_areas', 'user_id', 'area_id', 'areas');
    }
}

来源:https://stackoverflow.com/questions/38671664/laravel-how-to-set-connection-for-many-to-many-relationship

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!