PHP code example of nauxa-labs / laravel-repository-service
1. Go to this page and download the library: Download nauxa-labs/laravel-repository-service 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/ */
nauxa-labs / laravel-repository-service example snippets
namespace App\Services;
use Nauxa\RepositoryService\Contracts\ServiceContract;
interface UserService extends ServiceContract
{
// Define your own methods with any signature
public function create(UserDTO $dto, ?string $role = null): User;
public function findByEmail(string $email): ?User;
}
namespace App\Services;
use Nauxa\RepositoryService\Abstracts\BaseService;
class UserServiceImplement extends BaseService implements UserService
{
public function __construct(
protected UserRepository $userRepository
) {}
public function create(UserDTO $dto, ?string $role = null): User
{
// Your implementation
}
public function findByEmail(string $email): ?User
{
// Your implementation
}
}
namespace App\Repositories;
use Nauxa\RepositoryService\Contracts\RepositoryContract;
interface UserRepository extends RepositoryContract
{
// Add custom methods if needed
public function findByEmail(string $email): ?User;
}
namespace App\Repositories;
use App\Models\User;
use Nauxa\RepositoryService\Abstracts\EloquentRepository;
class UserRepositoryImplement extends EloquentRepository implements UserRepository
{
public function __construct(User $model)
{
$this->model = $model;
}
public function findByEmail(string $email): ?User
{
return $this->findWhere(['email' => $email])->first();
}
}
// Load users with their posts
$users = $this->userRepository->with(['posts', 'profile'])->all();
// Or chain with other methods
$users = $this->userRepository->with('posts')->paginate(10);