dimanche 26 novembre 2017

Laravel 5.5 How to get latest N record When using Many to Many releationships?

I want to get latest ten record from breaking news category. I searched here and web but any solutions doesn't work..

I have categories, posts and pivot tables.

Category Model:

 public function posts(){

    return $this->belongsToMany('App\Post');
}

Posts Model

 public function categories(){

        return $this->belongsToMany('App\Category');
    }

Controller

$breaking_news = Category::with(['posts' => function ($query) {
    $query->orderBy('id', 'desc')->take(10)->get();
}]);

return view('frontend.home',compact('breaking_news'));

Any advice ?



from Newest questions tagged laravel-5 - Stack Overflow http://ift.tt/2k1K1VO
via IFTTT

Aucun commentaire:

Enregistrer un commentaire