Laravel 5 eager loading with limit

前端 未结 6 1202
小蘑菇
小蘑菇 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:04

    Let change a bit the @berkayk's code.

    Define this relation in Users model,

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

    And

    Users::with(['latestAction' => function ($query) {
        $query->where('id_action', 1);
    }])->get();
    

提交回复
热议问题