我正在尝试使用 laravel 从 mysql 获取一些信息。 我的 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']);
我的 Blade :
@foreach ($match->authid as $username)
@foreach ($match->message as $text)
{{ $username->authid }} {{ $text->message }}<br />
@endforeach
@endforeach
但错误地得到 4 个结果。获取:
- AUTHD_ID1 log1
- AUTHD_ID1 log2
- AUTHD_ID2 log1
- AUTHD_ID2 log2
应该是:
- AUTHD_ID1 log1
- AUTHD_ID2 log2
怎么了?
最佳答案
它正在被复制,因为您在 foreach 中有一个 foreach。
试试下面的代码。
$matches = DB::table('log')->where('match_id', '=', $match->match_id)->get();
@foreach ($matches as $match)
{{ $match->authid }} {{ $match->message }}<br />
@endforeach`
关于php - Laravel foreach 同一条记录多条,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50908172/