PHP code example of herojhc / laravel-repository

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

    

herojhc / laravel-repository example snippets


'providers' => [
    ...
    Herojhc\Repositories\Providers\RepositoryServiceProvider::class,
],

$app->register(Herojhc\Repositories\Providers\LumenRepositoryServiceProvider::class);

namespace App;

class Post extends Eloquent { // or Ardent, Or any other Model Class

    protected $fillable = [
        'title',
        'author',
        ...
     ];

     ...
}

namespace App;

use Herojhc\Repositories\Eloquent\BaseRepository;

class PostRepository extends BaseRepository {

    /**
     * Specify Model class name
     *
     * @return string
     */
    function model()
    {
        return "App\\Post";
    }
}


use Herojhc\Repositories\Contracts\RepositoryInterface;
use Herojhc\Repositories\Criteria\Criteria;

class MyCriteria extend Criteria {

    public function apply($model, RepositoryInterface $repository)
    {
        $model = $model->where('user_id','=', Auth::user()->id );
        return $model;
    }
}


namespace App\Http\Controllers;

use App\PostRepository;

class PostsController extends BaseController {

    /**
     * @var PostRepository
     */
    protected $repository;

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


    public function index()
    {
        $this->repository->pushCriteria(new MyCriteria1());
        $this->repository->pushCriteria(MyCriteria2::class);
        $posts = $this->repository->all();
		...
    }

}
shell
php artisan vendor:publish --provider "Herojhc\Repositories\Providers\RepositoryServiceProvider"
terminal
php artisan make:criteria My