@jmacdiarmid Your forgot to add ->get(). Right now you are passing a query builder to the view.
Mar 24, 2021
4
Level 2
In Livewire, how do I determine if an instance of a model is empty?
In my render method, I'm trying to retrieve all records ordered by Id and Desc
$contracts = Contract::orderBy('id', 'desc');
return view('livewire.contracts', compact('contracts'));
In my blade view of the component, I'm using $contracts to build a table. I have inside of a @if @endif where I check to see if it's empty or not. It's only displaying table header. Any thoughts about how I can fix this?
<table>
<thead>
<tr>
<th>blah blah name</th>
</tr>
</thead>
@if (!empty($contracts))
@if (count(array($contracts) > 0)
@foreach($contracts as $contract)
<tr>
<td> {{ Str::limit($contract->company, 25) }} </td>
</tr>
@endforeach
@endif
@else
<tr>
<td> Sorry - No Data Found </td>
</tr>
@endif
</table>
Level 104
FIrst, this Contract::orderBy('id', 'desc') is not a completed query; it is a Builder instance. You need to complete the query using get
$contracts = Contract::orderBy('id', 'desc')->get();
In the view there is a @forelse directive available to you which will do the empty check as well as the loop:
@forelse($contracts as $contract)
<tr>
<td> {{ Str::limit($contract->company, 25) }} </td>
</tr>
@empty
<tr>
<td> Sorry - No Data Found </td>
</tr>
@endforelse
1 like
Please or to participate in this conversation.