'Attempt to read property "alamat" on null - Laravel
hello I have 2 databases, namely users and profiles, profiles has a foreign key that is user_id. Then the relationship between the two is one to one.
Users migration
Schema::create('users', function (Blueprint $table) {
$table->id();
$table->string('name');
$table->string('email')->unique();
$table->timestamp('email_verified_at')->nullable();
$table->string('password');
$table->rememberToken();
$table->timestamps();
});
Profiles migration
Schema::create('profiles', function (Blueprint $table) {
$table->id();
$table->string('alamat')->nullable();
$table->string('nip')->nullable();
$table->string('jabatan')->nullable();
$table->unsignedBigInteger('user_id');
$table->foreign('user_id')->references('id')->on('users');
$table->timestamps();
});
User Model
use HasApiTokens, HasFactory, Notifiable;
protected $fillable = [
'name',
'email',
'password',
];
protected $hidden = [
'password',
'remember_token',
];
protected $casts = [
'email_verified_at' => 'datetime',
];
public function profile()
{
return $this->hasOne(Profile::class, 'user_id');
}
Profile Model
use HasFactory;
protected $table = 'profiles';
protected $guarded = [];
public function user()
{
return $this->belongsTo(User::class, 'user_id');
}
Profile Controller
public function render()
{
$userProfile = Profile::where('user_id', Auth::user()->id)->first();
if (!$userProfile) {
$profile = new Profile();
$profile->user_id = Auth::user()->id;
$profile->save();
}
$user = User::find(Auth::user()->id);
return view('profile.index', ['user' => $user]);
}
Profile index.blade.php
<x-app-layout>
<x-slot name="header">
<h2 class="font-semibold text-xl text-gray-800 leading-tight">
{{ __('Dashboard') }}
</h2>
</x-slot>
<div class="py-12">
<div class="max-w-7xl mx-auto sm:px-6 lg:px-8">
<div class="bg-white overflow-hidden shadow-sm sm:rounded-lg">
<div class="p-6 bg-white border-b border-gray-200">
Your Profile!
<div>
<p>Nama :{{ $user->name }}</p>
<p>Email : {{ $user->email }}</p>
<p>Alamat : {{ $user->profile->alamat }}</p>
</div>
</div>
</div>
</div>
</div>
the code above produces an error that is Attempt to read property "address" on null. so how do i solve it?
Solution 1:[1]
On your render method, you can do something like this:
if(!\Auth::check()){
abort(401);
}
$user = \Auth::user();
$user->load('profile');
return view('profile.index', ['user' => $user]);
The above code is just the optimization with eager loading. This should give you the profile address:
$user->profile->alamat
Three things you have to confirm while doing this:
- Is the user authenticated? If the user is not logged in you might not get data.
- Is the route wrap in
author similar middleware? If not, you might not getauthdata. - Is there data in
profilestable of the current user currently logged in?
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Saroj Shrestha |
