PHP code example of agungsugiarto / codeigniter4-repository

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

    

agungsugiarto / codeigniter4-repository example snippets


protected $searchable = [
    // where 'title' equals 'Title'
    'title',
];

protected $scopes = [
    // and custom parameter used in your scope
    'custom' => MyScope::class,
];

class MyScope extends ScopeAbstract
{
    public function scope($builder, $value, $scope)
    {
        return $builder->where($scope, $value)->orWhere(...);
    }
}

protected $scopes = [
    // orderBy field
    'orderBy' => OrderByScope::class,
];

return $this->news->withCriteria([
    new MyCriteria([
        'category_id' => '1',
        'name' => 'Name',
        ['created_at', '>', Time::now()],
    ]),
    ...
])->get();

namespace App\Models;

use CodeIgniter\Model;

class News extends Model 
{
    ...
}

namespace App;

use App\Models\News;
use Fluent\Repository\Eloquent\BaseRepository;

class NewsRepository extends BaseRepository
{
    public function entity()
    {
        // Whatever choose one your style.

        return new News();
        // or
        return 'App\Models\News';
        // or
        return News::class;
    }
}

use App\NewsRepository;

class NewsController extends BaseController 
{
    protected $news;

    public function __construct()
    {
        $this->news = new NewsRepository();
    }
    ....
}

/**
 * Get method implement parameter "select", "limit" and "offset".
 * The default will be select * and return all offset data.
 * 
 * Example: $this->news->get(['*'], 50, 100);
 */
$news = $this->news->get();

$news = $this->news->first();

$news = $this->news->find(1);

$news = $this->news->findWhere([
        // where id equals 1
        'id' => '1',
        // other "where" operations
        ['news_category_id', '<', '3'],
        ...
    ]);

$news = $this->news->paginate(15);

// return will be
{
    "data": [
        {
            "id": "3",
            "title": "Ms. Carole Wilderman DDS",
            "content": "Labore id aperiam ut voluptatem eos natus.",
            "created_at": "2020-08-05 17:07:16",
            "updated_at": "2020-08-05 17:07:16",
            "deleted_at": null
        },
        ...
     ],
    "paginate": {}
}

$news = $this->news->orderBy('title', 'desc')->get();

$news = $this->news->create($this->request->getVar());

$data = [
    [
        'title' => 'My title',
        'name'  => 'My Name',
        'date'  => 'My date'
    ],
    [
        'title' => 'Another title',
        'name'  => 'Another Name',
        'date'  => 'Another date'
    ]
];

$news = $this->news->createBatch($data);

$this->news->update($this->request->getVar(), $id);

$data = [
    [
        'title' => 'My title',
        'name'  => 'My Name',
        'date'  => 'My date'
    ],
    [
        'title' => 'Another title',
        'name'  => 'Another Name',
        'date'  => 'Another date'
    ]
];

$news = $this->news->updateBatch($data, 'title');

$this->news->destroy($id);

use Fluent\Repository\Contracts\CriterionInterface;

class MyCriteria implements CriterionInterface
{
    protected $conditions;
    
    public function __construct(array $conditions)
    {
        $this->conditions = $conditions;
    }

    public function apply($entity)
    {
        foreach ($this->conditions as $field => $value) {
            $entity = $entity->where($field, $value);
        }

        return $entity;
    }
}

use App\NewsRepository;

class NewsController extends BaseController 
{
    protected $news;

    public function __construct()
    {
        $this->news = new NewsRepository();
    }

    public function index()
    {
        return $this->news->withCriteria([
            new MyCriteria([
                'category_id' => '1', 'name' => 'Name'
            ]),
            new WhereAdmin(),
            ...
        ])->get();
    }
}

protected $searchable = [
    // where 'title' equals parameter value
    'title',
    // orWhere equals
    'body' => 'or',
    // where like
    'author' => 'like',
    // orWhere like
    'email' => 'orLike',
];

public function index()
{
    return $this->news->scope($this->request)->get();
}

protected $scopes = [
    // orderBy field
    'orderBy' => OrderByScope::class,
    // where created_at date is after
    'begin' => WhereDateGreaterScope::class,
    // where created_at date is before
    'end' => WhereDateLessScope::class,
];

$this->news->scope($this->request)->get();

public $orderable = ['email'];

public function scope(IncomingRequest $request)
{
    // apply build-in scopes
    parent::scope($request);

    // apply custom scopes
    $this->entity = (new NewsScopes($request))->scope($this->entity);

    return $this;
}

use Fluent\Repository\Scopes\ScopesAbstract;

class NewsScopes extends ScopesAbstract
{
    protected $scopes = [
        // here you can add field-scope mappings
        'field' => MyScope::class,
    ];
}

use Fluent\Repository\Scopes\ScopeAbstract;

class MyScope extends ScopeAbstract
{
    public function scope($builder, $value, $scope)
    {
        return $builder->where($scope, $value);
    }
}

https://example.com/news?orderBy=email_desc&begin=2019-01-24&end=2019-01-26