dongzhenge2014 2018-06-18 11:12
浏览 20
已采纳

Laravel也有同样的记录

I'm trying to take our some info from mysql with laravel. My controller:

$match->message = DB::table('log')->where('match_id', '=', $match->match_id)->get(['message']);

$match->authid = DB::table('log')->where('match_id', '=', $match->match_id)->get(['authid']);

My blade:

 @foreach ($match->authid as $username)
@foreach ($match->message as $text)
{{ $username->authid }} {{ $text->message }}<br />
@endforeach
@endforeach

But getting 4 results incorrectly. Getting:

  • AUTHD_ID1 log1
  • AUTHD_ID1 log2
  • AUTHD_ID2 log1
  • AUTHD_ID2 log2

Should be:

  • AUTHD_ID1 log1
  • AUTHD_ID2 log2

Whats wrong?

  • 写回答

3条回答 默认 最新

  • dongmeirang4679 2018-06-18 11:16
    关注

    It is being duplicated since you have a foreach within a foreach.

    Try the code below.

    $matches = DB::table('log')->where('match_id', '=', $match->match_id)->get();
    
    @foreach ($matches as $match)
    {{ $match->authid }} {{ $match->message }}<br />
    @endforeach`
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 用土力学知识进行土坡稳定性分析与挡土墙设计
  • ¥70 PlayWright在Java上连接CDP关联本地Chrome启动失败,貌似是Windows端口转发问题
  • ¥15 帮我写一个c++工程
  • ¥30 Eclipse官网打不开,官网首页进不去,显示无法访问此页面,求解决方法
  • ¥15 关于smbclient 库的使用
  • ¥15 微信小程序协议怎么写
  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?