PHP code example of gohari / repository-pattern

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

    

gohari / repository-pattern example snippets


Gohari\RepositoryPattern\RepositoryPatternServiceProvider::class

'providers' => [
    Gohari\RepositoryPattern\RepositoryPatternServiceProvider::class,
],



namespace App\Repositories;

use Gohari\RepositoryPattern\BaseRepository;
use App\Models\User;
use App\Repositories\Contracts\UserRepositoryInterface;

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



namespace App\Repositories\Contracts;

use Gohari\RepositoryPattern\BaseRepositoryInterface;

interface UserRepositoryInterface extends BaseRepositoryInterface
{
    //
}

$repository->query();
$repository->getAll();
$repository->paginate(perPage: 15, page: 2);
$repository->bigDataPaginate(perPage: 100, cursor: $cursor);
$repository->findById($id);
$repository->findOrFail($id);
$repository->firstWhere('email', '[email protected]');
$repository->findBy('email', '[email protected]');
$repository->exists($id);
$repository->count();
$repository->create($data);
$repository->updateOrCreate(['email' => $email], $data);
$repository->update($id, $data);
$repository->delete($id);
$repository->deleteMany([$firstId, $secondId]);
$repository->search('email', '[email protected]', '=');
$repository->searchByColumn('name', 'john');
$repository->with(['roles', 'profile'])->paginate();
$repository->withRelations(['roles', 'profile'])->get();
$repository->sortBy('created_at', 'desc')->get();
$repository->withTrashed()->getAll();
$repository->onlyTrashed()->count();
$repository->restore($id);
$repository->forceDelete($id);

$repository->insertData($data);
$repository->updateItem($id, $data);
$repository->deleteData($id);

$users->paginate(['roles'], 'created_at', 'desc', 20, 2);
$users->bigDataPaginate(['roles'], 'id', 'asc', 100, $cursor);
$users->find($id, ['profile']);
$users->create(UserData::fromArray($data));
$users->restore($id);
$users->forceDelete($id);

$users = $userService->bigDataPaginate(
    relations: ['roles'],
    sortBy: 'id',
    sortDirection: 'asc',
    perPage: 100,
    cursor: request()->query('cursor')
);

class User extends Model
{
    protected $fillable = ['name', 'age', 'is_active', 'settings'];

    protected $casts = [
        'age' => 'integer',
        'is_active' => 'boolean',
        'settings' => 'array',
    ];
}

public function register(): void
{
    $this->app->bind(
        \App\Repositories\Contracts\UserRepositoryInterface::class,
        \App\Repositories\UserRepository::class
    );
}

return [
    'paths' => [
        'repositories' => app_path('Repositories'),
        'interfaces' => app_path('Repositories/Contracts'),
        'services' => app_path('Services'),
    ],

    'namespaces' => [
        'repositories' => 'App\\Repositories',
        'interfaces' => 'App\\Repositories\\Contracts',
        'services' => 'App\\Services',
    ],

    'auto_bind' => true,
    'generate_service' => false,
    'generate_model_if_missing' => true,
];

use App\Repositories\Contracts\UserRepositoryInterface;

class UserService
{
    public function __construct(
        private readonly UserRepositoryInterface $users
    ) {
    }

    public function activeUsers()
    {
        return $this->users
            ->query()
            ->where('is_active', true)
            ->get();
    }
}

use Gohari\RepositoryPattern\BaseRepositoryInterface;

interface UserRepositoryInterface extends BaseRepositoryInterface
{
    public function findByEmail(string $email);
}

use App\Models\User;
use App\Repositories\Contracts\UserRepositoryInterface;
use Gohari\RepositoryPattern\BaseRepository;

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

    public function findByEmail(string $email)
    {
        return $this->query()->where('email', $email)->first();
    }
}
bash
php artisan vendor:publish --tag=repository-pattern-config
php artisan vendor:publish --tag=repository-pattern-stubs
bash
php artisan repository:make User --model=User
text
app/Repositories/UserRepository.php
app/Repositories/Contracts/UserRepositoryInterface.php
bash
php artisan repository:make {name} --model={model}
bash
php artisan repository:make User --model=User
php artisan repository:make UserRepository --model=User
php artisan repository:make Product --model="App\Models\Product"
php artisan repository:make Admin/User --model=User
php artisan repository:make User --model=User --service
bash
php artisan repository:make Product --model=Product --path=app/Domain/Repositories
bash
php artisan repository:make Product --model=Product --force
bash
php artisan repository:make User --model=User --bind
bash
php artisan repository:make User --model=User --service --bind
bash
php artisan repository:make User --model=User
bash
php artisan repository:make User --model=User --service
text
app/Repositories/UserRepository.php
app/Repositories/Contracts/UserRepositoryInterface.php
app/Services/UserService.php
app/Services/UserServiceInterface.php
app/DTOs/UserData.php
config/repository-pattern.php
bash
php artisan repository:make User --model=User --bind