PHP code example of arungpisyadi / laravel-repository-and-service-pattern-pack

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

    

arungpisyadi / laravel-repository-and-service-pattern-pack example snippets



return [
    /**
     * The directory for all the repositories
     */
    "repository_directory" => "app/Repositories",

    /**
     * Default repository namespace
     */
    "repository_namespace" => "App\Repositories",

    /**
     * The directory for all the services
     */
    "service_directory" => "app/Services",

    /**
     * Default service namespace
     */
    "service_namespace" => "App\Services",

    /**
     * Default repository implementation
     */
    "default_repository_implementation" => "Eloquent",

    /**
     * Current repository implementation
     */
    "current_repository_implementation" => "Eloquent",
];


// app/Repositories/Interfaces/UserRepository.php



namespace App\Repositories\Interfaces;

use LaravelEasyRepository\Repository;

class UserRepositoryInterface extends Repository{

    // Write something awesome :)
}


// app/Repositories/Eloquent/UserRepository.php



namespace App\Repositories\Eloquent;

use LaravelEasyRepository\Repository;
use LaravelEasyRepository\Implementations\Eloquent;
use App\Repositories\Interfaces\UserRepositoryInterface;

class UserRepository extends Eloquent implements UserRepositoryInterface{

    /**
    * Model class to be used in this repository for the common methods inside Eloquent
    * Don't remove or change variable $model or $this->model
    * @property Model|mixed $model;
    */
    protected $model;

    public function __construct(Model $model)
    {
        $this->model = $model;
    }
}


// app/Services/UserService



namespace App\Services;

use {repositoryInterfaceNamespace}\{repositoryInterface};
class UserService {

   /**
   * don't change $this->mainRepository variable name
   * because used in service class
   */
   protected $mainRepository;

  public function __construct({repositoryInterface} $mainRepository)
  {
    $this->mainRepository = $mainRepository;
  }

   // Define your custom methods :)
}




namespace App\Http\Controllers;

use App\Services\UserService;
use Illuminate\Http\Request;

class UserController extends Controller
{
    public function __construct(UserService $mainService)
    {
        $this->mainService = $mainService;
    }
    
    public function all () {
      return $this->mainService->all();
    }

}



interface Repository
{
    /**
     * Fin an item by id
     * @param int $id
     * @return Model|null
     */
    public function find(int $id);

    /**
     * Find or fail an item by id
     * @param int $id
     * @return Model|null
     */
    public function findOrFail(int $id);

    /**
     * Return all items
     * @return Collection|null
     */
    public function all();

    /**
     * Return query builder instance to perform more manouvers
     * @return Builder|null
     */
    public function query();

    /**
     * Create an item
     * @param array|mixed $data
     * @return Model|null
     */
    public function create($data);

    /**
     * Update a model
     * @param int|mixed $id
     * @param array|mixed $data
     * @return bool|mixed
     */
    public function update($id, array $data);

    /**
     * Delete a model
     * @param int|Model $id
     */
    public function delete($id);

    /**
     * multiple delete
     * @param array $id
     * @return mixed
     */
    public function destroy(array $id);

}




namespace App\Services;

use LaravelEasyRepository\Traits\ResultService;
use {repositoryInterfaceNamespace}\{repositoryInterface};

class UserService {
 use ResultService;

  /**
    * don't change $this->mainRepository variable name
    * because used in service class
    */
    protected $mainRepository;

   public function __construct({repositoryInterface} $mainRepository)
   {
     $this->mainRepository = $mainRepository;
   }

    // Define your custom methods :)
    
    public function all () {
        try {
            $result = $this->mainRepository->all();
            return $this->setStatus(true)
                        ->setResult($result)
                        ->setCode(200)
                        ->setMessage('your message');
        } catch (\Exception $e) {
            return $this->exceptionResponse($e);
        }            
    }    
}



namespace App\Http\Controllers;

use App\Services\UserService;
use Illuminate\Http\Request;
use LaravelEasyRepository\Traits\Response;

class UserController extends Controller
{
   use Response;
    public function __construct(UserService $mainService)
    {
        $this->mainService = $mainService;
    }
    
    public function all () {
      $result = $this->mainService->all();
      return $this->responseJson(
        $result->getStatus(),
        $result->getMessage(),
        $result->getResult(),
        $result->getCode()
      );
    }

}
bash
php artisan vendor:publish --provider="LaravelEasyRepository\LaravelEasyRepositoryServiceProvider" --tag="easy-repository-config"