Home > Enterprise >  Laravel 8: Attempt to read property "id" on null
Laravel 8: Attempt to read property "id" on null

Time:11-09

I am facing this error 'Attempt to read property "id" on null' in laravel 8. It was working fine before but in my view I changed $user->id to $user->profile->id and now this is happening. I am logged in the app and I have changed my route accordingly to match profile id, I have also tried clearing cache etc.

Here is my Code:

User Model:

    class User extends Authenticatable
{
    use HasApiTokens, HasFactory, Notifiable;

    /**
     * The attributes that are mass assignable.
     *
     * @var string[]
     */
//    protected $fillable = [
//        'name',
//        'email',
//        'password',
//    ];

    protected $table = 'users';
    protected $guarded = [];

    /**
     * The attributes that should be hidden for serialization.
     *
     * @var array
     */
    protected $hidden = [
        'password',
        'remember_token',
    ];

    /**
     * The attributes that should be cast.
     *
     * @var array
     */
    protected $casts = [
        'email_verified_at' => 'datetime',
    ];

    public function setPasswordAttribute($password)
    {
        $this->attributes['password'] = bcrypt($password);
    }

    public function posts ()
    {
        return $this->hasMany(Post::class);
    }

    public function profile()
    {
        return $this->hasOne(Profile::class);
    }
}

Profile Model:

class Profile extends Model
{
    use HasFactory;

    protected $table = 'profiles';
    protected $guarded = [];

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

ProfilesController:

class ProfilesController extends Controller
{
    public function show(User $user)
    {
        return view ('profiles.index', compact('user'));
    }
    
    public function edit(User $user)
    {
        return view ('profiles.index', compact('user'));
    }
}

Route:

    Route::get('profile/{profile}', [ProfilesController::class, 'show'])->middleware('auth');
Route::get('profile/{profile}/edit', [ProfilesController::class, 'edit'])->middleware('auth');

View:

<x-layout>
<section class="py-8 max-w-4xl mx-auto">
    <h1 class="text-lg font-bold mb-8 pb-2 border-b">
        @if ($user->id == auth()->user()->id)
        Hello {{ $user->name }}, welcome to your profile.
        @else
        {{ $user->name }}'s Profile.
        @endif
    </h1>
    <div >
        <aside >
            <h4 >
                Navigation
            </h4>
            <ul style="max-width: 75%">
                <li>
                    <a href="/profile/{{ $user->profile->id }}" hljs-string">'.$user->profile->id) ? 'text-blue-500' : '' }}">View Profile</a>
                </li>
                <li>
                        @if ($user->id == auth()->user()->id)
                            <a href="/profile/{{ $user->profile->id }}/edit" hljs-string">'.$user->profile->id.'/edit') ? 'text-blue-500' : '' }}">Edit Profile</a>
                        @endif
                </li>
            </ul>
        </aside>
        <main >
            <x-panel>
                <div >
                    <div >
                        <div >
                            <div >
                                <div >
                                    <div >
                                        <div >
                                            <img src="http://i.pravatar.cc/60?u={{ $user->profile->id }}" alt="" width="" height="" >
                                        </div>
                                    </div>
                                    <div >
                                        <div >
                                            <div >
                                                <div >Name:</div>
                                                <div >Email:</div>
                                                <div >About:</div>
                                            </div>
                                            <div >
                                                <div >{{ $user->profile->name }}</div>
                                                <div >{{ $user->profile->email }}</div>
                                                <div ><p>{{ $user->profile->description }}</p></div>
                                            </div>
                                        </div>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
            </x-panel>
        </main>
    </div>
</section>

CodePudding user response:

I think the problem is that you are passing a Profile model as indicated by your routes, but the method is looking for a User model.

Show and Edit have a User Model as a parameter. If you pass the Profile id, the method is gonna find the User model by id but with the id of the Profile model and not the user_id of the Profile model.

You will need to change the methods to:

public function show(Profile $profile)
{
    $user = $profile->user;
    return view ('profiles.index', compact('user'));
}

public function edit(Profile $profile)
{
    $user = $profile->user;
    return view ('profiles.index', compact('user'));
}

With this code the Profile model is found and via the relationship the User is obtained and passed to the view.

  • Related