Read this part of documentation https://laravel.com/docs/7.x/eloquent-relationships#eager-loading
Eloquent blade duplicate queries
I have student management application. While indexing the student list in the blade page it takes too much of time because of duplicate queries.
I have attached the image https://ibb.co/hXMT7SM
Now i have 450 students later i may have 5000 students bit worrying about the delay. Please help me
Controller
$mystudents = Student::with(
array(
'gender' => function ($query) {
$query->select('id', 'gender_name');
},
'status' => function ($query) {
$query->select('id', 'status_name');
},
'probation' => function ($query) {
$query->select('id', 'probation_name');
},
'advisor' => function ($query) {
$query->select('id', 'name');
},
))->get();
blade
@foreach($mystudents as $student)
<tr>
<td>
{{$loop->index+1 ?? ""}}
</td>
<td>
{{ $student->studentid ?? '' }}
</td>
@if(auth()->user()->IsAdmin())
<td>
{{ $student->advisor->name ?? 'Not Avalibale' }}
</td>
@endif
<td>
{{ $student->studentname ?? '' }}
</td>
<td>
{{ $student->mobilenumber ?? '' }}
</td>
<td>
{{ $student->status->status_name ?? '' }}
</td>
<td>
{{ $student->probation->probation_name ?? '' }}
</td>
<td>
@can('scenario_create')
<a class="btn btn-xs btn-success"
href="{{ route('admin.students.scenario', $student->id) }}">
Create Scenario
</a>
@endcan
@can('students_show')
<a class="btn btn-xs btn-primary"
href="{{ route('admin.students.show', $student->id) }}">
{{ trans('global.view') }}
</a>
@endcan
@can('students_edit')
<a class="btn btn-xs btn-info"
href="{{ route('admin.students.edit', $student->id) }}">
{{ trans('global.edit') }}
</a>
@endcan
@can('transcript_create')
<a class="btn btn-xs btn-warning"
href="{{ route('admin.transcript.create')}}">
Transcript
</a>
@endcan
@can('students_delete')
<form action="{{ route('admin.students.destroy', $student->id) }}"
method="POST"
onsubmit="return confirm('{{ trans('global.areYouSure') }}');"
style="display: inline-block;">
<input type="hidden" name="_method" value="DELETE">
<input type="hidden" name="_token" value="{{ csrf_token() }}">
<input type="submit" class="btn btn-xs btn-danger"
value="{{ trans('global.delete') }}">
</form>
@endcan
</td>
</tr>
@endforeach
@michaloravec this is my code bro
You don't put there forein keys, or just simple
$students = Student::with(['gender', 'status', 'probation', 'advisor'])->get();
And why do you name your variable mystudents better when it will only students
And $student->studentid, $student->studentname etc why did you name columns as that?
Again better $student->id, $student->name
@michaloravec here is the list of duplicate queries. I tried your syntax.
It's because you use @can('students_show') etc.
How to reduce the queries
@michaloravec even if remove it showing same queries
As @michaloravec already answered, use eager loading: https://laravel.com/docs/7.x/eloquent-relationships#eager-loading
This is where your queries are originating from:
@if(auth()->user()->IsAdmin())
Remove it and check. Try handling it in controller rather then in the view maybe?
@aurawindsurfing thank you sir you are correct
Please or to participate in this conversation.