PHP code example of adityadarma / laravel-service-repository

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

    

adityadarma / laravel-service-repository example snippets


protected NameService $nameService;

public function __construct(
    NameService $nameService
)
{
    $this->nameService = $nameService;
}

public function data()
{
    $this->nameService->functionName()->getData();
}

public function json(Request $request)
{
    $this->nameService->functionName()->toJson();
}

public function withResource(Request $request)
{
    $this->nameService->functionName()->toJsonFromResource(ClassResource::class);
}

public function nameMethod()
{
    try {
         .........
         if (false) {
            throw new CustomException('Error exception');
         }
        ..........
        // Call toJsonFromResource at controller
        return $this->setData($data)
            ->setMessage('Message data')
            ->setCode(200);
        // OR
        // Call toJson at controller
        return $this->setData($data)
            ->setResource(ClassResource::class)
            ->setMessage('Message data')
            ->setCode(200);
    } catch (Exception $e) {
        return $this->exceptionResponse($e);
    }
}

protected NameRepository $nameRepository;

public function __construct(
    NameRepository $nameRepository
)
{
    $this->nameRepository = $nameRepository;
}

public function data()
{
    $this->nameRepository->functionName();
}
bash
php artisan service-repository:install
bash
php artisan make:service nameService
bash
php artisan make:repository nameRepository --model