Reputation: 165
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:
Should be:
Whats wrong?
Upvotes: 0
Views: 111
Reputation: 820
If you need only two field data then you don't have to build two query in multiple variable you can do like below:
$data = DB::table('log')->where('match_id', '=', $match->match_id)->get(['authid','message']);
@if ($data)
@for ($i = 0; $i < count($data); $i++)
{{ $data[$i]->authid }} {{ $data[$i]->message }}
@endfor
@endif
Using above code you will never get error in foreach
if you didn't get data from $data
variable
Upvotes: 0
Reputation: 450
Try below
$match->message = DB::table('log')->where('match_id', '=', $match->match_id)->get(['message','authid']);
@foreach ($match->message as $text)
{{ $text->authid }} {{ $text->message }}
@endforeach
Upvotes: 0
Reputation: 331
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`
Upvotes: 3