PHP code example of samkitano / repository

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

    

samkitano / repository example snippets


 namespace App\Repositories;

use Kitano\Repository\Contracts\RepositoryInterface;
use Kitano\Repository\Eloquent\Repository;

class UseRepository extends Repository {

    public function model() {
        return 'App\User';
    }
}

 namespace App;

use Illuminate\Database\Eloquent\Model;

class User extends Model {

    protected $primaryKey = 'user_id';

    protected $table = 'users';

    protected $casts = [
        "verified"       => 'boolean'
    ];
}



namespace App\Http\Controllers;

use App\Repositories\UsersRepository as User;

class UsersController extends Controller {

    protected $user;

    public function __construct(User $user) {

        $this->user = $user;
    }

    public function index() {
        return response()->json($this->user->all());
    }
}

public function all($columns = array('*'))
public function lists($value, $key = null)
public function paginate($perPage = 1, $columns = array('*'));
public function create(array $data)
public function update(array $data, $id, $attribute = "id")
public function delete($id)
public function find($id, $columns = array('*'))
public function findBy($field, $value, $columns = array('*'))
public function findAllBy($field, $value, $columns = array('*'))
public function findWhere($where, $columns = array('*'))

public function apply($model, Repository $repository)

$this->user->create($input);

$this->user->update($input, $user_id);

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

$this->user->find($id);

$this->user->find($id, ['name', 'email', 'created_at']);

$this->user->findBy('email', $email);

$this->user->findAllBy('active', true);

$this->user->findWhere([
    'active' => true,
    ['created_at', '>', Carbon::yesterday()]
]);



namespace App\Repositories\Criteria\Users;

use Carbon\Carbon;
use Kitano\Repository\Criteria\Criteria;
use Kitano\Repository\Contracts\RepositoryInterface as Repository;

class RegisteredToday extends Criteria {

    /**
     * @param $model
     * @param RepositoryInterface $repository
     * @return mixed
     */
    public function apply($model, Repository $repository)
    {
        $yesterday = Carbon::yesterday();
        $model     = $model->where('created_at', '>', $yesterday);

        return $model;
    }
}



namespace App\Http\Controllers;

use App\Repositories\Criteria\Users\RegisteredToday;
use App\Repositories\UsersRepository as User;

class UsersController extends Controller {

    /**
     * @var User
     */
    protected $user;

    public function __construct(User $user) {

        $this->user = $user;
    }

    public function index() {
        $this->user->addCriteria(new RegisteredToday());
        return response()->json($this->user->all());
    }
}