两个外键,如何用laravel雄辩地图

时间:2022-10-16 08:25:19

I have two tables in MySQL, where the first one is called users and the second one is called games. The table structure is as follows.

我在MySQL中有两个表,第一个叫做用户,第二个叫做游戏。表结构如下。

users

  • id (primary)
  • email
  • password
  • real_name

games

  • id (Primary)
  • user_one_id (foreign)
  • user_one_score
  • user_two_id (foreign)
  • user_two_score

My games table is holding two foreign relations to two users.

我的游戏桌与两个用户持有两个外交关系。

My question is how do I make the model relations for this table structure?? - According to the laravel documentation, I should make a function inside the model and bind it with its relations

我的问题是如何为这个表结构建立模型关系? - 根据laravel文档,我应该在模型中创建一个函数并将其与其关系绑定

for instance

public function users()
{
    $this->belongsTo('game');
}

however I can't seem to find anything in the documentation telling me how to deal with two foreign keys. like in my table structure above.

但是我似乎无法在文档中找到任何告诉我如何处理两个外键的内容。就像我上面的表结构一样。

I hope you can help me along the way here.

我希望你能在这里帮助我。

Thank you

2 个解决方案

#1


13  

A migration:

$table->integer('player1')->unsigned();
$table->foreign('player1')->references('id')->on('users')->onDelete('cascade');
$table->integer('player2')->unsigned();
$table->foreign('player2')->references('id')->on('users')->onDelete('cascade');

And a Model:

一个模型:

public function player1()
{
    $this->belongsTo('Game', 'player1');
}
public function player2()
{
    $this->belongsTo('Game', 'player2');
}

EDIT changed 'game' to 'Game' as user deczo suggested.

正如用户deczo建议的那样,EDIT将“游戏”改为“游戏”。

#2


0  

Unfortunately the way you have this setup is not likely to work in the current context. You may have more luck with the belongsTo method, but again that only supports one relationship.

不幸的是,这种设置的方式不太可能在当前上下文中工作。您可以使用belongsTo方法获得更多运气,但同样只支持一种关系。

You could implement a user1() belongsTo, a user2() belongsTo and finally just declare a non eloquent function to return both (something like $users = array($this->user1(), $this->user2())

你可以实现一个user1()belongsTo,一个user2()belongsTo,最后只是声明一个非雄辩的函数来返回两者(比如$ users = array($ this-> user1(),$ this-> user2())

#1


13  

A migration:

$table->integer('player1')->unsigned();
$table->foreign('player1')->references('id')->on('users')->onDelete('cascade');
$table->integer('player2')->unsigned();
$table->foreign('player2')->references('id')->on('users')->onDelete('cascade');

And a Model:

一个模型:

public function player1()
{
    $this->belongsTo('Game', 'player1');
}
public function player2()
{
    $this->belongsTo('Game', 'player2');
}

EDIT changed 'game' to 'Game' as user deczo suggested.

正如用户deczo建议的那样,EDIT将“游戏”改为“游戏”。

#2


0  

Unfortunately the way you have this setup is not likely to work in the current context. You may have more luck with the belongsTo method, but again that only supports one relationship.

不幸的是,这种设置的方式不太可能在当前上下文中工作。您可以使用belongsTo方法获得更多运气,但同样只支持一种关系。

You could implement a user1() belongsTo, a user2() belongsTo and finally just declare a non eloquent function to return both (something like $users = array($this->user1(), $this->user2())

你可以实现一个user1()belongsTo,一个user2()belongsTo,最后只是声明一个非雄辩的函数来返回两者(比如$ users = array($ this-> user1(),$ this-> user2())