PHP code example of mrtolouei / laravel-repository

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

    

mrtolouei / laravel-repository example snippets


App\Models\User

namespace App\Repositories;

use App\Models\User;use MrTolouei\Repository\Repositories\BaseRepository;

class UserRepository extends BaseRepository
{
    public function __construct(User $model)
    {
        parent::__construct($model);
    }

    protected array $allowedFilters = [
        'email',
        'name',
        'age',
        'created_at',
    ];
}

$user = $repository->create([
    'email' => '[email protected]',
    'name' => 'John'
]);

$user = $repository->find($id);

$user = $repository->findOrFail($id);

$repository->update($id, [
    'name' => 'Updated Name'
]);

$repository->delete($id);

$repository->restore($id);

$repository->insert([
    ['email' => '[email protected]'],
    ['email' => '[email protected]']
]);

$repository->where('age', '>', 18)->all();

$repository
    ->where('age', 18)
    ->orWhere('age', 25)
    ->all();

$repository->whereIn('id', [1,2,3])->all();

$repository->whereNotIn('id', [4,5])->all();

$repository->orderBy('created_at', 'desc')->all();

$repository->limit(10)->all();

$repository->paginate(15);

$repository->count();

$repository->sum('amount');

$repository->avg('rating');

$repository->where('email', '[email protected]')->exists();

$repository->filter([
    'age' => ['gte' => 18],
    'email' => ['like' => 'gmail']
])->all();

public function index(Request $request)
{
    $repo = new UserRepository(new User());

    $users = $repo
        ->filter($request->query())  // apply filters
        ->orderBy('created_at', 'desc')
        ->paginate(15);

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

$repo->persistQuery()
    ->where('is_active', true)
    ->orderBy('created_at', 'desc');

$firstActive = $repo->first();
$activeUsers = $repo->all();

// Create
$newUser = $userRepo->create([
    'name' => 'Ali',
    'email' => '[email protected]',
]);

// Update
$updated = $userRepo->update($newUser->id, ['name' => 'Ali Tolouei']);

// Delete (soft)
$deletedCount = $userRepo->delete($newUser->id);

// Restore
$restoredCount = $userRepo->restore($newUser->id);

// Insert many
$userRepo->insert([
    ['name' => 'John', 'email' => '[email protected]'],
    ['name' => 'Jane', 'email' => '[email protected]'],
]);

// First or create
$user = $userRepo->firstOrCreate(['email' => '[email protected]'], ['name' => 'User']);

// Update or create
$user = $userRepo->updateOrCreate(['email' => '[email protected]'], ['name' => 'Updated']);

$users = $userRepo
    ->filter([
        'is_active' => true,
        'roles.id' => ['in' => [1, 5]],
        'or' => [
            ['created_at' => ['gte' => '2024-01-01']],
            ['name' => ['like' => 'Ali']],
        ],
    ])
    ->with(['profile', 'posts'])
    ->orderBy('created_at', 'desc')
    ->paginate(10);