Support the ongoing development of Laravel.io →
Requests Database Views

Migration files

user table

<?php

use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class CreateUsersTable extends Migration
{
   
    public function up()
    {
        Schema::create('users', function (Blueprint $table) {
            $table->increments('id');
            $table->string('name');
            $table->string('username');
            $table->string('email')->unique();
            $table->string('password', 60);
            $table->rememberToken();
            $table->timestamps();
        });
    }

   
    public function down()
    {
        Schema::drop('users');
    }
}

Profile table


<?php

use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class CreateProfilesTable extends Migration
{
   
    public function up()
    {
        Schema::create('profiles', function (Blueprint $table) {
            $table->increments('id');
            $table->integer('user_id')->unsigned();
            $table->string('display_pic');
            $table->string('status');
            $table->text('bio');
            $table->string('facebook_url');
            $table->string('instagram_url');
            $table->string('youtube_url');
            $table->string('twitter_url');
            $table->timestamps();

            $table->foreign('user_id')->references('id')->on('users')->onDelete('cascade'); 
        });
    }

    
    public function down()
    {
        Schema::drop('profiles');
    }
}

User model


class User extends Model implements AuthenticatableContract, CanResetPasswordContract, SluggableInterface
{   

    use Authenticatable, CanResetPassword;

   
    protected $table = 'users';

    
    protected $fillable = ['name', 'email', 'password', 'username'];

  
    protected $hidden = ['password', 'remember_token'];

    use SluggableTrait;

    protected $sluggable = [
        'build_from' => 'name',
        'save_to'    => 'username',
    ];

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

    public function getDisplayName(){
        return $this->name;

    }
    
}

Profile model


<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Profile extends Model
{
    protected $table = 'profiles';

    protected $fillable = ['display_pic', 'status', 'bio', 'facebook_url', 'instagram_url', 'youtube_url','twitter_url'];

    protected $hidden = [];


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

    public function userStatus() {
        return $this->status;
    }

}

Now I added data in the profile table manually but I can't fetch it. doing $user->profile->getStatus isn't working. I can fetch anything from user table but unable to fetch it from profile table.

Last updated 2 years ago.
0

Try $user->profile->userStatus()

0

dd($user->profile);

results into

Profile {#188 ▼ #table: "posts" #fillable: array:3 [▼ 0 => "title" 1 => "post_pic" 2 => "post_story" ] #hidden: [] #connection: null #primaryKey: "id" #perPage: 15 +incrementing: true +timestamps: true #attributes: array:8 [▶] #original: array:8 [▶] #relations: [] #visible: [] #appends: [] #guarded: array:1 [▶] #dates: [] #dateFormat: null #casts: [] #touches: [] #observables: [] #with: [] #morphClass: null +exists: true +wasRecentlyCreated: false }

I can see there is no relationship set

Last updated 9 years ago.
0

RossTsachev said:

Try $user->profile->userStatus()

it was my tyop here. I still can't fetch data from profile table.

0
dd($user->profile);

There should be no relationship here. Status is not a relationship. It's a field in profile so you shouldn't even need to create a function for that. You can just access it by doing

dd($user->profile->status);
Last updated 9 years ago.
0

thomastkim said:

dd($user->profile);

There should be no relationship here. Status is not a relationship. It's a field in profile so you shouldn't even need to create a function for that. You can just access it by doing

dd($user->profile->status);

I understand it now. Thanks for explaining. anyway dd($user->profile->status) returns Null.

0

If you dd($user->profile), you said that you get this, correct?

Profile {#188#table: "posts" #fillable: array:3 [▼ 0 => "title" 1 => "post_pic" 2 => "post_story" ] #hidden: [] #connection: null #primaryKey: "id" #perPage: 15 +incrementing: true +timestamps: true #attributes: array:8 [▶] #original: array:8 [▶] #relations: [] #visible: [] #appends: [] #guarded: array:1 [▶] #dates: [] #dateFormat: null #casts: [] #touches: [] #observables: [] #with: [] #morphClass: null +exists: true +wasRecentlyCreated: false }

Can you open up attributes (click on it when you dd the output) and tell us what you see? Status should be listed under attributes.

0

This is totally weird. I just noticed that here in the dd output. table is "posts". It should be "profile" table. I deleted postController and post model and post routes to see if there was any conflict. But there isn't. It is still showing posts table.

0

Can you post the code on how you are retrieving these models?

0

I've already posted both models here. Do you want to see controller files?

0

Sign in to participate in this thread!

Eventy

Your banner here too?

apurva2342 apurva2342 Joined 17 Sep 2014

Moderators

We'd like to thank these amazing companies for supporting us

Your logo here?

Laravel.io

The Laravel portal for problem solving, knowledge sharing and community building.

© 2025 Laravel.io - All rights reserved.