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

stylianospanagakos's avatar

Method addEagerConstraints does not exist.

I have a Booking model that has some attributes linked to it. I do an eager load for the attributes property in my BookingsController:

Eager load in BookingsController

class BookingsController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Database\Eloquent\Collection
     */
    public function index()
    {
        //
        return Booking::with(['attributes'])->get();
    }
//Method in Booking model
public function attributes() {
        return $this->belongsToMany(Attribute::class, 'gfw_bookings_attributes', 'booking', 'attribute')->withPivot('value');
    }

which returns the expected data. The only problem though is that the data is in the wrong format. So, I decided to use pluck to construct the new output:

// Modified method
public function attributes() {
        $collection = $this->belongsToMany(Attribute::class, 'gfw_bookings_attributes', 'booking', 'attribute')->withPivot('value');
        return $collection->pluck('value', 'name');
    }

but what I get now is the following error:

Method addEagerConstraints does not exist.

Any idea how to fix this issue?

0 likes
0 replies

Please or to participate in this conversation.