lundi 20 février 2017

Laravel hasMany relationship, limit with query

Scenario: On a hasMany relationship, using the with function (for eager loading), I want to limit the results from each of the rows within (instead of the total).

I am following this tutorial (as I think it's the only way to achieve what I need from what I've read on SO) - http://ift.tt/29bicF2

I have a Model - Room that hasMany Reviews.

Room Model:

class Room extends Scopes\BaseModel

   public function reviews()
    {
        return $this->hasMany('App\Review');
    }

    public function latestReviews()
    {
        return $this->reviews()->latest()->nPerGroup('room_id', 1);
    }

Scopes/BaseModel.php:

Direct copy from the website's tutorial, but with the namespace of namespace App\Scopes;

Controller using the with function:

class Room extends Scopes\BaseModel

$rooms = Room::where('room_types_id', $specialism_id)
            ->with('latestReviews')
            ->get();

Error:

RelationNotFoundException in RelationNotFoundException.php line 20: Call to undefined relationship [latestReviews] on model [App\Room].



from Newest questions tagged laravel-5 - Stack Overflow http://ift.tt/2lelvz8
via IFTTT

Aucun commentaire:

Enregistrer un commentaire