douyinglan2599 2015-10-03 22:12
浏览 90
已采纳

检查视图层中是否存在记录

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!

  • 写回答

3条回答 默认 最新

  • douxi3404 2015-10-04 00:15
    关注

    it is as easy as:

    @if(count($transactions) > 0)
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥20 基于MSP430f5529的MPU6050驱动,求出欧拉角
  • ¥20 Java-Oj-桌布的计算
  • ¥15 powerbuilder中的datawindow数据整合到新的DataWindow
  • ¥20 有人知道这种图怎么画吗?
  • ¥15 pyqt6如何引用qrc文件加载里面的的资源
  • ¥15 安卓JNI项目使用lua上的问题
  • ¥20 RL+GNN解决人员排班问题时梯度消失
  • ¥60 要数控稳压电源测试数据
  • ¥15 能帮我写下这个编程吗
  • ¥15 ikuai客户端l2tp协议链接报终止15信号和无法将p.p.p6转换为我的l2tp线路