mardi 7 mai 2019

Limiting laravel query using whereIn

so i have a query to make but i don`k know how best to it. I have 2 class like below:

User.class

class User {
    function posts(){
        return $this->hasMany(Post::class, 'user_id');
    }
}

Post.class

class Post {
    function user(){
        return $this->belongsTo(User::class, 'user_id');
    }
}

and in my controller i want to get posts per User with a limit for each user NOT for all results. So here is what i have this in my controller:

function getPosts(Request $request){
    $user_ids = [1,2,3,4];

    $posts = Post::whereIn('user_id', $user_ids)->latest()->take(10)->get();
    return $posts;
} 

So the above get will get me just 10 entries from all of them yet i want to get 10 for each user nomatter how many user IDs



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire