Laravel using custom rotation (Extends model or Extends Pivot)

I am really stuck using a custom model for my core, and I don’t understand why we can create a custom model for it if we cannot use it as a model. I saw a lot of answers and articles, and everyone decided to expand Pivot or Model, but the use of real life never takes into account and never goes further than giving it a “named” class.

Here is an example of what I'm trying to achieve: My main models: Player , with a table of players . A game with a table of games .

Player and Game have a many-to-many relationship that we can call " Party " with game_player .

Less important now, I also have a Score model that contains scores / turns, Party can have a lot of points, so the table score has a party_id entry.

So, here are basically my classes (Laravel 5.2):

Player

class Player extends Model
{
    // The Games this Player is participating to.
    public function games() {
        return $this->belongsToMany(Game::class)->withPivot('id')->withTimestamps();
    }

    // The Scores belonging to this Player.
    public function parties() {
        return $this->hasManyThrough(Score::class, Party::class);
    }

    // The Custom Pivot (Party) creation for this Player.
    public function newPivot(Model $parent, array $attributes, $table, $exists) {
        if ($parent instanceof Game) {
            return new Party($parent, $attributes, $table, $exists);
        }
        return parent::newPivot($parent, $attributes, $table, $exists);
    }
}

A game

class Game extends Model
{
    // The Players participating into this Game.
    public function players() {
        return $this->belongsToMany(Player::class)->withPivot('id')->withTimestamps();
    }

    // The Scores belonging to this Party.
    public function parties() {
        return $this->hasManyThrough(Score::class, Party::class);
    }

    // The Custom Pivot (Party) creation for this Game.
    public function newPivot(Model $parent, array $attributes, $table, $exists) {
        if ($parent instanceof Player) {
            return new Party($parent, $attributes, $table, $exists);
        }
        return parent::newPivot($parent, $attributes, $table, $exists);
    }
}

The consignment

class Party extends Pivot
{
    protected $table = 'game_player';

    // The Player this Party belongs to.
    public function player() {
        return $this->belongsTo(Player::class);
    }

    // The Game this Party belongs to.
    public function event() {
        return $this->belongsTo(Game::class);
    }

    // The Scores belonging to this Party.
    public function scores()
    {
        return $this->hasMany(Score::class);
    }
}

Rating

class Score extends Model
{
    // The Party this Score belongs to (has "party_id" column).
    public function party() {
        return $this->belongsTo(Party::class);
    }
}

Now I have two different sets of problems that appear depending on whether I extend the model or Pivot at Party:

1) When a Party expands the Code:

  • It is impossible to do something like Party :: count () , anything that works with models.
  • - Party:: where ( "player_id", $player- > id) .. , , / .
  • - $events- > player- > first() → score() event $events- > player- > first() → pivot- > score(),

PHP: 1 Illuminate\Database\Eloquent\Relations\Pivot:: __ construct() Illuminate\Database\Eloquent\Model

2) :

  • , Party:: count(), Party:: where ( "Game_id", $game- > id)
  • , $game- > , $game- > players(): $game- > players() → count() , $game- > () → first() $game- > players- > first() :

PHP: 1 Illuminate\Database\Eloquent\Model:: __ construct() ,

:

?

:

  • $events- > players- > attach ($ player)
  • , $score- > party_id = $events- > players- > find ($ x) → pivot- > id ( )
  • , (, Party:: count(), $player- > parties- > count() ..)

.

+4
1

laravel , pivot tables , relatinship many-to-many, A B. , .

, A / B. , (, Sales), userA productB? .

, " " ( - ), , , .

, 4 :

  • | one-to-many
  • | one-to-many
  • | one-to-one | many-to-one | many-to-one Player
  • | one-to-one

.

0

All Articles