PHP code example of shintarosakata / laravel-repository

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

    

shintarosakata / laravel-repository example snippets




namespace App\Providers;

use shintarosakata\LaravelRepository\RepositoryProvider\RepositoryProvider as upstreamRepositoryProvider;

class RepositoryProvider extends upstreamRepositoryProvider
{
    // ...
    
    protected $repositories = [
        'Samples', // Add <name> here
    ];

    // ...



namespace App\Repositories\Samples;

use shintarosakata\LaravelRepository\Repository\Repository;
use Illuminate\Database\Eloquent\Collection;
use App\Entities\Sample;

class Samples extends Repository implements SamplesInterface
{
    protected $table = 'samples'; // Add DB Table name here
    
    public function fetchFirst(): Sample
    {
        return $this->builder->first();
    }
    
    public function fetchAll(): Collection
    {
        return $this->builder->get();
    }
}




namespace App\Repositories\Samples;

use shintarosakata\LaravelRepository\Repository\RepositoryInterface;
use Illuminate\Database\Eloquent\Collection;
use App\Entities\Sample;

interface SamplesInterface extends RepositoryInterface
{
    // Add public functions here...
    
    public function fetchFirst(): Sample;

    public function fetchAll(): Collection;
}



namespace App\Entities;

use shintarosakata\LaravelRepository\Entity\Entity as upstreamEntity;

class Sample extends upstreamEntity
{
    // Add behavior here...
}



namespace App\Http\Controllers;

use App\Repositories\Samples\SamplesInterface;

class SampleController extends Controller
{
    private $samples_repository;

    // Dependency injection
    public function __construct(SamplesInterface $samples_repository) {
        $this->samples_repository = $samples_repository;
    }

    public function index()
    {
        $first_sample_entity = $this->samples_repository->fetchFirst();
        return view('sample.index', $first_sample_entity);
    }
}


.
└── app
    ├── Entities
    │   └── Sample.php
    │
    ├── Repositories
    │   ├── Samples.php
    │   └── SamplesInterface.php
    │
    └── Providers
        └── RepositoryProvider
./config/app.php


return [
    
    // ...
    
    'providers' => [
        // ...

        App\Providers\RepositoryProvider::class, // register provider
    ],
    
    // ...

];