Jump to content

Memory limit issue with Laravel blade


fastsol

Recommended Posts

So I have a page that is loading a good amount of models and then looping those in the blade file.  The issue I am trying to fix is on my hosting server I keep getting this error

Symfony\Component\Debug\Exception\FatalErrorExceptionGET /admin/quotes/appointments

Allowed memory size of 33554432 bytes exhausted (tried to allocate 53248 bytes

This only happens on my hosting server which is a dedicated server with 8GB of meomry and the php.ini file is set to 500M already.  BUT on my local laptop where I only have 128M set in the ini file and run the exact same data on the page it loads just fine.  Why is the memory having an issue on my hosting server but not local that is set at a lower limit?

I've already tried running the loop with the chunk method but it hasn't helped.  I'm not having an N+1 issue either cause debugbar shows the same number of queries ran no matter how big the data set I pull from the db.  This is a critical page in my site and it's busy season right now, so this is a very bad thing to have happen.  Luckily it only affects one page in the admin area so my customers aren't affected by it.

Link to comment
Share on other sites

2 hours ago, requinix said:

33554432 bytes is 32MB...

Yes I know.

This is the blade file code:

@extends('layouts.admin')

@section('content')

    @include('layouts.partials.form-errors')
    <div class="row">
        <div class="col-lg-2">
            <a href="{{ route('admin.quotes.appointments.phone.create') }}" class="mr-4">Add Install Time</a>
        </div>
        <div class="col-lg-2">
            <a href="{{ route('admin.quotes.appointments.off') }}" class="mr-4">Take Days Off</a>
        </div>
        <div class="col-lg-5">
            <form action="{{ route('admin.quotes.appointments.index') }}" method="get" class="form-inline mb-2">
                <input type="text" name="date" class="form-control datepicker mb-2" placeholder="Search Date" value="{{ request('date') }}">
                <button class="btn btn-primary mt-0 ml-2 mb-2">Search</button>
            </form>
        </div>
    </div>

    @if($search)
        {!! alert('warning', 'Currently in Search Mode') !!}
    @endif

    @if($appointments->count())
        <div id="backtotop">Back To Top</div>
        <form action="{{ route('admin.quotes.appointments.purchased') }}" method="post" id="mark-as-done">
            @include('helpers.previous_page_input_full_url')
            @csrf
            <button class="btn btn-primary float-right mb-2">Mark Checked Installs As Done</button>
            @adminTable(['headings' => ['Date', 'Name', 'Vehicle', 'Product / Notes', 'Actions']])
            @foreach($appointments->chunk(5) as $chunks)
            @foreach($chunks as $appointment)
                <tr>
                    <td>
                        @php
                            $color = $appointment->install_time->lessThan(\Carbon\Carbon::tomorrow()) ? 'text-danger'
                        : ($appointment->install_time->lessThan(\Carbon\Carbon::tomorrow()->addDay()) ? 'text-primary' : '')
                        @endphp
                        <span class="{{ $color }}">{{ $appointment->install_time->format(config('sitespec.scheduler.install_time_display_format')) }}</span>
                    </td>
                    <td>
                        @if($appointment->deposit)
                            <div class="customer-name">
                                <a href="{{ searchByUrl($appointment->deposit->quote->customer->name) }}"
                                   data-toggle="tooltip" data-placement="bottom" title="{{ $appointment->deposit->quote->customer->phone }}">
                                    {{ $appointment->deposit->quote->customer->name }}
                                    @if($appointment->deposit->quote->customer->gift_name)
                                        &nbsp;{!! GIFT_ICON !!}
                                    @endif
                                </a>
                                <div class="customer-details">
                                    {{ $appointment->deposit->quote->customer->phone }}
                                    @if($appointment->deposit->quote->customer->gift_name)
                                        <br><br>
                                        {{ $appointment->deposit->quote->customer->gift_name }}
                                    {!! $appointment->deposit->quote->customer->gift_phone ?
                                            '<br>'.$appointment->deposit->quote->customer->gift_phone : '' !!}
                                        {!! $appointment->deposit->quote->customer->gift_date  ?
                                            '<br>'.$appointment->deposit->quote->customer->gift_date->format('m-d-Y') : '' !!}
                                        @endif
                                    </div>
                                </div>
                            @elseif(optional($appointment->phoneAppointment)->customer)
                                <a href="{{ searchByUrl($appointment->phoneAppointment->customer) }}">
                                    {{ $appointment->phoneAppointment->customer }}
                                </a>
                            @endif
                        </td>
                        <td class="text-center">
                            @if($appointment->deposit)
                                {{ $appointment->deposit->quote->customer->strippedVehicle }}
                                @if($appointment->deposit->quote->customer->vyear->wires->count())
                                    <a href="{{ route('admin.wires.show', $appointment->deposit->quote->customer->vyear) }}">{!! INFO_ICON !!}</a>
                            @else
                                {!! RED_X !!}
                            @endif
                        @elseif(optional($appointment->phoneAppointment)->vehicle)
                            {{ $appointment->phoneAppointment->vehicle }}
                        @endif
                    </td>
                    <td class="text-center">
                        @if($appointment->deposit)
                            {{ $appointment->deposit->quote->changedProduct->count() ? $appointment->deposit->quote->changedProduct->first()->name :
                            $appointment->deposit->product->name }}
                        @elseif(optional($appointment->phoneAppointment)->product)
                            {{ $appointment->phoneAppointment->product->name }}
                        @endif
                            {{ $appointment->notes ? ' - '.$appointment->notes : '' }}
                    </td>
        @php
            $bg = ($appointment->deposit && !$appointment->deposit->quote->purchased && now() > $appointment->install_time->addHour(2)) ? 'bg-warning' : '';
        @endphp
                    <td class="text-center text-md {{ $bg }}">
                        <a href="{{ $appointment->deposit ? route('admin.quotes.appointments.app.edit', $appointment->deposit) :
                                route('admin.quotes.appointments.phone.edit', $appointment) }}" class="mr-2">
                            {!! EDIT_ICON !!}
                        </a>
                        @if($appointment->deposit && $appointment->deposit->quote->purchased)
                            {!! CHECK_MARK !!}
                        @else
                            <a href="javascript:void(0)" class="delete_record mr-2"
                               data-url="{{ route('admin.quotes.appointments.destroy', $appointment) }}" data-toggle="modal"
                               data-target="#destroy_confirmation">{!! GARBAGE_ICON !!}</a>
                            @if($appointment->deposit)
                                <div class="custom-control custom-checkbox custom-control-inline">
                                    <input type="checkbox" class="custom-control-input" id="purchased{{ $appointment->id }}"
                                           name="purchased[]" value="{{ $appointment->deposit->id }}">
                                    <label class="custom-control-label" for="purchased{{ $appointment->id }}"></label>
                                </div>
                            @endif
                        @endif
                    </td>
                </tr>
            @endforeach
            @endforeach
            @endadminTable
        </form>
        @include('partials.modals.destroy_confirmation')
    @else
        {!! alert('warning', 'No Appointments scheduled at this time.') !!}
    @endif

