I've got a problem with a many to many relationship in my laravel 5.6 project. I've got some different many-to-many relationships already working but I can't find what is wrong in this one. I've tried google and stackoverflow already but couldn't find the answer.
So, I've got 3 tables; players, teams and players_in_teams I would like to show a player and all the teams he is a part of.
This is my (simple) table layout:
Teams - id - teamName
Players - id - firstName - lastName
PlayersInTeams - id - FKplayerID - FKteamID
my code:
Player.php:
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Player extends Model
{
protected $fillable = [
'firstName', 'lastName'
];
public function teams() {
return $this->belongsToMany('App\PlayersInTeam', 'players_in_teams', 'FKteamID', 'FKplayerID');
}
}
Team.php
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Team extends Model
{
protected $fillable = ['FKuserID', 'teamName', 'teamDescription', 'FKmediaID'];
public function players(){
return $this->belongsToMany('App\PlayersInTeam', 'players_in_teams', 'FKteamID', 'FKplayerID');
}
}
PlayersInTeam.php
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class PlayersInTeam extends Model
{
protected $fillable = ['FKteamID', 'FKplayerID'];
}
PlayerController.php
public function index()
{
$players = Player::all();
return view('players.index', compact('players', $players));
}
showplayer.blade.php
<li></li>
<li></li>
@if($player->teams)
<li></li>
@endif
The full error i'm receiving:
SQLSTATE[42000]: Syntax error or access violation: 1066 Not unique table/alias: 'players_in_teams' (SQL: select players_in_teams
.*, players_in_teams
.FKteamID
as pivot_FKteamID
, players_in_teams
.FKplayerID
as pivot_FKplayerID
from players_in_teams
inner join players_in_teams
on players_in_teams
.id
= players_in_teams
.FKplayerID
where players_in_teams
.FKteamID
= 1) (View: ../resources/views/players/showplayer.blade.php)
If hope someone sees what I'm missing,
Thanks in advance!
via Chebli Mohamed
Aucun commentaire:
Enregistrer un commentaire