I wonder if the similar query for the total count is triggering the warning; you can get the total count anyway from the LengthAwarePaginator instance:
$this->countmembers = $ensemblemember->total();
Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.
Hi - I'm getting n+1 warnings when I run a query using a 'with' condition.
Here's the model:
class Ensemblemember extends Model
{
use HasFactory, SoftDeletes;
protected $fillable = ['ensemble_id', 'instrumentation_id', 'schoolyear_id', 'teacher_user_id', 'user_id', ];
public function person()
{
return $this->belongsTo(Person::class, 'user_id', 'user_id');
}
public function instrumentation()
{
return $this->belongsTo(Instrumentation::class);
}
public function schoolyear()
{
return $this->belongsTo(Schoolyear::class);
}
}
Here's the Livewire method:
use App\Models\Ensemblemember;
use App\Models\Schoolyear;
use App\Models\Userconfig;
use Livewire\Component;
class MembersTable extends Component
{
...
private function ensemblemembers()
{
$ensemblemembers = Ensemblemember::with('person', 'instrumentation')
->where('schoolyear_id', $this->schoolyear_id)
->where('ensemble_id', Userconfig::getValue('ensemble_id', auth()->id()))
->paginate(Userconfig::getValue('pagination', auth()->id()));
//count total members regardless of pagination
$this->countmembers = Ensemblemember::where('schoolyear_id', $this->schoolyear_id)
->where('ensemble_id', Userconfig::getValue('ensemble_id', auth()->id()))
->count();
return $ensemblemembers;
}
...
}
and here are the resulting queries:
select * from 'ensemblemembers' where 'schoolyear_id' = 2020 and 'ensemble_id' = '1' and 'ensemblemembers'.'deleted_at' is null limit 4 offset 0
select * from `people` where `people`.`user_id` in (499, 500, 640, 840)
select * from `instrumentations` where `instrumentations`.`id` in (63, 64)
It looks to me like the query engine is ignoring the 'with' condition in the LIvewire method, but I'm not seeing what I've got wrong in the method.
Thanks - Rick
Please or to participate in this conversation.