I'm implementing an application where some things should be approved first by an admin. So I added a column to my model needing approval.
Common users, all along the app, should only see models approved by an admin, so I added a global scope to the model to filter just the records approved by an admin. My problem is that this is not working ok with soft deletes.
When an admin decides to not approve something, he should be able to delete it. So, I do something like this:
$model = Model::withUnapproved()->find($model_id);
if(!$model){
//handle error
}
$model->delete();
This code actually finds the model, but when the delete is executed, the query is:
DELETE FROM table WHERE deleted_at IS NULL and approved_by IS NOT NULL and model_id=`id`;
Why it is adding the and approved_by IS NOT NULL
?? This causes any records to be found, so nothing is deleted.
If I change it to $model->withUnapproved()->delete()
it deletes all records.
Any lights on this?? Thanks in advance
Your global scope is just that - global. It applies to all Eloquent queries for that model, deletions included.