Laravel Default Where caluse

- 1 answer


In Laravel Eloquent model, is it possible to set the Model so it would always use a default Where clause unless specified otherwise.

So for example turn this:

$activeRecords = Records::where('status','active')->get()

Into this:

$activeRecords = Records::all();

But if I would like to get inactive records I would have to tell it todo so




You might want to take a look at the SoftDelete Trait ( ) its not exactly the same as with your status flag but is quite a clean way of doing it.

By adding the deleted_at column to your schema and introducing the trait use SoftDeletes; on your model you can then delete records but they won't actually be removed from the database.

If you then did Records::all() you would see all records that haven't been deleted if you want the ones that have been deleted you can access then with the withTrashed method.