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

Neeraj1005's avatar

Call to undefined relationship [booking_service] on model [App\BookingEvent].

Can anyone solve this problem What I wrong with these relationship? This is my BookingEvent Model

<?php

namespace App;

use Illuminate\Support\Str;
use Spatie\Sluggable\HasSlug;
use Spatie\Sluggable\SlugOptions;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
use Illuminate\Database\Eloquent\Factories\HasFactory;

class BookingEvent extends Model
{
    use SoftDeletes, HasFactory, HasSlug;
    /**
     * @var string
    */
    protected $table = 'booking_events';

    /**
     * @var string
    */
    protected $primaryKey = 'id';


    /**
     * @var array
    */
    protected $fillable = [
        'event_name',
        'user_id',
        'booking_service_id',
        'duration',
        'price',
        'event_start',
        'event_end',
        'event_description',
    ];

    /**
     * Get the event based on the booking services.
     */
    public function bookingService()
    {
        return $this->belongsTo( BookingService::class );
    }

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


}

And this is my BookingService Model

<?php

namespace App;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;

class BookingService extends Model
{
    use SoftDeletes, HasFactory;
    /**
     * @var string
    */
    protected $table = 'booking_services';

    /**
     * @var string
    */
    protected $primaryKey = 'id';

    /**
     * @var array
    */
    protected $fillable = ['service_name'];

    public function bookingevents() {

        return $this->hasMany( BookingEvent::class );

    }
}

and in index file

 @forelse($bookevents as $event)
                              <tr>
                                  <td>{{ $event->event_name ?? '' }}</td>
                                  <td>{{ $event->user->name ?? '' }}</td>
                                  <td>{{ $event->bookingService->service_name ?? '' }}</td>
@empty
.....................data...............
@endforelse
0 likes
1 reply
MichalOravec's avatar
Level 75

In BookService model it has to be bookingEvents and not bookingevents

public function bookingEvents()
{
    return $this->hasMany(BookingEvent::class);
}

Please or to participate in this conversation.