PHP code example of madulinux / repository-pattern

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

    

madulinux / repository-pattern example snippets


class UserRepository extends Repository implements UserRepositoryInterface
{
    public function __construct(User $model)
    {
        $this->model = $model;
    }
}

// Usage
$users = $repository->all();
$user = $repository->find(1);
$user = $repository->create(['name' => 'John']);
$repository->update(1, ['name' => 'Jane']);
$repository->delete(1);

// Enable/Disable cache
$repository->enableCache();
$repository->disableCache();

// Set cache time
$repository->setCacheTime(60); // 60 minutes

// Set cache tags (for Redis/Memcached)
$repository->setCacheTags(['users']);

// Custom cache key
$repository->setCacheKeyGenerator(function($method, $args, $model) {
    return "users:{$method}:" . md5(serialize($args));
});

$repository->on('creating', function($data) {
    // Before create
});

$repository->on('created', function($model) {
    // After create
});

// Available events:
// - creating/created
// - updating/updated
// - deleting/deleted

// Get with soft deleted records
$repository->withTrashed()->get();

// Get only soft deleted records
$repository->onlyTrashed()->get();

// Restore soft deleted records
$repository->restore($id);

// Force delete
$repository->forceDelete($id);

// Insert multiple
$repository->insert([
    ['name' => 'John'],
    ['name' => 'Jane']
]);

// Update multiple
$repository->bulkUpdate(
    ['status' => 'active'],
    ['department' => 'IT']
);

// Delete multiple
$repository->bulkDelete([1, 2, 3]);

// Upsert
$repository->upsert(
    [['id' => 1, 'name' => 'John']],
    'id',
    ['name']
);

// Chunk processing
$repository->chunk(100, function($users) {
    foreach($users as $user) {
        // Process each user
    }
});

// Using callback
$repository->transaction(function() use ($repository) {
    $user = $repository->create(['name' => 'John']);
    $profile = $repository->create(['user_id' => $user->id]);
});

// Manual control
$repository->beginTransaction();
try {
    $user = $repository->create(['name' => 'John']);
    $profile = $repository->create(['user_id' => $user->id]);
    $repository->commit();
} catch (\Exception $e) {
    $repository->rollBack();
    throw $e;
}

// Chainable methods
$repository
    ->with(['posts', 'comments'])
    ->filter(['status' => 'active'])
    ->orderBy('created_at', 'desc')
    ->paginate(15);

// Search
$users = $repository->search('john', ['name', 'email']);

// Advanced filtering
$users = $repository->getFiltered([
    'status' => 'active',
    'role' => ['admin', 'manager'],
    'age' => ['operator' => '>=', 'value' => 18]
], [
    'sort_by' => 'created_at',
    'sort_direction' => 'desc',
    'per_page' => 15
]);
bash
php artisan make:repository User