Vaughan Slater
Vaughan Slater

Reputation: 115

Check if no records exist in view laravel

Okay so I've hit a wall for some reason and I can't figure this out. I wan't to check if there are any records in a query and then use an @if in my view to say either "yes, there are records" or "none". Nothing I've tried is working..

What I want:

<div class="col-md-12 col-style">
    <h1 class="no-border">YOUR LATEST CONVERSIONS</h1>
        @if(something here that lets me test if $transactions is empty)
        <table class="table table-striped">
            <tr>
                <th>#</th>
                <th>Type</th>
                <th>User</th>
                <th>Amount</th>
                <th>Value</th>
                <th>Result</th>
                <th>Result Value</th>
                <th>Date</th>
            </tr>
            @foreach($transactions as $transaction)
                <tr>
                    <td>{{ $transaction->id }}</td>
                    <td>{{ $transaction->status }}</td>
                    <td>{{ $transaction->username }}</td>
                    <td>{{ $transaction->amount }}</td>
                    <td>{{ $transaction->value }}</td>
                    <td>{{ number_format($transaction->result, 2) }}</td>
                    <td>{{ $transaction->result_value }}</td>
                    <td>{{ $transaction->created_at }}</td>
                </tr>
            @endforeach
        </table>
        @else
            yo no records
        @endif
</div>

My controller:

public function index($username)
{
    $user = User::where('username', $username)->firstOrFail();
    $id = $user->id;

    $transactions = Transactions::where('user_id', '=', $id)->take(5)->get();

    return view('user.profile', compact('user', '=', 'userCurrency', 'transactions'));

So if you see in the top part there's a basic @if. Maybe I'm over thinking it, or am I passing in something that cant be checked the way I want it to?

Thanks!

Upvotes: 0

Views: 4631

Answers (3)

Raymond Cheng
Raymond Cheng

Reputation: 2505

@if (!$transactions->isEmpty())
...
@endif

this use laravel collection method do the trick.

Upvotes: 3

Anurag Saxena
Anurag Saxena

Reputation: 468

@if($transactions) should do the trick.

Upvotes: 0

Mohamed Bouallegue
Mohamed Bouallegue

Reputation: 1362

it is as easy as:

@if(count($transactions) > 0)

Upvotes: 1

Related Questions