Laravel Relationship One to two columns Laravel Relationship One to two columns laravel laravel

Laravel Relationship One to two columns


Maybe you can try this:

class Team extends Model{    protected $table = 'teams';    public function gameOwner()    {        return $this->hasMany('App\Games', 'team_1');    }    public function gameVisitor()    {        return $this->hasMany('App\Games', 'team_2');    }    public function games() {        return $this->gameOwner->merge($this->gameVisitor);    }}class Game extends Model{    public function teamOvner() {        return $this->belongsTo('App\Team', 'team_1');    }    public function teamVisitor() {         return $this->belongsTo('App\Team', 'team_2');     }    public function teams() {        return $this->teamOvner->merge($this->teamVisitor);    }}


You might want to consider redesigning your database structure in order to satisfy the intent of relationships in Laravel.

For example you could have two Models

1) Team, with columns 'id' and 'name' (as you have it)

2) Game, with columns 'id' and 'date' (or any other specific identifier of the game like 'name' where you would name each game after the two contending teamsin camelCase for example).

Then you would reason the relationship as follows:

Many teams can participate in a given game (typically two teams but maybe more per game depending on the nature of the sporting event), and a team can participate in more than one game. This would be an example of a Many to Many relationship in Laravel.

Note: Only you can define the exact nature of the relationship based on the information you have but this is an educated guess for a relationship where there are a number of games in which there can be many teams for each game, and where a given team is able to participate in many games (presumably although not necessarily against different contenders each time, for example)

Then when you reference the models

In your Team model you will add the relationship

<?phpnamespace App;use Illuminate\Database\Eloquent\Model;class Team extends Model {public function games()  {return $this->belongsToMany('App\Game');}}

and in your Game model you would add the inverse of the relationship

<?phpnamespace App;use Illuminate\Database\Eloquent\Model;class Game extends Model {public function teams() {return $this->belongsToMany('App\Team');}}

Once the relationship is defined, Laravel will create a joining table with the name game_teams

You may access a team's games using the games dynamic property:

$team = App\Team::find(1);foreach ($team->games as $game) {//}

Of course, like all other relationship types, you may call the games method to continue chaining query constraints onto the relationship:

$games = App\Team::find(1)->games()->orderBy('name')->get();

Also, when creating your migrations, don't forget to include team_id in createGamesTable so that Laravel can link the two models.

<?phpuse Illuminate\Database\Schema\Blueprint;use Illuminate\Database\Migrations\Migration;class CreateGamesTable extends Migration{public function up(){Schema::create('games', function(Blueprint $table) {    $table->increments('id');    $table->integer('team_id')->index();    $table->text('name');    $table->timestamps();    });

Eloquent will try to match the team_id from the Game model to an id on the Team model. Eloquent determines the default foreign key name by examining the name of the relationship method and suffixing the method name with _id.

Similarly you will want to register game_id in the Team model.