rabol
246
2
General

How to add a user profile

Posted 2 years ago by rabol

Hi

For a new project I would like to add a 'User profile' to the user, however I do not want to add 20+ new fields to the users table

so i did the following: created a new migration (no foreign key constraint) created a new controller created a new model

modified the User model to create a new UserProfile when created Added a new function(relation) to my User model like this:

public function userProfile()
{
    $this->hasOne(UserProfile::class,'user_id','id');
}

then to test I did this in my home.blade.php

{{ auth()->user()->userProfile()->age }}

but... I get an exception:

Trying to get property of non-object (View: /Users/steenrabol/Sites/laravel/resources/views/home.blade.php)

What did I miss ?

Please sign in or create an account to participate in this conversation.