Home > Back-end >  how to view different data from same row in laravel 8
how to view different data from same row in laravel 8

Time:04-10

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" >
<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'));
}

CodePudding user response:

 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(); 

CodePudding user response:

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;
  
  • Related