Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

vidhyaprakash85's avatar

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

0 likes
10 replies
vidhyaprakash85's avatar

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

MichalOravec's avatar

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

aurawindsurfing's avatar
Level 50

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?

1 like

Please or to participate in this conversation.