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

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

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

      1. <tfoot id='d3dRR'></tfoot>

        调用未定义的方法 IlluminateDatabaseQueryBuilder::asso

        时间:2023-09-23
          <legend id='ab8Kh'><style id='ab8Kh'><dir id='ab8Kh'><q id='ab8Kh'></q></dir></style></legend>

                <tbody id='ab8Kh'></tbody>
              <tfoot id='ab8Kh'></tfoot>

            1. <small id='ab8Kh'></small><noframes id='ab8Kh'>

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

                1. <i id='ab8Kh'><tr id='ab8Kh'><dt id='ab8Kh'><q id='ab8Kh'><span id='ab8Kh'><b id='ab8Kh'><form id='ab8Kh'><ins id='ab8Kh'></ins><ul id='ab8Kh'></ul><sub id='ab8Kh'></sub></form><legend id='ab8Kh'></legend><bdo id='ab8Kh'><pre id='ab8Kh'><center id='ab8Kh'></center></pre></bdo></b><th id='ab8Kh'></th></span></q></dt></tr></i><div id='ab8Kh'><tfoot id='ab8Kh'></tfoot><dl id='ab8Kh'><fieldset id='ab8Kh'></fieldset></dl></div>
                  本文介绍了调用未定义的方法 IlluminateDatabaseQueryBuilder::associate()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  参考:如何更新Laravel 4 中现有的 Eloquent 关系?

                  $userinfo = Userinfo::find($id);
                  User::find($id)->userinfo()->associate($userinfo)->save();
                  

                  我收到错误:Call to undefined method IlluminateDatabaseQueryBuilder::associate()

                  这里是整个方法:

                  public function saveUser($id)
                  {
                      $user = User::find($id);
                  
                      $userdata = Input::all();
                  
                      $rules = array(
                          'email' => 'required|email',
                          'state' => 'size:2',
                          'zip'   => 'size:5',
                          'phone' => array('regex:/^(?([0-9]{3}))?[-. ]?([0-9]{3})[-. ]?([0-9]{4})$/')
                      );
                  
                      $validator = Validator::make($userdata, $rules);
                  
                      if ($validator->passes())
                      {
                          if ($userdata['email'] !== $user->email)
                          {
                              $rules = array('email' => 'unique:users');
                              $validator = Validator::make($userdata, $rules);
                              if ($validator->fails()) return Redirect::route('admin.user.edit', array('user' => $user))
                                  ->with('error', 'Specified email already exists.');
                          }
                  
                          $user->email                = $userdata['email'];
                          $user->firstname            = $userdata['firstname'];
                          $user->lastname             = $userdata['lastname'];
                  
                          $userinfoArray = array(
                              'address'   => $userdata['address'],
                              'city'      => $userdata['city'],
                              'state'     => $userdata['state'],
                              'zip'       => $userdata['zip'],
                              'phone'     => preg_replace('/[^0-9]/', '', $userdata['phone'])
                          );
                  
                          $user->save();
                  
                          if (!$user->userinfo)
                          {
                              $userinfo = new Userinfo($userinfoArray);
                              $userinfo = $user->userinfo()->save($userinfo);
                          }
                          else
                          {
                              $userinfo = Userinfo::find($id);
                              User::find($id)->userinfo()->associate($userinfo)->save();
                              //$user->userinfo()->update($userinfoArray);
                          }
                  
                          return Redirect::route('admin.user.detail', array('id' => $id))
                              ->with('success', 'User updated.');
                      }
                  
                      return Redirect::route('admin.user.edit', array('id' => $id))
                          ->withInput()
                          ->withErrors($validator);
                  }
                  

                  推荐答案

                  associate() 是belongsTo 关系的一个方法,但从上面看来,您试图通过hasOne 关系调用它.

                  associate() is a method of the belongsTo relationship, but it looks like from the above you are trying to call it via the hasOne relationship.

                  我只是猜测,因为您没有提供您雄辩的模型类代码,所以无法看到您是如何准确设置关系的,但如果您有:

                  I am just guessing as you have not provided your eloquent model class code so can't see how you have set the relationships exactly, but if you have:

                  class User extends Eloquent {
                      public function userinfo()
                      {
                          return $this->hasOne('Userinfo');
                      }
                  }
                  
                  class Userinfo extends Eloquent {
                  
                      public function user() {
                          return $this->belongsTo('User');
                      }
                  }
                  

                  然后需要针对 Userinfo 调用关联,因为它具有关联 () 方法附加到的belongsTo 关系.

                  Then associate needs to be called against Userinfo as this has the belongsTo relationship to which the associate() method is attached.

                  例如

                  $user = User::find(4);      
                  $userinfo = UserInfo::find(1);
                  
                  $userinfo->user()->associate($user);
                  $userinfo->save();
                  

                  将user_info表中的外键user_id设置为$user对象的id.

                  Will set the foreign key user_id in the user_info table to the id of the $user object.

                  看看你上面的代码,这似乎不是你真正想要做的,

                  Looking at your above code it doesn't appear that this is what you are actually trying to do and that the

                  $user->userinfo()->update($userinfoArray);
                  

                  您注释掉的调用实际上会执行您似乎想要实现的目标,即更新与当前用户相关的用户信息(如果该用户已存在).

                  call which you have commented out will in fact do what you seem to be trying to achieve, which is to update the userinfo that is related to the current user if that user already exists.

                  希望这会有所帮助.

                  格伦

                  这篇关于调用未定义的方法 IlluminateDatabaseQueryBuilder::associate()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:获取所有属性的 Laravel 模型 下一篇:什么是 WhereHas Laravel 中的关系计数条件

                  相关文章

                  最新文章

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

                2. <small id='NS4l0'></small><noframes id='NS4l0'>

                  <tfoot id='NS4l0'></tfoot>

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