Récuperer 3 derniers posts pour chaque chaques categories

Par uknowcreation, il y a 10 ans


Bonjour me revoila,
Encore un soucis sur le retrieving data.
Le soucis et d'avoir pour chaque categories afficher les 2 posts associé à la category.
Voici mes models

//model POST public function category(){ return $this->belongsTo('Category'); } //model Category public function posts(){ return $this->hasMany('Post'); }

et voici mon controller

//show 4 categories with only one post by Category $categories = Category::with(['posts' => function($query){ $query->orderBy('published_at', 'DESC')->take(6)->get(); }])->take(4)->get();

Merci pour votre aide

Ceci me retourne 6 posts mais pas 2 posts par categories

1 réponse

arnich, il y a 10 ans

Peut-etre qu'en remplacant le 6 par un 2 il y a une chance qu'il recupère 2 post et non 6.