Home > other >  Laravel HasRelationships.php Error Occured
Laravel HasRelationships.php Error Occured

Time:10-19

One user has one profile. So i need to add record in here. I did , but I got following error

 $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);

Error

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

CodePudding user response:

You were not supposed to create a record like that. Try this:

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

or, after defining User -|---|- Profile relationship inside your User model, you can do it like this too:

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