Laravel Default Where caluse

- 1 answer

Ad

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

Ad

Answer

Ad

You might want to take a look at the SoftDelete Trait ( http://laravel.com/docs/5.1/eloquent#soft-deleting ) 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.

Ad
source: stackoverflow.com
Ad