-
Hello,
Here is the code from my controller : $users_query = User::query()->when(null !== $request->certified,
function (Builder $query) use ($request) {
if ($request->certified) {
$query->whereHas('role', function (Builder $query) {
return $query->name === 'professional_certified';
});
} else {
$query->whereDoesntHave('role', function (Builder $query) {
return $query->name === 'professional_certified';
});
}
});
echo $users_query->toSql();
exit; That throws this error :
My user rather simple model is like so: <?php
namespace App\Models;
use Illuminate\Contracts\Auth\MustVerifyEmail;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;
use Laravel\Sanctum\HasApiTokens;
use Spatie\Permission\Traits\HasRoles;
use Zoha\Metable;
class User extends Authenticatable implements MustVerifyEmail
{
use HasApiTokens, HasFactory, Notifiable, HasRoles, Metable;
/**
* The attributes that are mass assignable.
*
* @var array<int, string>
*/
protected $fillable = [
'lastname',
'firstname',
'birthdate',
'email',
'password',
];
/**
* The attributes that should be hidden for serialization.
*
* @var array<int, string>
*/
protected $hidden = [
'password',
'remember_token',
];
/**
* The attributes that should be cast.
*
* @var array<string, string>
*/
protected $casts = [
'email_verified_at' => 'datetime',
'activated_at ' => 'datetime',
'tos_accepted_at ' => 'datetime',
'privacy_accepted_at' => 'datetime',
'password' => 'hashed',
];
/**
* Get the organization that owns the user.
*/
public function organization(): BelongsTo
{
return $this->belongsTo(Organization::class);
}
} |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments
-
It seems maybe your query is firing the scope when you were trying to write a bespoke lookup of your own, and they're working against each other. Did you mean something like this? $users_query = User::query();
if (null !== $request->certified) {
if ($request->certified) {
$users_query = $users_query->role('professional_certified');
} else {
$users_query = $users_query->withoutRole('professional_certified');
}
}
// note: withoutRole() scope requires v6.x
// but you could copy the withoutRole scope from the v6 trait into your User model as a temporary bridge until v6 is released If that's not what you meant, do you mind writing out in english what you "meant" that part of your controller to be doing? Perhaps there's another way to write it? |
Beta Was this translation helpful? Give feedback.
-
Change $query->whereHas('role'
$query->whereDoesntHave('role' To $query->whereHas('roles'
$query->whereDoesntHave('roles' |
Beta Was this translation helpful? Give feedback.
-
Thanks, I went for the trait because I have other uses of it. But @erikn69's answer works too. |
Beta Was this translation helpful? Give feedback.
It seems maybe your query is firing the scope when you were trying to write a bespoke lookup of your own, and they're working against each other.
Did you mean something like this?
If that's not what you meant, do you mind writing out in english what you "meant" t…