1. Go to this page and download the library: Download directorytree/activeredis 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/ */
directorytree / activeredis example snippets
namespace App\Redis;
use DirectoryTree\ActiveRedis\Model;
class Visit extends Model {}
Visit::create(['id' => 'custom-id']);
// DuplicateKeyException: A model with the key 'custom-id' already exists.
Visit::create(['id' => 'custom-id']);
// InvalidKeyException: A key is
Visit::create(['id' => 'custom-id']);
// The existing model will be deleted.
Visit::create(['id' => 'custom-id'], force: true);
(new Visit(['id' => 'custom-id'])->save();
(new Visit(['id' => 'custom-id']))->save(force: true);
namespace App\Redis;
use DirectoryTree\ActiveRedis\Model;
class Visit extends Model
{
/**
* The key name for the model.
*/
protected string $key = 'custom_key';
}
namespace App\Redis;
use Illuminate\Support\Str;
use DirectoryTree\ActiveRedis\Model;
class Visit extends Model
{
/**
* Generate a new key for the model.
*/
protected function getNewKey(): string
{
return Str::uuid();
}
}
class Visit extends Model
{
/**
* Indicates if the model should be timestamped.
*/
public bool $timestamps = false;
}
class Visit extends Model
{
const CREATED_AT = 'creation_date';
const UPDATED_AT = 'updated_date';
}
class Visit extends Model
{
/**
* The attributes that should be cast to native types.
*/
protected array $casts = [
'user_id' => 'integer',
'authenticated' => 'boolean',
];
}
use App\Enums\VisitType;
class Visit extends Model
{
/**
* The attributes that should be cast to native types.
*/
protected array $casts = [
'type' => VisitType::class,
];
}
class Visit extends Model
{
/**
* The "booted" method of the model.
*/
protected static function booted(): void
{
static::creating(function (Visit $visit) {
// ...
});
// ...
}
}
class VisitObserver
{
/**
* Handle the "creating" event.
*/
public function creating(Visit $visit): void
{
// ...
}
}
use App\Redis\Visit;
class AppServiceProvider extends ServiceProvider
{
/**
* Bootstrap any application services.
*/
public function boot(): void
{
Visit::observe(VisitObserver::class);
}
}