<tfoot id='ZzAy7'></tfoot>

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

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

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

      1. <legend id='ZzAy7'><style id='ZzAy7'><dir id='ZzAy7'><q id='ZzAy7'></q></dir></style></legend>

        无法让 Laravel 助理工作

        时间:2023-09-23
        • <small id='WdwPA'></small><noframes id='WdwPA'>

        • <tfoot id='WdwPA'></tfoot>

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

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

                  本文介绍了无法让 Laravel 助理工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  我不太确定我是否理解 associate 方法在 Laravel 中.我理解这个想法,但我似乎无法让它发挥作用.

                  I'm not quite sure if I understand the associate method in Laravel. I understand the idea, but I can't seem to get it to work.

                  使用这个(蒸馏)代码:

                  With this (distilled) code:

                  class User
                  {
                  
                      public function customer()
                      {
                          return $this->hasOne('Customer');
                      }
                  
                  }
                  
                  class Customer
                  {
                  
                      public function user()
                      {
                          return $this->belongsTo('User');
                      }
                  
                  }
                  
                  $user = new User($data);
                  $customer = new Customer($customerData);
                  
                  $user->customer()->associate($customer);
                  

                  当我尝试运行它时,我收到一个 Call to undefined method IlluminateDatabaseQueryBuilder::associate().

                  I get a Call to undefined method IlluminateDatabaseQueryBuilder::associate() when I try to run this.

                  据我所知,我完全按照文档中的说明进行操作.

                  From what I can read, I do it exactly as is stated in the docs.

                  我做错了什么?

                  推荐答案

                  我不得不承认,当我第一次开始使用 Laravel 时,我必须始终如一地参考文档的部分关系,甚至在某些情况下我不太对劲.

                  I have to admit that when I first started using Laravel the relationships where the part that I had to consistently refer back to the docs for and even then in some cases I didn't quite get it right.

                  为了帮助您,associate() 用于更新 belongsTo() 关系.查看您的代码,从 $user->customer() 返回的类是一个 hasOne 关系类,上面没有关联方法.

                  To help you along, associate() is used to update a belongsTo() relationship. Looking at your code, the returned class from $user->customer() is a hasOne relationship class and will not have the associate method on it.

                  如果你反过来做.

                  $user = new User($data);
                  $customer = new Customer($customerData);
                  
                  $customer->user()->associate($user);
                  $customer->save();
                  

                  它会像 $customer->user()belongsTo 关系一样工作.

                  It would work as $customer->user() is a belongsTo relationship.

                  反过来,您需要先保存用户模型,然后将客户模型保存到其中,例如:

                  To do this the other way round you would first save the user model and then save the customer model to it like:

                  $user = new User($data);
                  $user->save();
                  
                  $customer = new Customer($customerData);
                  $user->customer()->save($customer);
                  

                  可能没有必要先保存用户模型,但我总是这样做,不知道为什么.

                  It may not be necessary to save the user model first but I've just always done that, not sure why.

                  这篇关于无法让 Laravel 助理工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:Laravel 不同计数 下一篇:Laravel 在 withCount 方法上使用 where 子句

                  相关文章

                  最新文章

                  <tfoot id='i7sVE'></tfoot>
                  <legend id='i7sVE'><style id='i7sVE'><dir id='i7sVE'><q id='i7sVE'></q></dir></style></legend>
                    <bdo id='i7sVE'></bdo><ul id='i7sVE'></ul>
                  1. <small id='i7sVE'></small><noframes id='i7sVE'>

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