0

Hello I have tried to Join tables from multiple database, below is my code I was just wondering if there is another or shorter way and best & safer approach in constructing my code.

$variable = Mymodel::join('database1.table1 as t1','t1.id','=','database2.table2.t1_id')->get();

And is it possible in Laravel to have relationship(1to1,1tomany,manytomany) of two table with a different database? Or only join method is the only way to connect them? If possible can please give some example thanks.

llinvokerl
  • 1,029
  • 10
  • 25
Greigh
  • 25
  • 7
  • Have a time to read this documentation from laravel https://laravel.com/docs/6.x/eloquent-relationships – Poldo Jan 07 '20 at 01:31
  • Here are some discussions regardint to the use of query builder, union and pagination. https://stackoverflow.com/questions/25338456/laravel-union-paginate-at-the-same-time/58118253#58118253 – Phil Jan 07 '20 at 04:59
  • how bout the multiple database is the approach same for all table? – Greigh Jan 07 '20 at 05:54
  • If you read [table conventions](https://laravel.com/docs/6.x/eloquent#eloquent-model-conventions) there's a way to define a custom table name for your model. This custom name can be `database2.table2` if you need the model to be read from a different database schema. The rest is just Laravel boilerplate – apokryfos Jan 07 '20 at 08:20

0 Answers0