codewiser/inarticulate

Inarticulate is not Eloquent (Laravel Redis Model)

0.1.12 2020-10-26 13:27 UTC

This package is auto-updated.

Last update: 2024-04-26 21:41:42 UTC


README

Inarticulate is a Eloquent extender to work with Redis storage.

Inarticulate Model is absolutely the same as Eloquent Model. It has one important difference — there are no autoincrement keys in Redis. So Inarticulate has

public $incrementing = false;

Inarticulate Builder extends Eloquent Builder, but it overrides just few methods — find, insert, update, delete and exists. So just do not call any others — that are not applicable for Redis.

As Inarticulate extends Eloquent Model your may use attributes Accessors and Mutators, castings, property guards etc.

Redis key

Redis key is build up from three properties — hashed APP_KEY, $table property and model primary key. Redis key will look like

d781b523ed68d90c599832956ff10ea9:article:243

Redis expire

Set positive protected $redisExpire value to set expiration timout for Model.

Usage

To prevent Builder from resolving undefined attributes as a relations (that is not supported, you know) just define default attribute values in $attributes array.

/**
 * @property mixed id
 * @property string title
 * @property string body
 */
class Article extneds \Codewiser\Inarticulate\Model
{
    protected $attributes = [
        'title' => null,
        'body' => null
    ];
}
$article = new Article();

$article->exists; // FALSE

$article->id = 1;
$article->title = "Title";
$article->save();

$article->exists; // TRUE
$article = Article::query()->findOrFail(1);

echo $article->title; 

Builder methods

Fearlessly use these Model and Builder methods:

  • find
  • findOrFail
  • findOrNew
  • findMany
  • exists
  • refresh

Get all records

Model::all();
// or
Model::query()->get();