• <bdo id='00T6j'></bdo><ul id='00T6j'></ul>
    <legend id='00T6j'><style id='00T6j'><dir id='00T6j'><q id='00T6j'></q></dir></style></legend>

  • <small id='00T6j'></small><noframes id='00T6j'>

    <i id='00T6j'><tr id='00T6j'><dt id='00T6j'><q id='00T6j'><span id='00T6j'><b id='00T6j'><form id='00T6j'><ins id='00T6j'></ins><ul id='00T6j'></ul><sub id='00T6j'></sub></form><legend id='00T6j'></legend><bdo id='00T6j'><pre id='00T6j'><center id='00T6j'></center></pre></bdo></b><th id='00T6j'></th></span></q></dt></tr></i><div id='00T6j'><tfoot id='00T6j'></tfoot><dl id='00T6j'><fieldset id='00T6j'></fieldset></dl></div>

        <tfoot id='00T6j'></tfoot>

        两个外键,如何用laravel eloquent映射

        时间:2023-09-24
          • <bdo id='Px9Kb'></bdo><ul id='Px9Kb'></ul>
              <tbody id='Px9Kb'></tbody>
            <tfoot id='Px9Kb'></tfoot>

                  <small id='Px9Kb'></small><noframes id='Px9Kb'>

                  <legend id='Px9Kb'><style id='Px9Kb'><dir id='Px9Kb'><q id='Px9Kb'></q></dir></style></legend>

                  <i id='Px9Kb'><tr id='Px9Kb'><dt id='Px9Kb'><q id='Px9Kb'><span id='Px9Kb'><b id='Px9Kb'><form id='Px9Kb'><ins id='Px9Kb'></ins><ul id='Px9Kb'></ul><sub id='Px9Kb'></sub></form><legend id='Px9Kb'></legend><bdo id='Px9Kb'><pre id='Px9Kb'><center id='Px9Kb'></center></pre></bdo></b><th id='Px9Kb'></th></span></q></dt></tr></i><div id='Px9Kb'><tfoot id='Px9Kb'></tfoot><dl id='Px9Kb'><fieldset id='Px9Kb'></fieldset></dl></div>
                1. 本文介绍了两个外键,如何用laravel eloquent映射的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  我在 MySQL 中有两个表,其中第一个称为用户,第二个称为游戏.表结构如下.

                  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.

                  用户

                  • id(主要)
                  • 电子邮件
                  • 密码
                  • 真实姓名

                  游戏

                  • id(主要)
                  • user_one_id(国外)
                  • user_one_score
                  • user_two_id(国外)
                  • user_two_score

                  我的游戏桌有两个对外关系,两个用户.

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

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

                  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

                  例如

                  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.

                  谢谢

                  推荐答案

                  迁移:

                  $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');
                  

                  还有一个模型:

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

                  编辑按照用户 deczo 的建议将游戏"更改为游戏".

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

                  这篇关于两个外键,如何用laravel eloquent映射的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:手动将项目添加到现有对象 [Laravel 5] 下一篇:Laravel 5.5 对具有不同列名的单独表的唯一验证规

                  相关文章

                  最新文章

                  <legend id='NeeU0'><style id='NeeU0'><dir id='NeeU0'><q id='NeeU0'></q></dir></style></legend>
                2. <tfoot id='NeeU0'></tfoot>
                    <bdo id='NeeU0'></bdo><ul id='NeeU0'></ul>

                  1. <i id='NeeU0'><tr id='NeeU0'><dt id='NeeU0'><q id='NeeU0'><span id='NeeU0'><b id='NeeU0'><form id='NeeU0'><ins id='NeeU0'></ins><ul id='NeeU0'></ul><sub id='NeeU0'></sub></form><legend id='NeeU0'></legend><bdo id='NeeU0'><pre id='NeeU0'><center id='NeeU0'></center></pre></bdo></b><th id='NeeU0'></th></span></q></dt></tr></i><div id='NeeU0'><tfoot id='NeeU0'></tfoot><dl id='NeeU0'><fieldset id='NeeU0'></fieldset></dl></div>

                    <small id='NeeU0'></small><noframes id='NeeU0'>