jeudi 19 novembre 2015

Laravel relationship DB connection

I m trying to get Model relationship with different connection, basically my connections are dynamic.

$con = 'con1'
$persons = new \App\Models\Person;
$persons->setConnection($con);
$persons = $persons->where('somevalue', 1)->get()

So here I get Person from con1 (where con1 is stored in config/databse.php It can be con2, con3, con4 and etc.

However this works, but when I am trying to loop through this data and get relationship it switches to default table.

@foreach($persons as $person)
   {{$person->data->name}}
@endforeach

In above loop data is a belongsTo relation ship in Person Model, and it throws error because it switches back to default table and not using con1

It is possible to setConnection() and keep for hasMany relationship and also for belongsTo relationship?

I cant set protected $connection; in Model because connections are changble



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire