vincent15000's avatar

Retrieve a relationship with the soft deleted items

Hello,

I have the user model with the soft deletetion activated.

The user model is binded to the team model and can belong to many different teams, so it's a belongsToMany relationship via a pivot table.

// team model
public function users()
{
	return $this->belongsToMany(User::class);
}

In some cases I need to retrieve only the non deleted team's users.

$team = Team::find(4);
$members = $team->users;

In other cases I need to retrieve all the team's users, those non deleted and the solf deleted ones .

$team = Team::find(4);
$members = $team->users-> ... ?

How is it possible to retrieve the soft deleted users via the relationship ?

I have tried to add ->withTrashed() but it doesn't work.

$members = $team->users->withTrashed(); // doesn't work

If you can help me, it would be great ;).

Thanks for your help.

V

0 likes
13 replies
vincent15000's avatar

@MichalOravec Oh yes thank you ... I didn't thought about using the relationship with the parenthesis ;).

Is it possible to add a condition, for example retrieve the team's trashed users, but only the users which have participated to a quizz. I mean with a closure to filter the some unwanted trashed users.

vincent15000's avatar

@MichalOravec For example doing something like this.

$query->withTrashed(function ($query) {
    $query->has('quizzes');
});

This code generates no error, but it doesn't retrieve only the trashed users having quizzes.

vincent15000's avatar

@Snapey That's a quite different question, but around the same theme, the soft delete.

Result::with(['user' => function ($query) {
    $query->withTrashed(function ($query) {
        $query->has('quizzes');
    });
}])->get();

NOT ONLY the trashed, but WITH the trashed users.

The idea is that if a user is deleted but has already completed a quizz, his results are displayed in the list but as anonymous user.

MichalOravec's avatar

@vincent15000 You are working with eloquent for the first time?

$members = $team->users()->where(function ($query) {
    $query->withTrashed()
		->has('quizzes');
})->get();

BYE THE WAY close all your solved threads...

1 like
vincent15000's avatar

@MichalOravec

I really need help, sure I am working with Eloquent for years, but it's a bit confused for me with this case.

I should have open a new post for this question, effectively it's not anymore with the relationship, but with a classic query.

I need to retrieve :

  • all non deleted users, with or without quizzes

  • and additionally the deleted users, but only those who have quizzes

That's why I wanted to add a closure to withTrashed().

$query->withTrashed(function ($query) {
    $query->has('quizzes');
});

Your suggestion allows to retrieve all users (non deleted and deleted) but only those having quizzes.

I will close the solved threads as soon as I have solved them ;).

MichalOravec's avatar
Level 75
$members = $team->users()->withoutTrashed()->orWhere(function ($query) {
    $query->onlyTrashed()->has('quizzes');
})->get();
1 like
vincent15000's avatar

@MichalOravec I didn't know that it was possible to add ->orWhere() after withoutTrashed().

Thank you very much ;).

I'm on this forum for some years now and I appreciate it. Be sure that if I ask for something, that means that I have searched by myself before asking and that I don't find any solution.

Please or to participate in this conversation.