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

jmacdiarmid's avatar

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>

0 likes
4 replies
CorvS's avatar

@jmacdiarmid Your forgot to add ->get(). Right now you are passing a query builder to the view.

1 like
tykus's avatar
tykus
Best Answer
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
MichalOravec's avatar

@jmacdiarmid You can use orderByDesc instead of orderBy in your case.

$contracts = Contract::orderByDesc('id')->get();

return view('livewire.contracts', compact('contracts'));

You miss tbody element in your table.

<table>
    <thead>
        <tr>
            <th>Company</th>
        </tr>
    </thead>

    <tbody>
        @forelse ($contracts as $contract)
            <tr>
                <td>
                    {{ $contract->formatted_company }}
                </td>
            </tr>
        @empty
            <tr>
                <td>
                    Sorry - No Data Found
                </td>
            </tr>
        @endforelse
    </tbody>
</table>

And you can use accessor to limit company text, It's useful if you want to use it in other places as well.

use Illuminate\Support\Str;

public function getFormattedCompanyAttribute()
{
    return Str::limit($this->company, 25);
}

Docs: https://laravel.com/docs/8.x/eloquent-mutators#defining-an-accessor

Another option how to check if you have empty collection or not is it isNotEmpty() or isEmpty()

<table>
    <thead>
        <tr>
            <th>Company</th>
        </tr>
    </thead>

    <tbody>
        if ($contracts->isNotEmpty())
            @foreach ($contracts as $contract)
                <tr>
                    <td>
                        {{ $contract->formatted_company }}
                    </td>
                </tr>
            @endforeach
        @else
            <tr>
                <td>
                    Sorry - No Data Found
                </td>
            </tr>
        @endif
    </tbody>
</table>

Docs: https://laravel.com/docs/8.x/collections#method-isnotempty

Also if you have more columns in the table and for empty table you can use colspan attribute.

<table>
    <thead>
        <tr>
            <th>Column 1</th>

            <th>Column 2</th>
        </tr>
    </thead>

    <tbody>
        <tr>
            <td colspan="2">
                Sorry - No Data Found
            </td>
        </tr>
    </tbody>
</table>

Docs: https://developer.mozilla.org/en-US/docs/Web/HTML/Element/td#attributes

1 like

Please or to participate in this conversation.