PHP code example of adamkhoa03 / eloquent-repository

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

    

adamkhoa03 / eloquent-repository example snippets

 bash
php artisan make:repository UserRepository
 bash
php artisan make:repository UserRepository --model=User
 php
namespace App\Http\Controllers;

use App\Repositories\UserRepository;

class HomeController extends Controller
{
    public function index(UserRepository $userRepository)
    {
        return $userRepository->get();
    }
}
 php
namespace App\Http\Controllers;

use App\User;
use Adamkhoa03\EloquentRepository\EloquentRepository;

class HomeController extends Controller
{
    public function index(EloquentRepository $repository)
    {
        return $repository->setEntity(User::class)->get();
    }
}
 php
$userRepository->all();
 php
$userRepository->get(['id', 'first_name']);
 php
$userRepository->paginate(10);
 php
$userRepository->find(1); 
 php
$userRepository->getWhere('first_name', 'John');
 php
$userRepository->getWhereFirst('first_name', 'John');
 php
$userRepository->getWhereIn('first_name', ['John', 'Jane', 'Dave']);
 php
$userRepository->getWhereInFirst('first_name', ['John', 'Jane', 'Dave']);
 php
$userRepository->findAndUpdate(1, ['first_name' => 'Dave']);
 php
$user = $userRepository->find(1);
$userRepository->delete($user);
 php
$userRepository->findAndDelete(1);
 php
$userRepository->findFromTrashed(1);
 php
$user = $userRepository->findFromTrashed(1);
$userRepository->restore($user);
 php
$userRepository->findAndRestore(1);
 php
$userRepository->withCriteria(new EagerLoad('posts', 'country'))->get();
 php
$userRepository->withCriteria(new Scope('active', 'admin'))->get();
 php
$userRepository->withCriteria(new OrderBy('username', 'asc'))->get();
 php
$userRepository->withCriteria(new OrderBy('username'))->get();
 php
use Adamkhoa03\EloquentRepository\Repository\Criteria\Criterion;

class Ascending implements Criterion
{
    /**
     * @var string
     */
    private $column;

    /**
     * @param string $column
     */
    public function __construct($column = 'id')
    {
        $this->column = $column;
    }

    /**
     * @param $entity
     *
     * @return mixed
     */
    public function apply($entity)
    {
        return $entity->orderBy($this->column, 'asc');
    }
}