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

unk's avatar
Level 1

Trying to get property 'name' of non-object

Why i have this error? For the doctor work but for user why not? https://imgur.com/m0lEwaz My code:

<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use App\Models\User;

class Booking extends Model
{
    protected $guarded = [];
    
    public function doctor()
    {
        return $this->belongsTo(User::class);
    }
    public function user()
    {
        return $this->belongsTo(User::class);
    }
    
}
<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Models\Booking;

class PatientlistController extends Controller
{
    public function index()
    {
        $bookings = Booking::latest()->get();
        return view('patientlist.index', compact('bookings'));
    }
}
@extends('layouts.app')

@section('content')
<div class="container">
    <div class="row justify-content-center">
        <div class="col-md-10">
        @if(Session::has('message'))
            <div class="alert bg-success alert-success text-white" role="alert">
                {{Session::get('message')}}
            </div>
        @endif
            <div class="card">
                <div class="card-header">Appointments({{$bookings->count()}})</div>

                <div class="card-body table-responsive">
                    <table class="table table-striped">
                      <thead>
                        <tr>
                          <th scope="col">#</th>
                          <th scope="col">Date</th>
                          <th scope="col">User</th>
                          <th scope="col">Time</th>
                          <th scope="col">Doctor</th>
                          <th scope="col">Status</th>
                        </tr>
                      </thead>
                      <tbody>
                        @forelse($bookings as $key=>$booking)
                        <tr>
                          <th scope="row">{{$key+1}}</th>
                          <td>{{$booking->date}}</td>
                          <td>{{$booking->user->name}}</td>
                          <td>{{$booking->time}}</td>
                          <td>{{$booking->doctor->name}}</td>
                          <td>
                              @if($booking->status==0)
                              <button class="btn btn-primary" style="white-space: nowrap;">Pending</button>
                              @else 
                              <button class="btn btn-success" style="white-space: nowrap;">Checked</button>
                              @endif
                          </td>
                        </tr>
                        @empty
                        <td>You have no any appointments</td>
                        @endforelse
                       
                      </tbody>
                    </table>
                </div>
            </div>
        </div>
    </div>
</div>
@endsection
0 likes
2 replies
MichalOravec's avatar
Level 75

At least one of bookings has no user.

<td>{{ $booking->user->name ?? 'No user' }}</td>
1 like

Please or to participate in this conversation.