Laravel Default Where caluse
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
Answer
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.
Related Questions
- → "failed to open stream" error when executing "migrate:make"
- → October CMS Plugin Routes.php not registering
- → OctoberCMS Migrate Table
- → OctoberCMS Rain User plugin not working or redirecting
- → October CMS Custom Mail Layout
- → October CMS - How to correctly route
- → October CMS create a multi select Form field
- → October CMS - Conditionally Load a Different Page
- → How to disable assets combining on development in OctoberCMS
- → October CMS - Radio Button Ajax Click Twice in a Row Causes Content to disappear
- → OctoberCms component: How to display all ID(items) instead of sorting only one ID?
- → In OctoberCMS how do you find the hint path?
- → How to register middlewares in OctoberCMS plugin?