Categories
Fixing Stuff Laravel Samuel

FIxing Laravel Query

We had a query that was breaking several pages after a client removed information that was referenced from another table (even thought the system explicitly warns them from removing this info lol). To fix the errors messages the client was receiving we needed to update our controller so that our database lookup only included items with vehicles present. To do this we made the following changes in two files:

Changes to our Inquiries Controller:
Step 1:
Replacing our DB lookup:
Old Query: $inquiries = Inquiry::all();

New Query: $inquiries = Inquiry::whereHas('vehicle_id')->get();

Step 2:
Fixing error message: Illuminate\Database\Eloquent\Builder::whereHas() must be an instance of Closure, none given'

Instead of using whereHas we were able to fix the error message by simply using has. This was a simple error in hindsight, we don’t need to query to lookup multiple items, we only need to know if the inquiry HAS a vehicle associated with it.

Our Final working fix: $inquiries = Inquiry::has('package.vehicle')->get();

Changes to our separate view files:
We also had issues related to inquiries on 4 other pages. The system references inquiries on part types as well and these pages all referenced an individual view file, so instead of refactoring this code into a controller we took the path of least resistance and made the changes below:

Original code:
@foreach($inquiries as $inquiry)
package; ?>
vehicle; ?>

{!! link_to_route('inquiries.show', $inquiry->id, ['id' => $inquiry->id]) !!} {!! link_to_route('vehicles.show', $vehicle->display(), ['id' => $vehicle->id]) !!} {!! link_to_route('packages.show', $package->display(), ['id' => $package->id]) !!} {{ $inquiry->getType()->name }} {{ $inquiry->getEngine()->name }} {{ $inquiry->getDriveTrain()->short_name }} {{ $inquiry->getMarking()->name }} {!! Helper::sortDollarAmount($inquiry->getPrice()) !!} {{ $inquiry->name }} {{ $inquiry->agency }} {{ $inquiry->email }} {{ $inquiry->displayPhone() }} {!! link_to_route('inquiries.excel', 'Excel', ['id' => $inquiry->id], ['class' => 'success']) !!}
 / {!! Form::deleteModelLink('Inquiry', $inquiry->id, 'inquiries') !!}

@endforeach

Changed Code:
@foreach($inquiries as $inquiry)
package; ?>
vehicle; ?>

@if($vehicle)

{!! link_to_route('inquiries.show', $inquiry->id, ['id' => $inquiry->id]) !!} {!! link_to_route('vehicles.show', $vehicle->display(), ['id' => $vehicle->id]) !!} {!! link_to_route('packages.show', $package->display(), ['id' => $package->id]) !!} {{ $inquiry->getType()->name }} {{ $inquiry->getEngine()->name }} {{ $inquiry->getDriveTrain()->short_name }} {{ $inquiry->getMarking()->name }} {!! Helper::sortDollarAmount($inquiry->getPrice()) !!} {{ $inquiry->name }} {{ $inquiry->agency }} {{ $inquiry->email }} {{ $inquiry->displayPhone() }} {!! link_to_route('inquiries.excel', 'Excel', ['id' => $inquiry->id], ['class' => 'success']) !!}
 / {!! Form::deleteModelLink('Inquiry', $inquiry->id, 'inquiries') !!}

@endif
@endforeach