Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // QuoteController.php
- public function index()
- {
- $quotes = Quote::approved()->orderBy('created_at', 'desc')->get();
- $categories = Category::has('approvedQuotes')->orderBy('id')->withCount('approvedQuotes')->get();
- return view('category.index', compact('quotes', 'categories'));
- }
- // Quote.php
- class Quote extends Model
- {
- public function scopeApproved($q)
- {
- $q->where('approved', true);
- }
- }
- // Category.php
- class Category extends Model
- {
- public function quote()
- {
- return $this->belongsTo(Quote::class);
- }
- public function approvedQuote()
- {
- return $this->quote()->approved();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement