PHP code example of chr15k / laravel-repository

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

    

chr15k / laravel-repository example snippets


 $this->app->bind(
    'App\Repositories\Contracts\UserRepositoryInterface', // <-- injected into controller constructor
    'App\Repositories\Eloquent\UserRepository' // <-- Repo class
);

Chr15k\Repository\RepositoryServiceProvider::class,

$this->app->bind(
    'App\Repositories\Contracts\UserRepositoryInterface',
    'App\Repositories\Eloquent\UserRepository'
);



namespace App\Http\Controllers;

use App\Repositories\Contracts\UserRepositoryInterface;

class UserController extends Controller
{
    protected $userRepo;

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

    public function index()
    {
        $users = $this->userRepo->all();

        return view('users.index', compact('users'));
    }

    public function show($id)
    {
        $user = $this->userRepo->find($id);

        return view('users.show', compact('user'));
    }
}

$users = $this->userRepo->someComplexQuery();

    $this->repo->all($related = []);
    $this->repo->chunk($size, $callback);
    $this->repo->cursor();
    $this->repo->create($attributes = [], $related = []);
    $this->repo->destroy($id);
    $this->repo->errors();
    $this->repo->find($id, $related = []);
    $this->repo->findOrFail($id, $related = []);
    $this->repo->findOrNew($id, $related = []);
    $this->repo->getNew($attributes = []);
    $this->repo->model();
    $this->repo->paginate($perPage, $related = []);
    $this->repo->update($id, $attributes = [], $related = []);
bash
php artisan vendor:publish --provider="Chr15k\Repository\RepositoryServiceProvider"
bash
php artisan make:repository User
bash
├── Repositories
│   ├── Contracts
│   │   └── UserRepositoryInterface.php
│   └── Eloquent
│       └── UserRepository.php