Laravel 5 eager loading with limit

前端 未结 6 1188
小蘑菇
小蘑菇 2020-12-09 12:28

I have two tables, say \"users\" and \"users_actions\", where \"users_actions\" has an hasMany relation with users:

users

id | name          


        
6条回答
  •  借酒劲吻你
    2020-12-09 13:15

    I think the solution you are asking for is explained here http://softonsofa.com/tweaking-eloquent-relations-how-to-get-latest-related-model/

    Define this relation in User model,

    public function latestAction()
    {
      return $this->hasOne('Action')->latest();
    }
    

    And get the results with

    User::with('latestAction')->get();
    

提交回复
热议问题