@endsection

@section('scripts')
    <script>
        $(function () {
            $('[data-toggle="tooltip"]').tooltip()
        });
    </script>
@endsection

And this is the query:

$appointments = Appointment::with(
    		'deposit.quote.customer.vmake',
			'deposit.quote.customer.vmodel',
			'deposit.quote.customer.vyear.wires',
			'phoneAppointment.product',
			'deposit.product',
			'deposit.quote.changedProduct'
		)
								->where(function($query) use($validator) {
								    return $this->appointmentWhere($query, $validator);
                                })
								->whereNull('cancelled')
								->orderBy('install_time')
								->get();

As you can see I'm loading a fair number of relationships.  The query is in the Controller.  The problem comes when it tries to render the blade file.  It will get so far in rendering and then error out of memory.  I'm using the chunk method but even setting the chunk to 5 or less doesn't solve the problem.  I imagine the problem is arrising from trying to access the deep relationship data on so many lines multiple times.  I've also tried something like this but it too didn't help.

@php $customer = $appointment->deposit->quote->customer; @endphp
                            <div class="customer-name">
                                <a href="{{ searchByUrl($customer->name) }}"
                                   data-toggle="tooltip" data-placement="bottom" title="{{ $customer->phone }}">
                                    {{ $customer->name }}
                                    @if($customer->gift_name)
                                        &nbsp;{!! GIFT_ICON !!}
                                    @endif
                                </a>
                                <div class="customer-details">
                                    {{ $customer->phone }}
                                    @if($customer->gift_name)
                                        <br><br>
                                        {{ $customer->gift_name }}
                                    {!! $customer->gift_phone ?
                                            '<br>'.$customer->gift_phone : '' !!}
                                        {!! $customer->gift_date  ?
                                            '<br>'.$customer->gift_date->format('m-d-Y') : '' !!}
                                        @endif
                                    </div>
                                </div>

That was to reduce the number of relationship dives to get at the data.

Link to comment
Share on other sites

48 minutes ago, fastsol said:

Yes I know.

Then you must have realized that

14 hours ago, fastsol said:

and the php.ini file is set to 500M already

isn't the whole truth. It may very well say 500M in the php.ini you saw but something somewhere is setting it to 32M instead.

Link to comment
Share on other sites

White mage? Oh, no, nevermind.

First use phpinfo() to see what the "master" (original) setting was. If that's 32M then the setting is coming from the php.ini, or another INI, or .htaccess, or something like that.

If the master value is correct and the local value is 32M then... well, that's going to be kinda hard to track down since it could happen anywhere. Search your codebase for "memory_limit" or "32M".

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.