PHP code example of darwinnatha / laravel-repository-pattern

1. Go to this page and download the library: Download darwinnatha/laravel-repository-pattern library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

darwinnatha / laravel-repository-pattern example snippets




namespace App\Repositories\Contracts;

use App\Models\Customer;
use Illuminate\Contracts\Pagination\Paginator;
use Illuminate\Database\Eloquent\Collection;
use Darwinnatha\LaravelRepositoryPattern\Contracts\Repository;

/**
 * @extends Repository<Customer>
 */
interface CustomerRepository extends Repository
{
    /**
     * Search for a customer by phone number.
     */
    public function wherePhoneNumber(string $phoneNumber): ?Customer;

    /**
     * Get a list of active verified customers.
     *
     * @return Collection<int, Customer>|Paginator<int, Customer>
     */
    public function getActiveVerifiedCustomers(): Collection|Paginator;
}



namespace App\Repositories\Eloquent;

use App\Models\Customer;
use App\Repositories\Contracts\CustomerRepository;
use Darwinnatha\LaravelRepositoryPattern\Repositories\BaseRepository;
use Illuminate\Database\Eloquent\Collection;

/**
 * @extends BaseRepository<Customer>
 */
final class CustomerRepositoryEloquent extends BaseRepository implements CustomerRepository
{
    protected string $model = Customer::class;

    public function wherePhoneNumber(string $phoneNumber): ?Customer
    {
        return $this->model::whereRelation('profile', 'phone_number', $phoneNumber)->first();
    }

    public function getActiveVerifiedCustomers(): Collection
    {
        return $this->buildQuery()
            ->where('is_active', true)
            ->whereNotNull('email_verified_at')
            ->get();
    }
}



namespace App\Repositories\Eloquent;

use App\Models\Customer;
use App\Repositories\Contracts\CustomerRepository;
use Darwinnatha\LaravelRepositoryPattern\Repositories\BaseRepository;
use Illuminate\Contracts\Pagination\Paginator;
use Illuminate\Database\Eloquent\Collection;

/**
 * @extends BaseRepository<Customer>
 */
final class CustomerRepositoryEloquent extends BaseRepository implements CustomerRepository
{
    protected string $model = Customer::class;

    public function wherePhoneNumber(string $phoneNumber): ?Customer
    {
        return $this->model::whereRelation('profile', 'phone_number', $phoneNumber)->first();
    }

    public function getActiveVerifiedCustomers(): Collection|Paginator
    {
        return $this->handleMaybePaginatedQuery(fn() => $this->buildQuery()
            ->where('is_active', true)
            ->whereNotNull('email_verified_at'),
            request()->query()
        );
    }
}



namespace App\Http\Controllers;

use App\Repositories\Contracts\CustomerRepository;
use Illuminate\Http\JsonResponse;

class CustomerController extends Controller
{
    public function __construct(
        private readonly CustomerRepository $customerRepository
    ) {}

    public function findByPhone(string $phone): JsonResponse
    {
        // Fluent call to the specific method declared in the interface
        $customer = $this->customerRepository->wherePhoneNumber($phone);

        return response()->json($customer);
    }
}



namespace App\Repositories\Eloquent;

use App\Models\Customer;
use App\Repositories\Contracts\CustomerRepository;
use Darwinnatha\LaravelRepositoryPattern\Repositories\BaseRepository;
use Spatie\QueryBuilder\AllowedFilter;

/**
 * @extends BaseRepository<Customer>
 */
final class CustomerRepositoryEloquent extends BaseRepository implements CustomerRepository
{
    protected string $model = Customer::class;
    
    protected string $defaultSort = '-id';

    public function __construct()
    {
        parent::__construct([
            'filters' => [
                AllowedFilter::scope('search'),
                AllowedFilter::partial('city', 'addresses.city'),
                AllowedFilter::partial('country', 'country.name'),
                AllowedFilter::exact('country_id'),
                AllowedFilter::exact('is_active'),
            ],
            '



public function index(Request $request): JsonResponse
{
    // Take the number of items per page from the query parameter, or default to 15
    $customers = $this->customerRepository->all([
        'paginate' => $request->query('paginate', 15),
    ]);

    return response()->json($customers);
}
bash
php artisan make:repository Customer