title: Laravel custom pivot usage (Extends Model or Extends Pivot)
body: <p>I am really stuck using a custom model for my pivot, and don't understand why can we make a custom model for it if we can't use it as a model.
  I've seen many answers and articles, and all chose to extend Pivot or Model, but real life usage is never taken into account and never go any further than giving it a "named" class.</p>
  
  <p>Here is an example of what I'm trying to achieve:
  My main models:
  <strong>Player</strong> model, with <strong><em>players</em></strong> table.
  <strong>Game</strong> model, with <strong><em>games</em></strong> table.</p>
  
  <p><strong>Player</strong> and <strong>Game</strong> have a many-to-many relationship, that we can call "<strong>Party</strong>" with a "<strong><em>game_player</em></strong>" table.</p>
  
  <p>Less important for now, I have also a <strong>Score</strong> model that holds the score/turns, a <strong>Party</strong> can have many scores, so <strong><em>scores</em></strong> table has a party_id entry.</p>
  
  <p>So, here are basically my classes (Laravel 5.2):</p>
  
  <p>Player</p>
  
  <pre class="lang-php prettyprint-override"><code>class Player extends Model
  {
      // The Games this Player is participating to.
      public function games() {
          return $this-&gt;belongsToMany(Game::class)-&gt;withPivot('id')-&gt;withTimestamps();
      }
  
      // The Scores belonging to this Player.
      public function parties() {
          return $this-&gt;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);
      }
  }
  </code></pre>
  
  <p>Game</p>
  
  <pre class="lang-php prettyprint-override"><code>class Game extends Model
  {
      // The Players participating into this Game.
      public function players() {
          return $this-&gt;belongsToMany(Player::class)-&gt;withPivot('id')-&gt;withTimestamps();
      }
  
      // The Scores belonging to this Party.
      public function parties() {
          return $this-&gt;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);
      }
  }
  </code></pre>
  
  <p>Party</p>
  
  <pre class="lang-php prettyprint-override"><code>class Party extends Pivot
  {
      protected $table = 'game_player';
  
      // The Player this Party belongs to.
      public function player() {
          return $this-&gt;belongsTo(Player::class);
      }
  
      // The Game this Party belongs to.
      public function event() {
          return $this-&gt;belongsTo(Game::class);
      }
  
      // The Scores belonging to this Party.
      public function scores()
      {
          return $this-&gt;hasMany(Score::class);
      }
  }
  </code></pre>
  
  <p>Score</p>
  
  <pre class="lang-php prettyprint-override"><code>class Score extends Model
  {
      // The Party this Score belongs to (has "party_id" column).
      public function party() {
          return $this-&gt;belongsTo(Party::class);
      }
  }
  </code></pre>
  
  <p>Now, I have two different sets of problems that appear depending on if I extend Model or Pivot on Party:</p>
  
  <p>1) When Party extends Pivot:</p>
  
  <ul>
  <li>Can't do something like <strong>Party::count()</strong>, anything that works with models.</li>
  <li>Can't do something like <strong>Party::where("player_id", $player->id)</strong> and so on ... Basically I want the party to have a scores/turns table attached to it and to do so I need to select the pivot model.</li>
  <li>Can't do something like $events->player->first()->scores() nor event $events->player->first()->pivot->scores(), it breaks with the following:</li>
  </ul>
  
  <blockquote>
    <p>PHP error:  Argument 1 passed to Illuminate\Database\Eloquent\Relations\Pivot::__construct() must be an instance of Illuminate\Database\Eloquent\Model</p>
  </blockquote>
  
  <p>2) When Party extends Model:</p>
  
  <ul>
  <li>Can do trivial things such as Party::count(), Party::where("Game_id", $game->id)</li>
  <li>Lose all pivot functions, so can't do $game->players, not even $game->players() work: $game->players()->count() is correct, but $game->players()->first() or $game->players->first() will break with the following:</li>
  </ul>
  
  <blockquote>
    <p>PHP error:  Argument 1 passed to Illuminate\Database\Eloquent\Model::__construct() must be of the type array, object given</p>
  </blockquote>
  
  <p>Question:</p>
  
  <p>How to correctly use the custom pivot model when instantiating them is different?</p>
  
  <p>I hope to achieve the following:</p>
  
  <ul>
  <li>Do the normal $events->players->attach($player)</li>
  <li>Be able to attach a score like $score->party_id = $events->players->find($x)->pivot->id (or any other way via a scope for example)</li>
  <li>Be able to count all parties being played, or the ones on one specific game or by a specific player (something like Party::count(), $player->parties->count() etc)</li>
  </ul>
  
  <p>Thank you.</p>