PHP code example of buqiu / repository

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

    

buqiu / repository example snippets




namespace App\Http\Controllers;

use App\Repository\UserRepository;

class FilmsController extends Controller {

    /**
     * @var UserRepository 
     */
    private $filmRepository;

    public function __construct(UserRepository $userRepository) 
    {
        $this->filmRepository = $userRepository;
    }
    
    /**
     * @return mixed
     */
    public function index() 
    {
        return response()->json($this->userRepository->all());
    }
}



namespace App\Repository\Criteria\Films;

use Buqiu\Repository\Criteria\Criteria;
use Buqiu\Repository\Contracts\RepositoryInterface;

class LengthOverTwoHours extends Criteria 
{
    /**
     * @param $model
     * @param RepositoryInterface $repository
     *                                       
     * @return Model
     */
    public function apply($model, RepositoryInterface $repository)
    {
        return $model->where('length', '>', 120);
    }
}

 

namespace App\Http\Controllers;

use App\Repositories\FilmRepository;
use App\Repositories\Criteria\Films\LengthOverTwoHours;

class FilmsController extends Controller 
{
    /**
     * @var FilmRepository
     */
    private $filmRepository;

    public function __construct(FilmRepository $filmRepository) 
    {
        $this->filmRepository = $filmRepository;
    }

    public function index() 
    {
        $this->filmRepository->pushCriteria(new LengthOverTwoHours());
        
        return response()->json($this->filmRepository->all());
    }
}
shell script
php artisan make:repository Film
shell script
php artisan vendor:publish --provider="Buqiu\Repository\Providers\RepositoryProvider"
shell script
public function all($columns = ['*'])
public function lists($value, $key = null)
public function paginate($perPage = 1, $columns = ['*'], $method = 'full');
public function create(array $data)
// 如果你使用的是 mongodb,则需要指定主键 $attribute
public function update(array $data, $id, $attribute = 'id')
public function destroy($id)
public function destroyWhere(array $attributes)
public function orderBy(string $columns, $direction = 'ASC')
public function with(array $relations)
public function withCount($relations)
public function whereIn(string $attribute, array $data)
public function whereHas(string $relation, \Closure $callback = null)
public function orWhereHas(string $relation, \Closure $callback = null)
public function find($id, $columns = ['*'])
public function findByField($field, $value, $columns = ['*'])
public function getByField($field, $value, $columns = ['*'])
public function findByAttributes(array $attributes = [], array $columns = array('*'))
public function getByAttributes(array $attributes = [], array $columns = array('*'))
public function paginateByAttributes(array $attributes = [], int $perPage = 20, array $columns = array('*'), $method = 'full')
shell script
$this->userRepository->destroy($id);
shell script
$this->userRepository->find($id);
shell script
$this->userRepository->find($id, ['name', 'mobile', 'email']);
shell script
$this->userRepository->findByField('mobile', $mobile);
shell script
$this->userRepository->getByField('user_id', $user_id);
shell script
$this->userRepository->getByAttributes([
  'sex' => $sex,
  ['year', '>', '$year']
]);
shell script
$this->userRepository->paginateByAttributes([
  'sex' => $sex,
  ['year', '>', '$year']
]);
shell script
php artisan make:criteria LengthOverTwoHours --model=user