Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

MostofaL's avatar

How to do a surgery on an Eloquent Query on the fly

Hi,

is there any way to interrupt and change the components and scopes of an eloquent query?

in order to add multitenancy to an existing project, I've added a global scope to my models, filtering results by tenant_id. and it works fine.

the problem is I have found more than 500 hardcoded 'where conditions ' and 'create statements' all over the place. such as these:

$notification_type= NotificationTypes::where('id', '2')->get();

or

$tickets = Tickets::create( $title, $body, $sender, '1'); // 1 as statusID

etc.

It's a problem because I'm using the single DB approach for multitenancy and these IDs must be relative to the tenant. for Example in the first query above, I don't want the 'NotificationTypes' with Id of '2', But I want the 'second' NotificationType with that tenant_id (id of this column could be 4 or 7 or else).

I can figure out a way to properly calculate the exact amount of these Relative IDs. but is there any way to find the prior scope and conditions of an eloquent object and change them?

I'm looking for a way to add multitenancy to the project without changing much. like a module or plugin.

0 likes
7 replies
Sinnbeck's avatar

There are several multi tenant packages. Just Google "laravel multi tenant"

1 like
MostofaL's avatar

@Sinnbeck thanks. I have done that before. but decided to implement it myself. and I'm also curious about the answer.

Sinnbeck's avatar

@mostafaalotfi@gmail.com ok then I honestly don't know. You can get access to queries as they are executed (check debug bar), but I expect the amount of data you have access to it most likely limited, unless you have some global state in the service container you can refer to

1 like
Snapey's avatar

What you described should be handled by the global scope, assume you are adding something like

$query->where('tenant_id', session('tenant'))

into every query and that every table has a tenant_id column

1 like
MostofaL's avatar

@Snapey Yep, I've done that.

the problem is the hardcoded codes. for example, when a new ticket arrives, it automatically triggers a 'create' statement for Notification, with the fixed status_id of '2' (which is a new ticket status). the global scope with tenant_id condition is useless here unless there is a way in which I could modify that status_id on the fly. for example, the 'new ticket' status for tenant 6 has the id of '19' in the statuses table. I want to modify the prior query based on the tenant_id and put '19' instead of '2' in the last example.

Snapey's avatar

@mostafaalotfi@gmail.com you should not be tying behaviour to specific primary keys. Thats the problem you need to fix, which you can only do by visiting all such statements and removing 'magic numbers'

1 like
jlrdw's avatar

@mostafaalotfi@gmail.com

but decided to implement it myself

You could still study the code in the package to get ideas how it's done.

1 like

Please or to participate in this conversation.