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.
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
RossTsachev said:
Try $user->profile->userStatus()
it was my tyop here. I still can't fetch data from profile table.
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);
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.
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.
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.
I've already posted both models here. Do you want to see controller files?
Sign in to participate in this thread!
The Laravel portal for problem solving, knowledge sharing and community building.
The community