mikenolimits / cache-repository by dondraper_

Simple to use Caching Repository for Laravel Eloquent
501
12
1
Package Data
Maintainer Username: dondraper_
Maintainer Contact: empathynyc@gmail.com (Michael Kantor)
Package Create Date: 2015-07-05
Package Last Update: 2018-04-30
Language: PHP
License: Apache-2.0
Last Refreshed: 2024-11-17 03:00:31
Package Statistics
Total Downloads: 501
Monthly Downloads: 0
Daily Downloads: 0
Total Stars: 12
Total Watchers: 1
Total Forks: 1
Total Open Issues: 0
  <?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.