PHP code example of fomvasss / laravel-repository

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

    

fomvasss / laravel-repository example snippets




namespace App\Repositories;

use App\Models\Article;
use Fomvasss\Repository\Eloquent\BaseRepository;

class ArticleRepository extends BaseRepository
{
    public function model()
    {
        return Article::class;
    }
    //...
}



namespace App\Http\Controllers;

use App\Repositories\ArticleRepository;

class ArticleController extends BaseController {

    protected $repository;

    public function __construct(ArticleRepository $repository) {
        $this->repository = $repository;
    }
    
    public function index() {
		$articles = $this->repository
			->scopes(['byStatus', 1], ['sortable', ['id'=>'desc']], 'searchable')
			->with('user')
			->where('created_at', \Carbon\Carbon::yesterday(), '>')
			->orderBy('created_at')
			->get();

        //....
    }
    
        public function show() {
    		$article = $this->repository
    			->scope('byStatus', 1)
    			->with(['user', 'categories'])
    			->where('created_at', \Carbon\Carbon::today(), '<')
    			->orderBy('created_at')
    			->firstOrFail();
    
            //....
        }
    //....
}

    public function myCustomMethodByType($attributes)
    {
        $this->applyExtras();
        $models = $this->query;

        if (!empty($attributes['type'])) {
            $models = $this->query->where('type', $attributes['type']);
        }

        $this->unsetClauses();
        return $models;
    }

protected $listen = [
	\Fomvasss\Repository\Events\RepositoryEntityCreated::class => [
		\App\Listeners\CreatedNewModelInRepoListener::class
	]
];

public function handle(RepositoryEntityCreated $event)
{
	dd($event->getAction());
	dd($event->getModel());
	dd($event->getRepository());
}



namespace App\Repositories;

use App\Models\Article;
use Fomvasss\Repository\Contracts\CacheableInterface;
use Fomvasss\Repository\Eloquent\BaseRepository;
use Fomvasss\Repository\Traits\CacheableRepository;

class ArticleRepository extends BaseRepository implements CacheableInterface
{
    use CacheableRepository;

	protected $cacheTime = 60;
	
    protected $cacheTimeForMethod = [
        'all' => 10,
        'get' => 10,
        'paginate' => 10,
        'find' => 1,
    ];
    
    protected $cacheOnly = ['all', 'get', 'find'];

    public function model()
    {
        return Article::class;
    }
}

protected $routeMiddleware = [
	//...
	'rpc-off' => \Fomvasss\Repository\Http\Middleware\RepositoryCacheOff::class,
];

Route::get('article', ArticleController@index)->middleware(['rpc-off']);
bash
php artisan vendor:publish --provider="Fomvasss\Repository\Providers\RepositoryServiceProvider" --tag="repository-config"