简体   繁体   中英

Laravel 5.6 - eloquent many to many error 1066

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>{{ $player->firstName.' '.$player->lastName }}</li>
<li>{{ $player->id }}</li>
@if($player->teams)
  <li>{{ $player->teams->id }}</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!

The parameters of the functions are set to the pivot tables. You should set them to the final model. Try this:

public function teams() {
    return $this->belongsToMany('App\Team', 'players_in_teams', 'FKplayerID', 'FKteamID');
}

public function players(){
    return $this->belongsToMany('App\Player', 'players_in_teams', 'FKteamID', 'FKplayerID');
}

I would make sure that in your migrations your foreign keys on your joining table are set properly. Something like this:

$table->foreign('FKplayerID')->references('id')->on('players')->onDelete('cascade')->onUpdate('cascade');
$table->foreign('FKteamID')->references('id')->on('teams')->onDelete('cascade')->onUpdate('cascade');

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM