laravel BelongsTo relationship with different databases not working

后端 未结 9 935
無奈伤痛
無奈伤痛 2020-12-06 00:25

I\'ve seen in several places to \"stay away\" from this, but alas - this is how my DB is built:

class Album extends Eloquent {

   // default connection

            


        
相关标签:
9条回答
  • 2020-12-06 00:42

    This is my own solution and it works in general for me but its mega-complicated.

    I'm using the builder "from" method to set the table and database correctly inside the subquery. I just need to pass the correct information inside.

    Assume the subquery can be as complicated as "genres.sample" or even deeper (which means albums has a relation to genres, and genres has a relation to samples) this is how

    $subQuery = 'genres.samples';
    $goDeep = (with (new Album));
    
    $tableBreakdown =  preg_split('/\./', $subQuery); //  = ['genres', 'samples']
    
    // I recurse to find the innermost table $album->genres()->getRelated()->sample()->getRelated()
    foreach ($tableBreakdown as $table)
        $goDeep = $goDeep->$table()->getRelated();
    
    // now I have the innermost, get table name and database name
    
    $alternativeConnection =  Config::get("database.connections." . $goDeep->getConnectionName() . ".database"); // should be equal to the correct database name
    
    $tableName = $goDeep->getTable(); // I have to use the table name in the "from" method below
    
    Album::whereHas($subQuery, function ($q) use ($alternativeConnection, $tableName) {
    $q->from("$alternativeConnection.$tableName"); 
    $q->where(....... yadda yadda);
        });
    

    tl:dr;

    Album::whereHas('genres', function ($q) { 
        $q->from('resources.genres')->where(....); 
    });
    
    0 讨论(0)
  • 2020-12-06 00:50

    To start change 'Resources' in database.php by 'resources', will be better !

    I'm curious, can you try that ?

    Album::whereHas('genre', function ($q) {
       $q->setConnection('resources')->where('genre', 'German HopScotch'); 
    });
    
    0 讨论(0)
  • 2020-12-06 00:53

    I was facing the same issue on Laravel 5.6. On a Many-to-Many scenario, and supposing the connection from my ModelA was the default one, what I did was the following:

    1.- Prefix the schema name in the relationships:

    // From ModelA and default connection (a.k.a connection1)
    $this->belongsToMany('ModelB', 'schema.pivot-table-name');
    
    // From ModelB and connection2
    $this->belongsToMany('ModelA', 'schema.pivot-table-name');
    

    2.- Overwrite connection parameter within the ModelB class and also specify the schema as a prefix in the overwritten $table attribute e.g.

    protected $connection = 'connection2';
    
    protected $table = 'connection2-schema-name.table';
    

    3.- In case of requiring a custom behavior for the pivot table, what I did was just to implement the required model and specify it via the ->using('PivotModel'); function on the models relationships (as stated in the documentation). Finally I did the same as in the point 2 above, but on the pivot model

    I haven't tried it yet, but I guess the same can be done for other kind of relationships, at least for the basic ones (One-to-One, One-to-Many, etc)

    0 讨论(0)
提交回复
热议问题