1. Go to this page and download the library: Download zatoday/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/ */
zatoday / repository example snippets
namespace App\Repositories;
use ZAToday\Repository\Repository;
class User extends Repository {
public function model() {
return \App\User::class;
}
}
namespace App;
use Illuminate\Database\Eloquent\Model;
class User extends Model {
}
namespace App\Http\Controllers;
use App\Repositories\User;
class UserController extends Controller {
private $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 - [x] lists($value, $key = null)
public function - [x] paginate($perPage = 1, $columns = array('*'));
public function - [x] create(array $data)
public function - [x] update(array $data, $id, $attribute = "id")
// if you use mongodb then you'll need to specify primary key $attribute
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)
namespace App\Repositories\Criteria;
use ZAToday\Repository\Criteria;
use ZAToday\Repository\Contracts\RepositoryInterface as Repository;
class UserMaxId extends Criteria {
/**
* @param $model
* @param RepositoryInterface $repository
* @return mixed
*/
public function apply($model, Repository $repository)
{
$model = $model->where('id', '<', 5);
return $model;
}
}
namespace App\Http\Controllers;
use App\Repositories\Criteria\UserMaxId;
use App\Repositories\User;
class UserController extends Controller {
/**
* @var User
*/
private $user;
public function __construct(User $user) {
$this->user = $user;
}
public function index() {
$this->user->pushCriteria(new UserMaxId);
return \Response::json($this->user->all());
}
}
namespace App\Http\Controllers;
use App\Repositories\Criteria\UserMaxId;
use App\Repositories\User;
class UserController extends Controller {
/**
* @var User
*/
private $user;
public function __construct(User $user) {
$this->user = $user;
}
public function index() {
$criteria = new UserMaxId
return \Response::json($this->user->getByCriteria($criteria)->all());
}
}