【问题标题】:Creating an Eloquent Object with relation included创建包含关系的 Eloquent 对象
【发布时间】:2015-08-27 14:36:21
【问题描述】:

我对 opps 和 laravel 都很陌生 因此,要将值插入到我的 usersprofiles 表中,这些表有 OneToOne 关系,这是我的 store() 方法的样子

public function store(Requests\StoreNewUser $request)
{
    // crate an objct of user model
        $user = new \App\User;
        // now request and assign validated input to array of column names in user table
        $user->first_name = $request->input('first_name');
        $user->last_name = $request->input('last_name');
        $user->email = $request->input('email');
        $user->password = $request->input('password');
        /* want to assign request input to profile table's columns in one go 
        */
        $user->profile()->user_id = $user->id; // foreign key in profiles table
        $user->profile()->mobile_no = $request->input('mobile'); 
        dd($user); // nothing related to profile is returned
}

我正在创建新记录,因此dd() 永远不会返回与配置文件表相关的任何内容。

这是因为$user 对象默认不包含关系吗? 如果是,我可以创建包含User 模型中关联关系的$user 对象吗?

或者我必须为每个表和save() 数据创建两个单独的对象
但是push() 方法的意义是什么?

编辑 1 附言是的,这些关系已经在User & Profile 模型中定义了

【问题讨论】:

    标签: php laravel eloquent laravel-5 foreign-key-relationship


    【解决方案1】:

    您可以尝试以下方法。首先像这样保存父模型:

    $user = new \App\User;
    $user->first_name = $request->input('first_name');
    // ...
    
    $user->save();
    

    然后使用以下方法创建并保存相关模型:

    $profile = new \App\Profile(['mobile_no' => $request->input('mobile')]);
    $user->profile()->save($profile);
    

    还要确保您已在 User 模型中创建了 profile 方法:

    public function profile()
    {
        return $this->hasOne('App\Profile');
    }
    

    【讨论】:

    • 是的,该方法确实已经存在于关系中。也就是说,我必须分别从UserProfile 创建两个对象,但$user->profile()->save($profile); 究竟意味着什么?与$profile->save() 有何不同
    • 它还将profileUser 模型附加在一起,因此您在手动创建Profile 时不必提供user.id(foreign key)
    • 完成。以及如何一次性保存数据,即如果多个用户同时在服务器上由多个单独的请求创建,它们会在集合中执行吗?而不是混在一起?您可能会考虑,我有一个创建用户表单,并且我的一些员工操作员正在同时提交此表单。
    • 是的,它们将被保存而不会发生任何冲突。
    • 后续问题是白名单 ($fillable[]) mobile_noMassAssignment 的其他非易失性信息字段好吗?我喜欢你的方式 cz 我很懒 :-D
    【解决方案2】:

    我想我会更新这个答案并使其适用于 Laravel 5 及以后的版本。我将使用@The Alpha 答案作为基础。

    $profile = new \App\Profile(['mobile_no' => $request->input('mobile')]);
    $user->profile()->associate($profile); // You can no longer call 'save' here
    $user->profile()->save();
    

    原因是您不能再在belongsTo 关系(或任何其他)上调用save,这现在返回Illuminate\Database\Query\Builder 的实例。

    【讨论】:

      【解决方案3】:

      现在最干净的方法是在您的用户类文件中添加:

      public function profile()
      {
          return $this->hasOne(App\Profile::class);
      }
      

      在您的用户控制器中,使用以下存储方法:

      public function store(Requests\StoreNewUser $request)
      {
          $user = App\User::create(
              $request->only(
                  [
                      'first_name',
                      'last_name',
                      'email'
                  ]
              )
          );
      
          $user->password = Illuminate\Support\Facades\Hash::make($request->password);
          //or $user->password = bcrypt($request->password);
      
          $user->profile()->create(
              [
                  'mobile_no' =>  $request->mobile;
              ]
          );
      
          dd($user);
      }
      

      我不知道你是把纯文本密码保存到你的数据库还是在密码属性上使用了一个mutator,无论如何我认为上面的建议是一个好习惯

      【讨论】:

        【解决方案4】:

        这是因为 $user 对象默认不包含关系吗?如果是,我可以创建包含用户模型中关联关系的 $user 对象吗?

        是的,您应该创建关系,默认不包含它们。

        在您的 User 模型中,您希望执行以下操作:

        public function profile()
        {
            return $this->hasOne('App\Profile'); // or whatever your namespace is
        }
        

        这还需要您创建一个Profile 模型。

        这肯定会回答您有关插入相关模型的问题:http://laravel.com/docs/5.1/eloquent-relationships#inserting-related-models

        正如 The Alpha 提到的,你也没有提到,我认为你需要先保存你的用户模型,然后你可以通过关系添加。

        【讨论】:

        • 已经有了,我忘了提,hasOnebelongsTo 分别在 UserProfile 模型中定义
        猜你喜欢
        • 2014-07-16
        • 2016-03-25
        • 1970-01-01
        • 2022-01-09
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-07-18
        • 1970-01-01
        相关资源
        最近更新 更多