Clayton Engle
Clayton Engle

Reputation: 581

Modifying the SQL Eloquent uses per connection

I'm using the latest version of Laravel to connect to multiple data sources. One of those sources is an old Oracle database that contains a lot of white space thanks to some ancient software requiring string length. Despite this constraint, the fields have since been edited by new software with different requirements, and the columns are all varying length (and thus unknown).

On account of this, I need to edit the SQL that accesses it in order to wrap some pieces of the queries in trim()s. For instance,

$customer = Customer::whereRaw("RTRIM(\"ID\") = TO_CHAR($id)")->get();

I'd like to be able to merely call the find method:

$customer = Customer::find($id)

This is just one example. Pretty much all of the default functions are broken because the queries need some kind of trim prepended to them. I understand how I can affect the dynamic portion of the query, but I need to edit the column ahead of that. Sorry if this is a dumb question and I've just missed something in the documentation.

Upvotes: 0

Views: 30

Answers (1)

aynber
aynber

Reputation: 23000

The easiest way to do this is to create a class that extends Eloquent, with the function for the query. Then modify all of your models to extend the new class instead of Eloquent. This way the models will have all the functionality of Eloquent as well as the functions you create.

Upvotes: 1

Related Questions