<?php namespace app\Models;
use Drapor\CacheRepository\Eloquent\BaseModel;
class User extends BaseModel
{ ... }
?>
<?php namespace app\Repositories;
use Drapor\CacheRepository\CacheRepository;
class UserRepository extends CacheRepository
{
public function __construct(User $user)
{
parent::__construct($user, 'user');
//Create a Relation instance and pass the second argument as a boolean
//indicating wether or not it should be removed from the cache when the User object is updated.
$task = new Relation('task',true);
//Only call $this->setRelations() if you wish to eager load these relations before every call. This method accepts both
//instances of Relation and strings.
$this->setRelations([
'group', $task
]);
}
}
<?php namespace app/controllers;
use Repositories/UserRepository;
use Request;
class UserController extends Controller
{
protected $repository;
public function __construct(UserRepository $repository)
{
$this->repository = $repository;
}
public function index()
{
$limit = Request::input('limit');
$users = $this->repository
->orderBy('name','ASC')
->paginate($limit);
return response()->json($users);
}
public function getSadUsers()
{
$limit = Request::input('limit');
//Paginate accepts a second argument for search parameters.
//This should be an array of arrays, each with at least a key and a value.
$params = [
[
'key' => 'sad',
'value' => 'true'
],
[
'key' => 'happiness',
'operator' => '<',
'value' => '2',
'keyword' => 'AND'
]
];
//Alternatively you can call Argument::extract(Request::input(),'search')
//and any search information will automatically be formatted
$users = $this->repository
->with('equity','orders')
->paginate($limit,$params);
return response()->json($users->toJson());
}
public function find($id)
{
$user = $this->repository->with('tasks')->find($id);
return response()->json($user->toJson());
}
public function update($id)
{
//The Repository class will automatically clean out any input
//that isn't fillable by our model.
$user = $this->repository->update($id,Request::input());
return response()->json($user->toJson());
}
}
$user = $this->repository->noCache()->find($id);
or by using the PlainRepository class instead.