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

hamo_dev's avatar

Laravel Issue : Call to a member function addEagerConstraints() on string in file C:\xampp\htdocs\F5_Venuu\vendor\laravel\framework\src\Illuminate\Database\Eloquent\Builder.php

i am getting error:Call to a member function addEagerConstraints() on string in file C:\xampp\htdocs\F5_Venuu\vendor\laravel\framework\src\Illuminate\Database\Eloquent\Builder.php

Venue.php

public function reviews()
    {
        return $this->hasMany(Rating::class);
    }
    public function avgrate()
    {
        return $this->reviews()->avg('rating');
    }

Rating.php

  public function venue()
    {
        return $this->belongsTo(Venue::class);
    }

in my controller I am using

return Venue::with('avgrate')->get()

I want to get each Venue model entry with average rating. Kindly Help.

0 likes
4 replies
tisuchi's avatar

@hamo_dev Make sure that your tables have the right conventional relationship column name.

Otherwise set the column custom column name in the relationship.

For example-

public function reviews()
{
        return $this->hasMany(Rating::class, 'foreign_key', 'localKey');
}

The same goes for venue() relationships if the foreign key column name doesn't follow the convention.

Sinnbeck's avatar

with() is use to load relationships. This isnt a relationship.. its a string

 public function avgrate()
    {
        return $this->reviews()->avg('rating');
    }

Please or to participate in this conversation.