PHP code example of dees040 / repository

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

    

dees040 / repository example snippets




namespace App\Repositories\Contracts;

use Dees040\Repository\Contracts\Repository;

interface PostRepository extends Repository
{

}




namespace App\Repositories;

use App\Post;
use App\Repositories\Contracts\PostRepository;
use Dees040\Repository\Eloquent\BaseRepository;

class PostEloquentRepository extends BaseRepository implements PostRepository
{
    /**
     * Get the base model.
     *
     * @return string
     */
    public function getModel()
    {
        return Post::class;
    }
}




namespace App\Providers;

use Illuminate\Support\ServiceProvider;

class RepositoryServiceProvider extends ServiceProvider
{
    /**
     * The contracts with their associated repositories.
     *
     * @var array
     */
    protected $repositories = [
        \App\Repositories\Contracts\PostRepository::class => \App\Repositories\PostEloquentRepository::class,
    ];
    
    /**
     * Bootstrap services.
     *
     * @return void
     */
    public function boot()
    {
       //
    }

    /**
     * Register services.
     *
     * @return void
     */
    public function register()
    {
        foreach ($this->repositories as $contract => $repository) {
            $this->app->bind($contract, $repository);
        }
    }
}

public function __contruct(PostRepository $repostiroy)
{
    $this->repository = $repostiroy;
}

$this->repository->pushCriteria(new Criteria());
$this->repository->pushCriteria(Criteria::class);



namespace App\Criteria;

use Dees040\Repository\Contracts\Criteria;

class HasUserRelationCriteria implements Criteria
{
    /**
     * Execute the criteria.
     *
     * @param  \Illuminate\Database\Eloquent\Model  $model
     * @param  \Dees040\Repository\Contracts\Repository  $repository
     * @return \Illuminate\Database\Eloquent\Model
     */
    public function apply($model, $repository)
    {
        return $this->model->orderBy('created_at')
            ->whereHas('users', function ($query) {
                $query->where('users.id', 1);
            });
    }
}

public function __construct()
{
    $this->pushCriteria(\Dees040\Repository\Criteria\RequestCriteria::class);
}

/**
 * Get all fields which are searchable.
 *
 * @return array
 */
public function getSearchableFields()
{
    return [
        'title',
        'body',
    ];
}
bash
php artisan make:repository Post