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
via Chebli Mohamed
Aucun commentaire:
Enregistrer un commentaire