HungryBus-8342267's avatar

Use Oracle DB link in Eloquent Model

Hello,

In my application I use Oracle DB connection (it is not the only one, since I also have MySQL and MSSQL).

Oracle DB has a thing called DB link (basically you can specify "table_name@database_link") - this used typically in the replication, but it's not the point.

In the standard QueryBuilder, I can do something like DB::connection('oracle')->table(DB::raw('table_name@dblink')) and it works, but how can I do the same in the Eloquent model?

Doing $table = 'table_name@dblink will produce an error stating that it can't find table or view.

0 likes
2 replies
Tray2's avatar

You can't, unless you an eloquent extension for it, or add support in the Eloquent core..

You can however use the db link in your database, and use a view to fetch it over the link.

create or replace view other_db_table as
select * from some_table@otherdabase;

Then you can jusr create a model for it and query it like normal.

OtherDBTable::where('id', $idy)->get();
1 like
HungryBus-8342267's avatar

@Tray2 Not really: the new model won't work, unless I specify a new connection specifically for that DB link in laravel-oci8 config.

Since the same table is accessible via both DB links (this is a replicated DB), it won't work. I was asking if it is possible to do the same in model itself, without changing the connection params.

I know it works in simple QueryBuilder like ->table(DB::raw('table_name@dblink'))

Please or to participate in this conversation.