PHP code example of gridprinciples / repository

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

    

gridprinciples / repository example snippets


    GridPrinciples\Repository\RepositoryServiceProvider::class,
    

    

    namespace App\Repositories;

    use GridPrinciples\EloquentRepository;

    class FooRepository extends EloquentRepository {
        protected static $model = \App\Foo::class;
    }

    

    

    namespace App\Http\Controllers;

    use App\Repositories\FooRepository;

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

    public function somePage($id)
    {
        $model = $this->repository->get($id);

        if(!$model) {
            // Model not found.
            return abort(404);
        }

        return view('my_view', [
            'foo' => $model,
        ]);
    }

    

$newModel = $this->repository->save([
    'title' => 'This is indicative of a title',
    'description' => 'You might have a description field, perhaps.',
]);

$singleModel = $this->repository->get(1);
$multipleModels = $this->repository->get([2, 3, 4]);

$pageOfModels = $this->repository->index(10); // 10 records per page

$data = [
    'status' => 'active',
];
$id = 1;

$this->repository->save($data, $id);

$this->repository->delete($id);