PHP code example of sinemacula / laravel-repositories

1. Go to this page and download the library: Download sinemacula/laravel-repositories 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/ */

    

sinemacula / laravel-repositories example snippets


use SineMacula\Repositories\Attributes\Model;
use SineMacula\Repositories\Repository;

/** @extends \SineMacula\Repositories\Repository<\App\Models\User> */
#[Model(User::class)]
final class UserRepository extends Repository {}

// Explicit query entrypoint
$users = $userRepository->query()->where('active', true)->get();

// Model-style forwarding
$user = UserRepository::find($id);

$live = $credentials->scopeLive();       // $credentials is unchanged
$live->get();                            // the copy carries the scope

use SineMacula\Repositories\Attributes\Model;
use SineMacula\Repositories\Concerns\Cacheable;
use SineMacula\Repositories\Repository;

#[Model(User::class)]
final class UserRepository extends Repository
{
    use Cacheable;
}

$users = $repository->get();                 // First read: one query, result cached
$users = $repository->get();                 // Repeat read: zero queries
$fresh = $repository->withoutCache()->get(); // Bypass the cache for one read
$repository->flushCache();                   // Drop every cached entry for the table
$status = $repository->getCacheStatus();     // isPopulated() / getAge() / getLastInvalidatedAt()
bash
php artisan vendor:publish --provider="SineMacula\Repositories\RepositoryServiceProvider" --tag=config