0

Here you can see I can store two different team names in my table .

This is my database

I want to view them in my index.blade.php so I did this in my index.blade.php but it`s showing team 1 name everytime.

<table id="example1" class="table table-bordered table-striped table-sm">
<thead>
  <tr>
     <th>SL</th>
     <th>Match Name</th>
     <th>Match Slug</th>
     <th>Team 1 Name</th>
     <th>Team 2 Name</th>
     <th>Action</th>
     </tr>
</thead>
<tbody>
     @foreach ($data as $key => $row)
     <tr>
     <td>{{ $key + 1 }}</td>
     <td>{{ $row->match_name }}</td>
     <td>{{ $row->match_slug }}</td>
     <td>{{ $row->team->team_name }}</td>
     <td>{{ $row->team->team_name }}</td>
     </tr>
@endforeach
</tbody>
</table>

Its showing team 1 name everytime what should I change

This is my index method

public function index()
{
   $data=Matchh::all();
   $team=Team::all();
   return view('admin.manage.matchh.index', compact('data','team'));
}
2
  • You need to fix your query, try joining the match table to your team table Commented Apr 10, 2022 at 3:08
  • I can store different team name you can see my database have different team id I just only wants to view team Commented Apr 10, 2022 at 3:12

2 Answers 2

1
 Try this apply this code
 
     $Matchh = DB::table('Matchh')
                ->join('Team', 'Matchh.team_id', '=', 'Team.team_id')
                ->join('Team', 'Matchh.team2_id', '=', 'Team.team2_id')
                ->select('Matchh.matchh_name','Matchh.matchh_slug','Team.team1_name','Team.team2_name_')
                ->get(); 
Sign up to request clarification or add additional context in comments.

Comments

0

you need a self join per reference table. Try this:

  $result = DB::select('SELECT
  m.match_name,
  m.match_slug,
  t1.team_name as team1_name,
  t2.team_name as team2_name

  FROM matchh as m
  LEFT JOIN team as t1 ON m.team_id = t1.id
  LEFT JOIN team as t2 ON m.team2_id = t2.id');
  return $result;
  

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.