Krenor
Krenor

Reputation: 641

Hook Trait into each Eloquent Select Query

I've made a small Trait for filtering an Eloquent Query. Now I want to apply this function in the Trait to each SELECT query before returning instead of calling
$data = MyModel::all()->filterable(),
$data = MyModel::with('foo')->filterable()
etc each time. Is this actually possible?

For example, the nullable package does it by observing
via static::saving(function ($model) { ...
Can this be done for selecting data, too?

Upvotes: 0

Views: 1120

Answers (1)

Joseph Silber
Joseph Silber

Reputation: 220026

What you're looking for is called a Global Query Scope.

Upvotes: 2

Related Questions