Hello I just noticed one weird behaviour of softDelete. Basically, when I query for a related set of models, Eloquent returns a collection that also contains soft deleted rows.
I have been following the 4.2 guides on traits usage for softdelete and my code works just fine as long as I get/delete/restore/force delete my models. The issue is triggered with relations.
Consider this scenario: I have a user model that has a belongToMany friendship relation where the friendship status can be accepted/pending/requested/blocked as follows:
public function friends() {
return $this->belongsToMany('User', 'friends', 'user_id', 'friend_id')->where('status', 'accepted');
}
This friends table rows are basically "vectors" where user1->status->user2 and viceversa (user2->status->user1 is on another row). When user1 decides not to be friend with user2 anymore, the 2 friends rows are softdeleted.
Here is the issue: when I query the DB from the controller like this:
$friends = $user->friends;
even the softdeleted rows show up in the returned collection even though I would have expected these to be hidden from results unless I used ->withTrashed().
I suspect the belongsToMany() method doesn't take into account the deleted_at field on the pivot table.
Did anyone faced a similar issue? Am I doing something wrong with this relation?
Thanks a lot for your help!!!