View difference between Paste ID: pcWVnsYF and AvRV9k9s
SHOW: | | - or go back to the newest paste.
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('quotes')->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-
    public function category()
19+
20
// Category.php
21-
        return $this->belongsTo(Category::class);
21+
22
{
23
    public function quote()
24
    {
25
        return $this->belongsTo(Quote::class);
26
    }
27
28-
    public function category()
28+
    public function approvedQuote()
29
    {
30-
        return $this->belongsTo('App\Category');
30+
        return quote()->approved();
31
    }
32
}