<tfoot id='MKcA8'></tfoot>

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

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

      <i id='MKcA8'><tr id='MKcA8'><dt id='MKcA8'><q id='MKcA8'><span id='MKcA8'><b id='MKcA8'><form id='MKcA8'><ins id='MKcA8'></ins><ul id='MKcA8'></ul><sub id='MKcA8'></sub></form><legend id='MKcA8'></legend><bdo id='MKcA8'><pre id='MKcA8'><center id='MKcA8'></center></pre></bdo></b><th id='MKcA8'></th></span></q></dt></tr></i><div id='MKcA8'><tfoot id='MKcA8'></tfoot><dl id='MKcA8'><fieldset id='MKcA8'></fieldset></dl></div>
      • <bdo id='MKcA8'></bdo><ul id='MKcA8'></ul>
      1. 一次创建新的记录和关系

        时间:2023-09-22
          <tbody id='MH75I'></tbody>

                <bdo id='MH75I'></bdo><ul id='MH75I'></ul>

              • <tfoot id='MH75I'></tfoot>
              • <i id='MH75I'><tr id='MH75I'><dt id='MH75I'><q id='MH75I'><span id='MH75I'><b id='MH75I'><form id='MH75I'><ins id='MH75I'></ins><ul id='MH75I'></ul><sub id='MH75I'></sub></form><legend id='MH75I'></legend><bdo id='MH75I'><pre id='MH75I'><center id='MH75I'></center></pre></bdo></b><th id='MH75I'></th></span></q></dt></tr></i><div id='MH75I'><tfoot id='MH75I'></tfoot><dl id='MH75I'><fieldset id='MH75I'></fieldset></dl></div>
                  <legend id='MH75I'><style id='MH75I'><dir id='MH75I'><q id='MH75I'></q></dir></style></legend>
                • <small id='MH75I'></small><noframes id='MH75I'>

                  本文介绍了一次创建新的记录和关系的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  我有以下基本架构:

                  玩家

                  id
                  name
                  

                  个人资料

                  id
                  player_id
                  email
                  

                  子集

                  id
                  profile_id
                  alias
                  

                  我的印象是在创建新记录时可以进行以下操作:

                  I was under the impression the following operation was possible when creating a new record:

                  Player::create([
                      'name' => 'Player 1',
                      'profile.email' => 'player1@email.com',
                      'profile.subset.alias' => 'Player 1 alias'
                  ]);
                  

                  既然这段代码好像不行,那么有没有办法把关系记录和create方法一起保存?

                  Since this code doesn't seem to work, is there anyway to save relationships records together with the create method?

                  推荐答案

                  基本上,您不能像看起来那么容易做到这一点.

                  Basically, you can't do this as easy as it looks.

                  在文档中,所有相关模型都是在创建基础模型之后创建的

                  In the docs, all related models are created after the base model is created

                  $profile = new Profile(array('email' => 'player1@email.com','alias'=>'Player 1 alias'));
                  
                  $player = new Player(array('name'=>'Player 1'));
                  
                  $player = $post->profile()->save($profile);
                  

                  但是,如果你真的想一口气搞定,你可以覆盖Player模型中的save()方法:

                  However , if you really want to do it in one go, you can overwrite the save() method in the Player model :

                  public function save(){
                          Database::transaction(function() {
                                      $profileModel = new Profile($this->profile);
                  
                                      parent::save();
                  
                                      $this->profile()->insert($profileModel);
                  
                                  });
                  }
                  

                  然后您将数组传递给 Player 方法,例如:

                  You will then pass the array to the Player method like :

                  array(
                  name='Player name',
                  profile=>array(
                           email=>'player1@email.com',
                           subset=>array(
                             alias=>'Player 1 alias'
                            )
                  );
                  

                  虽然这不是推荐的操作.

                  Although this is not a recommended action.

                  请阅读有关如何保存 Eloquent 模型和关系的更多信息:

                  Please read more about how to save the Eloquent models and relationships :

                  教程 1教程 2

                  任何地方都建议先创建基础模型,再创建相关模型.

                  Everywhere is suggested to create the base model first, then the related models.

                  这篇关于一次创建新的记录和关系的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:Laravel Eloquent ORM 返回 stdClass 而不是实际模型 下一篇:当同一模型也存在 HasMany 关系时,如何更新 Has

                  相关文章

                  最新文章

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

                    • <bdo id='MNE3p'></bdo><ul id='MNE3p'></ul>

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

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