Laravel, many-to-many relationship to multiple models

Looking for an easy way to do the following in Laravel 5.

Imagine these models and interface

  • Contest
  • User
  • Team
  • Member (the interface that the user and the team implement)

The competition will have a many-to-many relationship with participants who can be either users or teams. So I need a competition_participant pivot table to have the following columns for model definitions

  • competition_id
  • participant_id
  • participant_type

But how can I write the relation on the Competition model, so he knows which model he should extract from the database, and at the same time return a collection of mixed models or an interface type?

+4
2

, .

users teams. particpatable_id particpatable_type, id timestamps().

competition_participant, competitions participants.

participants competitions. , , $participant->participatable, App\User, App\Team .

.

public function up()
{
    Schema::create('competitions', function(Blueprint $table) {
        $table->increments('id');
        $table->string('name');
        $table->timestamps();
    });

    Schema::create('teams', function(Blueprint $table) {
        $table->increments('id');
        $table->string('name');
        $table->timestamps();
    });

    Schema::create('participants', function(Blueprint $table) {
        $table->increments('id');
        $table->integer('participatable_id');
        $table->string('participatable_type');
        $table->timestamps();
    });

    Schema::create('competition_participant', function(Blueprint $table) {
        $table->integer('competition_id');
        $table->integer('participant_id');
    });
}

class Competition extends Model
{
    public function participants()
    {
        return $this->belongsToMany(Participant::class);
    }
}

class Participant extends Model
{
    public function participatable()
    {
        return $this->morphTo();
    }

    public function competitions()
    {
        return $this->belongsToMany(Competition::class);
    }
}

class Team extends Model
{
    public function participants()
    {
        return $this->morphMany(Participant::class, 'participatable');
    }
}

class User extends Authenticatable
{

    public function participants()
    {
        return $this->morphMany(Participant::class, 'participatable');
    }

}

public function run()
{
    $faker = Faker\Factory::create();

    // Seed Users Table
    DB::table('users')->delete();

    $users = [];
    for($i = 0; $i < 100; $i++) {
        $users[] = [
            'name' => $faker->name,
            'email' => $faker->email,
            'password' => Hash::make($faker->password),
            'created_at' => new DateTime,
            'updated_at' => new DateTime
        ];
    }
    DB::table('users')->insert($users);


    // Seed Teams Table
    DB::table('teams')->delete();
    $teams = [];
    for($i = 0; $i < 20; $i++) {
        $teams[] = [
            'name' => 'Team ' . ucwords($faker->domainWord),
            'created_at' => new DateTime,
            'updated_at' => new DateTime
        ];
    }
    DB::table('teams')->insert($teams);


    // Seed Participants Table
    DB::table('participants')->delete();

    // Insert some of our users as participants
    $users = App\User::limit(20)->orderByRaw('rand()')->get();
    foreach($users as $user) {
        $user->participants()->create([]);
    }

    // Insert some of the teams as participants
    $teams = App\Team::limit(10)->orderByRaw('rand()')->get();
    foreach($teams as $team) {
        $team->participants()->create([]);
    }

    // Seed Competitions Table
    DB::table('competitions')->delete();

    $competitions = [];

    for($i = 0; $i < 10; $i++) {
        $competitions[] = [
            'name' => $faker->company,
            'created_at' => new DateTime,
            'updated_at' => new DateTime,
        ];
    }

    DB::table('competitions')->insert($competitions);


    // Seed Competitions Participants Relationships
    DB::table('competition_participant')->delete();

    // Sign up each participant to 3 random competitions
    $participants = App\Participant::all();
    $competitions = App\Competition::all();

    foreach($participants as $participant) {
        $participant->competitions()->sync($competitions->shuffle()->take(3));
    }
}

    $competition = App\Competition::with('participants')->has('participants')->first();

    foreach($competition->participants as $participant) {
        echo get_class($participant->participatable); // Will output either App\User or App\Team
        echo "<br />";
    }
+10

Laravel .

, , Laravel:

users == posts

teams == videos

competitions == tags

competitionables == tags

, , , :

:

users
    id - integer
    name - string

teams
    id - integer
    name - string

competitions
    id - integer
    name - string

competitionables
    competition_id - integer
    competitionable_id - integer
    competitionable_type - string

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class User extends Model
{
    /**
     * Get all of the Competition for the user.
     */
    public function competitions()
    {
        return $this->morphToMany('App\Competition', 'competitionable');
    }
}

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Team extends Model
{
    /**
     * Get all of the Competition for the Team.
     */
    public function competitions()
    {
        return $this->morphToMany('App\Competition', 'competitionable');
    }
}

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Competition extends Model
{
    /**
     * Get all of the users that are assigned to this Competition.
     */
    public function users()
    {
        return $this->morphedByMany('App\User', 'competitionable');
    }

    /**
     * Get all of the Teams that are assigned to this Competition.
     */
    public function teams()
    {
        return $this->morphedByMany('App\Video', 'competitionable');
    }
}

$user = App\User::find(1);

foreach ($user->competitions as $competition) {
    //
}

$competition = App\Competition::find(1);

foreach ($competition->users as $user) {
    // do something with your users
}

foreach ($competition->teams as $team) {
    // do something with your teams
}
+2

Source: https://habr.com/ru/post/1624417/


All Articles