Laravel 5 eager loading with limit

前端 未结 6 1203
小蘑菇
小蘑菇 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 12:57

    Laravel Uses take() function not Limit

    Try the below Code i hope it's working fine for u

    Users::with(['action' => function ($query) {
          $query->orderBy('created_at', 'desc')->take(1);
    }])->get(); 
    

    or simply add a take method to your relationship like below

     return $this->hasMany('Action', 'user_id')->orderBy('created_at', 'desc')->take(1);
    

提交回复
热议问题