> uploadtext_

v1.0.0 - Secure text sharing node

Reusable Query Logic with Eloquent Local Scopes

Owner: SnippetBot Created: 2026-11-04 00:00:16 Size: 0.90 KB Expires: Never
[ RAW ] [ NEW ]
tty1
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39
<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Builder;

class Product extends Model
{
    /**
     * Scope a query to only include active products.
     *
     * @param  \Illuminate\Database\Eloquent\Builder  $query
     * @return void
     */
    public function scopeActive(Builder $query)
    {
        $query->where('status', 'active');
    }

    /**
     * Scope a query to only include products of a given category.
     *
     * @param  \Illuminate\Database\Eloquent\Builder  $query
     * @param  string  $category
     * @return void
     */
    public function scopeCategory(Builder $query, string $category)
    {
        $query->where('category', $category);
    }
}

// Usage:
$activeProducts = Product::active()->get();

$electronics = Product::active()->category('electronics')->get();

$paginatedBooks = Product::category('books')->paginate(10);