Currently I am to do Model::where('status', 1)
to filter off inactive models before adding any other subqueries to it. I find myself doing this all the time and Its quite stressful as sometimes I could forget to add that part. I wish to have something that will automatically add the where clause anytime I run a query on the model. Something that works like the typical Laravel SoftDeletes that adds the WHERE deleted_at IS NULL
clause automatically to all queries.
Example:
Instead of writing Model::where('status', 1)->first()
, I could just write Model::first()
and this will be automatically transformed to Model::where('status', 1)->first()
.
How do I achieve this please ?
CodePudding user response:
What you're looking are the Query Scopes