Advertisement
Guest User

Untitled

a guest
Mar 28th, 2017
49
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // QuoteController.php
  2. public function index()
  3. {
  4.     $quotes = Quote::approved()->orderBy('created_at', 'desc')->get();
  5.     $categories = Category::has('approvedQuotes')->orderBy('id')->withCount('approvedQuotes')->get();
  6.  
  7.     return view('category.index', compact('quotes', 'categories'));
  8.  
  9. }
  10.  
  11. // Quote.php
  12. class Quote extends Model
  13. {
  14.     public function scopeApproved($q)
  15.     {
  16.         $q->where('approved', true);
  17.     }
  18. }
  19.  
  20. // Category.php
  21. class Category extends Model
  22. {
  23.     public function quote()
  24.     {
  25.         return $this->belongsTo(Quote::class);
  26.     }
  27.  
  28.     public function approvedQuote()
  29.     {
  30.         return quote()->approved();
  31.     }
  32. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement