PHP code example of ra3oul / eloquent-abstract-repository

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

    

ra3oul / eloquent-abstract-repository example snippets


'providers' => [
    ...

    ra3oul\EloquentAbstractRepository\EloquentAbstractRepositoryServiceProvider::class,
],

namespace App;

class Article extends Eloquent { // can be any other class name
    protected $fillable = [
        'name',
        'author',
        ...
     ];

     ...
}

namespace App;
use Foo ;
use ra3oul\EloquentAbstractRepository\repository\RepositoryInterface;

interface ArticleRepositoryInterface extends RepositoryInterface
{

}


namespace App;
use Foo ;
class ArticleRepository extends AbstractEloquentRepository implements ArticleRepositoryInterface


       public function __construct(Foo $model)
    {
        parent::__construct($model);
    }
}

namespace App\Providers;

use Illuminate\Support\ServiceProvider;

class RepositoryServiceProvider extends ServiceProvider
{
    protected function registeredRepositories()
    {
        // 'Repository Interface' => 'Implementation'
        return [
      '\App\ArticleRepositoryInterface' =>
                '\App\ArticleRepository',
                // you should add other interfaces and their implemented classes below !
        ];
    }

    /**
     * Register the service provider.
     *
     * @return void
     */
    public function register()
    {
        $repos = $this->registeredRepositories();

        foreach ($repos as $interface => $implemented) {
            $this->app->bind($interface, $implemented);
        }
    }

namespace App\Http\Controllers;

use App\ArticleRepositoryInterface;

class ArticlesController extends BaseController {

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

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

    ....
}

$articles = $this->repository->findAll();

$aritcles = $this->repository->columns([*])->paginate($limit=10);


$articles = $this->repository->findOneById($id);

$article = $this->repository->columns(['id', 'state_id'])->findOneById($id);

$article = $this->repository->with(['state'])->findOneById($id);

$articles = $this->repository->findOneBy('country_id','15');


$articles = $this->repository->findManyBy('name','rasoul');

$articles = $this->repository->findManyByIds([1,2,3,4,5]);

$article = $this->repository->create( Input::all() );

$article = $this->repository->updateOneById(  $id , Input::all());

$this->repository->deleteOneById($id)