PHP code example of rafi021 / repository-pattern

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

    

rafi021 / repository-pattern example snippets


return [
];

// will genearate controller, factory, service, seeder, repository, resource and migration
php artisan make:model User --all

// use the service and repository flag to generate the class
php artisan make:model User --service --repository

// use the short form to generate model with service and repository
php artisan make:model User -sr -rt

php artisan make:repository User
// or
php artisan make:repository UserRepository

// or create together with a service
php artisan make:repository User --service
// or
php artisan make:repository UserRepository --service

// or create a service separately
php artisan make:service User
// or
php artisan make:service UserService


// app/Repositories/Interfaces/UserRepository.php



namespace App\Repositories\Interfaces;

use Mwakalingajohn\LaravelEasyRepository\Repository;

class UserRepositoryInterface extends Repository{

    // Write something awesome :)
}


// app/Repositories/Eloquent/UserRepository.php



namespace App\Repositories\Eloquent;

use Mwakalingajohn\LaravelEasyRepository\Repository;
use Mwakalingajohn\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
    * @property Model|mixed $model;
    */
    protected $model = Model::class;

    // Write something awesome :)
}


// app/Services/UserService



namespace App\Services;

use Mwakalingajohn\LaravelEasyRepository\Service;

class UserService extends Service{

    /**
    * The repository interface to use in this service. Will allow to use within
    * methods $this->repository. It will be resolved from the container
    * @property string $repositoryInterface;
    */
    protected string $repositoryInterface = "App\Repositories\Interfaces\UserRepositoryInterface";

    // Define your custom methods :)
}




namespace App\Http\Controllers;

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

class UserController extends Controller
{
    public function index()
    {
        $userService = new UserService;
        return $userService->all();
    }
}

// or using the repository, the service container will automatically resolve the repository for you



namespace App\Http\Controllers;

use App\Repositories\Interfaces\UserRepositoryInterface;
use App\Services\UserService;
use Illuminate\Http\Request;

class UserController extends Controller
{

    public $repository;

    public function __construct(UserRepositoryInterface $userRepositoryInterface)
    {
        $this->repository = $userRepositoryInterface;
    }

    public function index()
    {
        return $this->repository->all();
    }
}



interface Repository
{
    /**
     * Fin an item by id
     * @param int $id
     * @return Model|null
     */
    public function find(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);
}



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",
];

## Testing

bash
php artisan vendor:publish --tag="repository-pattern-migrations"
php artisan migrate
bash
php artisan vendor:publish --tag="repository-pattern-config"
bash
php artisan vendor:publish --tag="repository-pattern-views"
bash
php artisan vendor:publish --provider="Mwakalingajohn\LaravelEasyRepository\LaravelEasyRepositoryServiceProvider" --tag="easy-repository-config"