Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

niho's avatar
Level 2

Link profile to user

Hi

I am currently working on creating a user table/model to a user however because i have been following laravel 4 tutorials i have become stuck. (i have only just started using laravel recently)

below is my code


class CreateProfilesTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        //
        Schema::Create('profiles', function(Blueprint $table) {
           $table->integer('user_id')->unsigned()->nullable();
           $table->foreign('user_id')->references('id')->on('users');
            $table->date('birthday');
            $table->string('image');
            $table->string('housenumber')->nullable();
            $table->string('addressline1')->nullable();
            $table->string('addressline2')->nullable();
            $table->string('postcode')->nullable();
            $table->string('county')->nullable();
            $table->string('country')->nullable();
            $table->timestamps();

         });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        //
        Schema::drop('profiles');
    }
}

routes.php

//Load User Profile
Route::get('dashboard/profile', 'ProfilesController@show');

profilesController.php

 public function show($id)
    {
        //
        $user = User::whereId($id)->
        return view('dashboard.profiles.show')->with(['user' => $user]);
       // dd($user->toArray());
    }

user.php
 public function profile()
    {

        return $this->hasOne('Profile');
    }

profile.php

public function user()
    {
        return $this->belongsTo('User');
    }
}

user.php

public function profile()
    {

        return $this->hasOne('Profile');
    }

how do i go about grabbing the profile for a particular used id and displaying it in the a view? Basically I am not sure what to do when in the pages controller to show a profile for a particular user or if i have even set it up correctly.

Any help or guidance appreciated

Thanks

0 likes
8 replies
bobbybouwmann's avatar

You can get the user profile like so

public function show($id)
{
    $user = User::with('profile')->findOrFail($id);

    return view('dashboard.profiles.show', compact('user'));
}

In your view you can then do this

{{ $user->name }}
{{ $user->profile->age }}
{{ $user->profile->gender }}
3 likes
jbowman99's avatar

@pushmyprofile

My show function for the same kind of set up looks like this:

public function show($user_id)
{
    $user= User::find(1);
    $user_profile = Profile::info($user_id)->first();
    return view('profiles.show', compact('profile' , 'user'));
}

hope that helps a tad

Erik's avatar
$user = User::with('profile')->find($id);

And in your view

$user->profile->birthday;
niho's avatar
Level 2

Thanks for the quick responses! Much appreciated!

I seem to get the same issue as before which makes me think I am doing something wrong elsewhere.

Missing argument 1 for App\Http\Controllers\ProfilesController::show()?

It is looking for the $id parameter but cant find it?

bobbybouwmann's avatar

You need to specify that in your url in the browser. So when you have a route like this

Route::get('profile/{id}', 'ProfilesController@show');

you need an url like this

// Where 1 is the user id of the user.
http://example.com/profile/1

You can make this much easier by giving an optional id

Route::get('profile/{id?}', 'ProfilesController@show');

You can then do this in your show function

public function show($id = null)
{
    $userId = $id ?: auth()->user()->id;
    
    $user = User::with('profile')->findOrFail($userId);

    // return the the view with the user
}
1 like
niho's avatar
Level 2

Ok Thanks

Now i just get ''' FatalErrorException in Model.php line 770: Class 'Profile' not found '''

On my research this is related to the namespaces but i am not really sure what should be referenced.

niho's avatar
Level 2

I have changed Profile.php

class Profile extends Model
{
    //
   public function user()
    {
        return $this->belongsTo(User');
    }
}

To this which seems to have don the trick

class Profile extends Model
{
    //
   public function user()
    {
        return $this->belongsTo'('App\User');
    }
}
nabeel_alalmai's avatar

Why would you create separate table for user profile? I think the relation will be one to one and data are relevant to user.

I think adding more fields to user table is better as long as the profile will contain avatar, dob, gender, ...

Please or to participate in this conversation.