Filtering table, second filter doesn't works

->filters([
Filter::make('is_applicant')

->toggle()
->query(function (Builder $query) use ($userId) {
$query->where('user_id', $userId);
}),
Filter::make('is_responsible')

->toggle()
->query(function (Builder $query) use ($userId) {
$query->whereHas('project.responsible', function ($query) use ($userId) {
$query->where('user_id', $userId);
});
})
->filters([
Filter::make('is_applicant')

->toggle()
->query(function (Builder $query) use ($userId) {
$query->where('user_id', $userId);
}),
Filter::make('is_responsible')

->toggle()
->query(function (Builder $query) use ($userId) {
$query->whereHas('project.responsible', function ($query) use ($userId) {
$query->where('user_id', $userId);
});
})
the first filter shows only the requests created by me the second filter should display only the requests for which i'm responsible trough a project (a request belongTo a project, a project has many responsibles (user) ) in the request model (rams)
public function project()
{
return $this->belongsTo(RamsProject::class, 'project_id');
}

in the project model
public function responsible(): BelongsToMany
{
return $this->belongsToMany(User::class, 'rams_projects_users', 'project_id', 'user_id');
}

public function rams(): hasMany
{
return $this->hasMany(Rams::class, 'project_id');
}
public function project()
{
return $this->belongsTo(RamsProject::class, 'project_id');
}

in the project model
public function responsible(): BelongsToMany
{
return $this->belongsToMany(User::class, 'rams_projects_users', 'project_id', 'user_id');
}

public function rams(): hasMany
{
return $this->hasMany(Rams::class, 'project_id');
}
1 Reply
Soundmit
Soundmit4mo ago
commenting this part in the table

// ->modifyQueryUsing(
// fn (Builder $query) => $query->when(auth()->user()->hasAnyRole(['super_admin', 'rga', 'supervisor']) === false, function ($query) use ($userId) {
// $query->where('user_id', $userId) // if the user is the author of the request
// ->orWhereHas('project.responsible', function ($query) use ($userId) {
// $query->where('user_id', $userId); // if the user is the project responsible
// });
// })
// )

// ->modifyQueryUsing(
// fn (Builder $query) => $query->when(auth()->user()->hasAnyRole(['super_admin', 'rga', 'supervisor']) === false, function ($query) use ($userId) {
// $query->where('user_id', $userId) // if the user is the author of the request
// ->orWhereHas('project.responsible', function ($query) use ($userId) {
// $query->where('user_id', $userId); // if the user is the project responsible
// });
// })
// )
solve the filtering problem but i need that user can view only their request and requests for which are responsible without this code i see all the requests from all the users solved by adding this public static function getEloquentQuery(): Builder {
$userId = Auth::id(); return parent::getEloquentQuery()->when(auth()->user()->hasAnyRole(['super_admin', 'rga', 'supervisor']) === false, function ($query) use ($userId) { $query->where(function ($query) use ($userId) { $query->where('user_id', $userId) // Se l'utente è l'autore della richiesta ->orWhereHas('project.responsible', function ($query) use ($userId) { $query->where('user_id', $userId); // Se l'utente è responsabile del progetto }); }); }); }