php – Laravel自定义数据透视用法(扩展模型或扩展数据透视表)
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了php – Laravel自定义数据透视用法(扩展模型或扩展数据透视表),小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含4155字,纯文字阅读大概需要6分钟。
内容图文
![php – Laravel自定义数据透视用法(扩展模型或扩展数据透视表)](/upload/InfoBanner/zyjiaocheng/767/841be9c0f59f4a239ac926cd7277045f.jpg)
我真的不习惯使用我的支点的自定义模型,并且不明白为什么我们可以为它制作自定义模型,如果我们不能将它用作模型.
我已经看过很多答案和文章,所有人都选择扩展Pivot或Model,但是现实生活中的用法从未被考虑过,而且从来没有比给它一个“命名”类更进一步.
这是我想要实现的一个例子:
我的主要型号:
球员模型,与球员表.
游戏模型,带游戏桌.
玩家和游戏有多对多关系,我们可以用“game_player”表称为“Party”.
现在不那么重要了,我还有一个得分/转弯的分数模型,一个Party可以有很多分数,所以得分表有一个party_id条目.
所以,这里基本上是我的课程(Laravel 5.2):
播放机
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);
}
}
游戏
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);
}
}
派对
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);
}
}
得分了
class Score extends Model
{
// The Party this Score belongs to (has "party_id" column).
public function party() {
return $this->belongsTo(Party::class);
}
}
现在,我有两组不同的问题,取决于我是否在Party上扩展Model或Pivot:
1)当Party扩展Pivot时:
>不能做类似Party :: count()的事情,任何适用于模型的东西.
>不能做像Party :: where(“player_id”,$player-> id)之类的东西……基本上我希望派对上有一个得分/转牌表并且这样做我需要选择枢轴模型.
>不能做类似$events-> player-> first() – >得分()或事件$events-> player-> first() – > pivot-> scores(),它突破了以下内容:
PHP error: Argument 1 passed to Illuminate\Database\Eloquent\Relations\Pivot::__construct() must be an instance of Illuminate\Database\Eloquent\Model
2)当Party扩展模型时:
>可以做一些琐碎的事情,比如Party :: count(),Party :: where(“Game_id”,$game-> id)
>失去所有的枢轴功能,所以不能做$game->玩家,甚至不是$game-> player()工作:$game-> players() – > count()是正确的,但$game – > players() – > first()或$game-> players-> first()将突破以下内容:
PHP error: Argument 1 passed to Illuminate\Database\Eloquent\Model::__construct() must be of the type array, object given
题:
如何在实例化时正确使用自定义枢轴模型是不同的?
我希望实现以下目标:
>做正常的$events->球员 – >附加($player)
>能够附加分数,如$score-> party_id = $events-> players-> find($x) – > pivot-> id(或通过范围的任何其他方式)
>能够计算正在玩的所有方,或某个特定游戏或特定玩家的方(如Party :: count(),$player-> parties-> count()等)
谢谢.
解决方法:
对于我对laravel的一点体验,我发现当你有多对多关系时只使用表A中的特定对象与表B之间保持一个链接时,就会使用数据透视表.例如,带角色的用户.
由于您可以成像,因此用户A只需要与角色B建立一个链接/关系.
但是,如果我们想要使用需要在userA和productB之间保留不同记录的数据透视表(例如Sales),会发生什么?我没有找到直接的方法来做到这一点.
我的解决方案是将intermadiate表视为一个实际模型,然后与两个表(用户 – 销售和产品 – 销售)建立一对多的关系,然后,如果我需要,将这个中间模型与另一个模型联系起来.
所以在你的情况下,我有4个明确的模型:
>播放器|与党一对多
>游戏|与党一对多
>派对|与分数一对一|与党多对一|与玩家多对一
>得分|与党一对一
使用这种方法,您还可以更好地控制每个模型的属性.
内容总结
以上是互联网集市为您收集整理的php – Laravel自定义数据透视用法(扩展模型或扩展数据透视表)全部内容,希望文章能够帮你解决php – Laravel自定义数据透视用法(扩展模型或扩展数据透视表)所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。