Advertisement
Guest User

Untitled

a guest
Mar 28th, 2017
61
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. // Quote.php
  11. class Quote extends Model
  12. {
  13.     public function scopeApproved($q)
  14.     {
  15.         $q->where('approved', true);
  16.     }
  17. }
  18.  
  19. // Category.php
  20. class Category extends Model
  21. {
  22.     public function quote()
  23.     {
  24.         return $this->belongsTo(Quote::class);
  25.     }
  26.  
  27.     public function approvedQuote()
  28.     {
  29.         return $this->quote()->approved();
  30.     }
  31. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement