PHP code example of mattb-it / larepo

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

    

mattb-it / larepo example snippets




declare(strict_types=1);

namespace App\Repositories;

use App\Models\User;
use Illuminate\Database\Eloquent\Model;
use Mattbit\Larepo\Repositories\EloquentRepository;

class UserRepository extends EloquentRepository
{
    public function model(): Model
    {
        return new User();
    }
}

php artisan larepo:make:dto User



declare(strict_types=1);

namespace App\DTO\Models;

use Mattbit\Larepo\DTO\ModelDTOInterface;
use Mattbit\Larepo\Enums\Attribute;

readonly class UserDTO implements ModelDTOInterface
{
    public function __construct(
        public Attribute|int $id = Attribute::UNDEFINED,
    ) {}
}



declare(strict_types=1);

namespace App\DTO\Models;

use Mattbit\Larepo\DTO\ModelDTOInterface;
use Mattbit\Larepo\Enums\Attribute;

readonly class UserDTO implements ModelDTOInterface
{
    public function __construct(
        public Attribute|int $id = Attribute::UNDEFINED,
        public Attribute|string $name = Attribute::UNDEFINED,
        public Attribute|string $email = Attribute::UNDEFINED,
        public Attribute|string $password = Attribute::UNDEFINED,
        public Attribute|string $rememberToken = Attribute::UNDEFINED,
    ) {}
}



declare(strict_types=1);

namespace App\Http\Controllers;

use App\DTO\Models\UserDTO;
use App\Models\User;
use App\Repositories\UserRepository;
use Illuminate\Http\RedirectResponse;
use Illuminate\Http\Request;
use Mattbit\Larepo\Enums\Attribute;

class UpdateUserController extends Controller
{
    public function __construct(
        private UserRepository $userRepository,
    ) {
    }

    public function __invoke(Request $request, User $user): RedirectResponse
    {
        $this->userRepository->save(
            modelDTO: new UserDTO(
                name: $request->input('name', Attribute::UNDEFINED),
                email: $request->input('email', Attribute::UNDEFINED),
            ),
            model: $user,
        );

        return response()->redirectTo('/users');
    }
}

// Find by primary key
$user = $this->userRepository->find(1);

// Find by a specific attribute (e.g., email)
$user = $this->userRepository->find('[email protected]', 'email');

// Find by primary key
$user = $this->userRepository->findOrFail(1);

// Find by a specific attribute (e.g., email)
$user = $this->userRepository->findOrFail('[email protected]', 'email');

$users = $this->userRepository->all();

$users = $this->userRepository
    ->query()
    ->where('active', true)
    ->where('role', 'admin')
    ->get();

$this->userRepository->delete($user);

$this->userRepository->save(
    dto: new UserDTO(
        name: 'John Doe',
    ),
    model: $user,
);

$user = $this->userRepository->save(
    dto: new UserDTO(
        name: 'John Doe',
    ),
);
bash
php artisan larepo:make:repository User