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
{
public function games() {
return $this->belongsToMany(Game::class)->withPivot('id')->withTimestamps();
}
public function parties() {
return $this->hasManyThrough(Score::class, Party::class);
}
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
{
public function players() {
return $this->belongsToMany(Player::class)->withPivot('id')->withTimestamps();
}
public function parties() {
return $this->hasManyThrough(Score::class, Party::class);
}
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';
public function player() {
return $this->belongsTo(Player::class);
}
public function event() {
return $this->belongsTo(Game::class);
}
public function scores()
{
return $this->hasMany(Score::class);
}
}
Rating
class Score extends Model
{
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() ..)
.