PHP code example of happynessarl / caching-management

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

    

happynessarl / caching-management example snippets


use Happynessarl\Caching\Management\Services\BaseService;
use Illuminate\Pagination\LengthAwarePaginator;
use Happynessarl\Caching\Management\Services\Contracts;

class UserService extends BaseService implements IBaseService
{ 
    private function initializeModel(): void
    {
        $this->setModel(new User());
    }

    /**
     * you need to implement it in your service to initialize Model Object
     * 
     * @inheritDoc
     */
    protected function getModelObject(): User
    {
        $this->initializeModel();
        return $this->getModel();
    }

    /**
     * To paginate, proceed as follows
     */
    public function getAllUsers(): LengthAwarePaginator
    {
        /** Call this method when you want to fetch all the data in a Table, similar to what Laravel offers: Employee::all(); instead, use this method */
        $items = $this->cacheAllRecords();

        /** Call this method and your data will be paginated, $items = collection or array, and 9 is a perPage that you want to paginate */
        $paginator = $this->paginateModelCollection($items, 9);

        return $paginator;
    }

    // your another methods here
}


namespace App\Models;
 
use Illuminate\Database\Eloquent\Model; 

class Client extends Model
{ 
    use  BaseModel; //Same to this BaseModelTrait

    protected $fillable = ['user_id', 'address', 'phone'];
    
    /** Here's the this to add to all your models, specifying the relationships inside.  */
    protected $cacheableRelations = ['user', 'orders'];

    public function user()
    {
        return $this->belongsTo(User::class);
    }

    public function orders()
    {
        return $this->hasMany(Order::class);
    }
}