mercredi 17 juin 2020

Laravel hasone relationship

I am trying to use hasone relation but getting null error. below is my code

User Model:

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

Profile Model:

   function User()
{
    return $this->belongsTo('App\User');
}

Register Controller

 protected function create(array $data)
{
    $user =  User::create([
        'name' => $data['name'],
        'email' => $data['email'],
        'password' => Hash::make($data['password']),
    ]);

    if($user) {

        $profile = new Profile();
        $profile->name = 'Fake Name';
        $profile->father = 'Fake Father';
        $user->profile()->save($profile);
    }

    return $user;

}

Error: Call to a member function save() on null



from Newest questions tagged laravel-5 - Stack Overflow https://ift.tt/3e9eJ6P
via IFTTT

Aucun commentaire:

Enregistrer un commentaire