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

rei's avatar
Level 1

Property [client] does not exist on this collection instance

I can't get the value of the foreign key that is inside the booking table.

Booking Table

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class booking extends Model
{
    protected $primaryKey = 'bookingID';
   protected $fillable = ['clientID', 'checkInDate', 'checkOutDate', 'roomsCount', 'roomTypeID', 'adultsCount', 'childrenCount', 'amenityID', 'paymentID'];

    public function client()
    {
        return $this->hasOne(Client::class,'clientID');
    }

}

Client Table

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class client extends Model
{
  protected $primaryKey = 'clientID';
   protected $fillable = ['fullNmae', 'firstName', 'lastName', 'phoneNumber', 'emailAddress'];
  public function booking()
  {
      return $this->hasMany(Booking::class);
  }
}

I tried adding the protected $primaryKey = 'bookingID'; and protected $primaryKey = 'clientID'; as suggested in my previous question but now I still can't get just the FirstName from the client table.

$bookingDetail = booking::with('client')->get();
    return  $bookingDetail->client->firstName;
0 likes
2 replies
Sykr's avatar

booking::with('client')->get() This query returns Collection of booking. So you can change: booking::with('client')->first() or use foreach() You need fetch only one, if you want get property from relation.

Snapey's avatar
Snapey
Best Answer
Level 122

because you dont use conventions for primary key, you must always mention the key name in your relationships

The probem here though is that if a model contains the foreign key then it should be a belongsTo relationship, not hasOne

Please or to participate in this conversation.