doubt with getEloquentQuery

I have a resource with the following getEloquentQuery:
if (isAdmin() || isSuperAdmin()) {
$query = parent::getEloquentQuery()
->withoutGlobalScopes([
SoftDeletingScope::class,
]);
} else {
$query = parent::getEloquentQuery()
->whereHas('customer.applicants', function ($q) {
$q->where('user_id', auth()->user()->id)->vip();
})
->orwhereBelongsTo(auth()->user(), 'requestedBy')
->withoutGlobalScopes([
SoftDeletingScope::class,
]);

\Log::info(auth()->user()->id);
\Log::info($query->toRawSql());

return $query;
if (isAdmin() || isSuperAdmin()) {
$query = parent::getEloquentQuery()
->withoutGlobalScopes([
SoftDeletingScope::class,
]);
} else {
$query = parent::getEloquentQuery()
->whereHas('customer.applicants', function ($q) {
$q->where('user_id', auth()->user()->id)->vip();
})
->orwhereBelongsTo(auth()->user(), 'requestedBy')
->withoutGlobalScopes([
SoftDeletingScope::class,
]);

\Log::info(auth()->user()->id);
\Log::info($query->toRawSql());

return $query;
In the table I see everything correct, but if I enter any of them in view/edit it always shows me the same record in all of them. Am I missing something? Thanks
10 Replies
awcodes
awcodes15mo ago
Maybe getTableQuery instead? Unless you need the same logic to apply.
H.Bilbao
H.BilbaoOP15mo ago
Ok, I will try, the logic is the same for the table and for the form. I only have problems with the query inside the else. What I find strange is that in the table I see the correct records, but instead when I open the form to view/edit I only see the same for all records
awcodes
awcodes15mo ago
Yea, that’s what’s weird to me. It doesn’t make sense that the scope is being applied to a single record.
H.Bilbao
H.BilbaoOP15mo ago
I can't solve the problem. The table is shown with the correct records, but when I open the form it is always the same record. I don't know what else to do, because the table is displayed correctly...
H.Bilbao
H.BilbaoOP15mo ago
Example:
No description
H.Bilbao
H.BilbaoOP15mo ago
If I delete from GetEloquentQuery function this, no problems:
->whereHas('customer.applicants', function ($q) {
$q->where('user_id', auth()->user()->id)->vip();
})
->whereHas('customer.applicants', function ($q) {
$q->where('user_id', auth()->user()->id)->vip();
})
awcodes
awcodes15mo ago
What is vip()?
H.Bilbao
H.BilbaoOP15mo ago
scope:
public function scopeVip(Builder $query): void
{
$query->where('is_vip', true);
}
public function scopeVip(Builder $query): void
{
$query->where('is_vip', true);
}
awcodes
awcodes15mo ago
There’s your problem. The query will only ever return the logged in user.
H.Bilbao
H.BilbaoOP15mo ago
Yes, but filter not working. I have the same problem with this:
return parent::getEloquentQuery()->withoutGlobalScopes([SoftDeletingScope::class])
->whereBelongsTo(auth()->user(), 'ownedBy')
->orWhereBelongsTo(auth()->user(), 'requestedBy');
return parent::getEloquentQuery()->withoutGlobalScopes([SoftDeletingScope::class])
->whereBelongsTo(auth()->user(), 'ownedBy')
->orWhereBelongsTo(auth()->user(), 'requestedBy');
When I enter a second condition the filters stop working. If I leave one of the conditions it works fine. Can anyone do a quick test to see if it fails as well? Thanks
Want results from more Discord servers?
Add your server