0

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?

1
  • Why not get(['message', 'authid'])? Commented Jun 18, 2018 at 11:14

3 Answers 3

3

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`
Sign up to request clarification or add additional context in comments.

11 Comments

Your code works great! U made me on the right road! Thanks! Another question: I have users table with row called 'auth_id' and 'username'. How can i convert to auth_id to username? Thanks for helping!
Can you elaborate on what you mean by converting auth_id into username?
Example: Users table has row called 'auth_id' and 'username'. My table 'log' also have row called 'auth_id'. I need to find same 'auth_id' on users table and get username by that 'auth_id'. Example: AUTH_ID1 to USERNAME5654 :) Sorry if my english is not unreadable :)
For this matter, you need to use joins. You have to join the users table and log table using the auth_id column. Here's a sample query: $username = \DB::table('users')->join('log', 'users.auth_id', 'log.auth_id')->select('users.username')->get(); Try to refer to this link for more information: laravel.com/docs/5.6/queries#joins
Oh everything works fine. But with username again i'm getting foreach not correctly. $match->messages = DB::table('log')->where('match_id', '=', $match->match_id)->get(); $match->messages->username = DB::table('users')->join('log', 'users.steam_id', 'log.authid')->select('users.username')->get(); and foreach in my view: @foreach ($match->messages as $message) @foreach ($match->messages->username as $username) {{ $username->username }}: {{ $message->message }}@endforeach @endforeach
|
0

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

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.