3

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');
    }
}
Cœur
  • 37,241
  • 25
  • 195
  • 267
  • [This guy](http://stackoverflow.com/a/26573474/2047394) believes that it is impossible. – Huzaib Shafi Jul 30 '16 at 08:24
  • Unfortunately, this solution applies to the same server, I have two connections from different servers, one oracle and one mysql – Gustavo Perez Machado Aug 01 '16 at 04:08
  • I've linked to a comment not the question. The comment specifies that it is not possible – Huzaib Shafi Aug 01 '16 at 08:17
  • @GustavoPerezMachado Your solution doesn't work if databases are on different server. Can you look in [this](https://stackoverflow.com/questions/45768051)? – Akshay Vaghasiya Aug 19 '17 at 07:04
  • @AkshayVaghasiya this solution has worked for me very well so far, are different servers, as you can see an oracle server and a mysql, the version of laravel is 5.2 but I do not think a different aversion is a problem – Gustavo Perez Machado Aug 22 '17 at 05:27

0 Answers0