Hello, i'm having a little problem by filtering results.
Controller:
try
{
$data_details = OrderDetail::where('order_id', $id)
->when($filters['filter_by_code'], function ($data_details, $code) {
$data_details->where('codice_articolo', 'like', '%' . $code . '%')->firstOrFail();
})
->when($filters['filter_by_desc'], function ($data_details, $desc) {
$data_details->where('descrizione', 'like', '%' . $desc . '%')->get();
})
->paginate(30);
}
catch (ModelNotFoundException $e)
{
return redirect()->back()->with('error_filter', 'No results found!');
}
In my view (show.blade.php):
@if(session()->has('error_filter'))
<div class="alert alert-warning text-center">
{{ session()->get('error_filter') }}
</div>
@else
// rest of code
First when() query search for item ID and i'm using firstOrFail() because i'm sure it's unique
The second one searches for the description, so it could find more results.
I get an error when i search for a description that doesn't exist
The problem is that the error comes out here in same view file where i use the ID order from related table:
<h1 class="me-5">
<i class="fa-solid fa-list-ul me-3"></i>
Detail of order N. <span class="text-primary">#{{$data_details->first()->order_grid->id}}</span>
</h1>
The error i get : Attempt to read property "order_grid" on null
I also tried the other way for check the filter results like this but i have the same error then:
@if ($data_details->isEmpty())
<div class="alert alert-warning text-center">
<p class="fs-3 m-0">No results found!</p>
</div>
@else
Why i'm getting error on the grid table?