Laravel HasRelationships.php 发生错误

Laravel HasRelationships.php Error Occured

一个用户有一个个人资料。所以我需要在这里添加记录。我做了,但是我得到了以下错误

 $user = User::find(20);

        $profile = new Profile();
        $profile->first_name = $data['firstName'];
        $profile->last_name = $data['lastName'];
        $profile->dob = $data['dob'];
        $profile->gender =  $data['gender'];
        $profile->contact_no =$data['contactNo'];
        $user->profile()->save($profile);

错误

Too few arguments to function Illuminate\Database\Eloquent\Relations\HasOneOrMany::__construct(), 0 passed in D:\Project Repo\Interviews\cmg\laravel_member_mis\vendor\laravel\framework\src\Illuminate\Database\Eloquent\Concerns\HasRelationships.php on line 745 and exactly 4 expected

你不应该创建这样的记录。试试这个:

$profile = Profile::create([
     // Fill your colums here, like so
     // 'user_id' => $userId, ...
);

或者,在 User 模型中定义 User -|----|- Profile 关系后,您也可以这样做:

$profile = $user->profile()->create([
     // Fill your colums here, like so
     // 'user_id' => $userId, ...
);