Important
- Not an official package
- The way I write it may not be according to the standard design pattern
Supports I always want to develop so that the code can get better. Please give me feedback if you see any problems
- Thanks redislabs-rejson
Not support
- Relationship
Next update plan
- Authentication support
- Relationship support
- Facade support
Database
Install
- Download lib
- Create lib in root project
- Add to composer.json
add to require
"require": {
"ken/redisjsonmodel": "*@dev",
....
}
add repositories
"repositories": [
{
"type": "path",
"url": "./lib/*"
}
]
run
composer update
Set in .env Prefix
// example: simba:post:id
REDIS_JSON_NAME=simba
// predis or phpredis
REDIS_CLIENT=predis
Create Index for Search add CreateIndex Interface
class Post extends Model implements CreateIndex
function getFielDataType
public function getFielDataType(): array
{
return [
'id' => [
'type' => DataType::INT->value,
'index' => true
],
'title' => [
'type' => DataType::TEXT->value,
'index' => true
],
];
}
RUN Command --class = path class model
php artisan create-index --class=App\\Models\\Redis\\Post
The data is supported by indexing
- case INT = 'NUMERIC';
- case BOOL = 'boolean';
- case TEXT = 'TEXT';
- case STRING = 'STRING';
- case TAG = 'TAG'; For data type Text, it will always be a full text search
Create Model
namespace App\Models\Redis;
use Ken\Contracts\CreateIndex;
use Ken\Enums\DataType;
use Ken\Models\Model;
class Post extends Model implements CreateIndex
{
public function prefix(): string
{
return 'post';
}
public $timestamps = true;
protected $fillable = [
'id',
'title',
'content',
];
public function getFielDataType(): array
{
return [
'id' => [
'type' => DataType::INT->value,
'index' => true
],
'title' => [
'type' => DataType::TEXT->value,
'index' => true
],
];
}
}
create
$post = new Post();
$post = $post->create([
'title' => 'Test create',
'content' => 'Hiha'
]);
update
$post = $post->firstById(1);
$post->content = "Change";
$post->save();
or
$post->update([
'content' => "Change"
]);
Delete
$post->delete();
first
//first by id
$post = $post->firstById(1);
//first where
$post = $post->where('title','=','Redis')->first();
get
$post = $post->where('title','=','Redis')->get();
paginate
$post = $post->where('title','=','Redis')->paginate(20);
support : in, >, <
$post = $post->where('id','>',1)
$post = $post->where('id','<',1)
$post = $post->whereIn('id',[1,2,3,4])
sort $post = $post->where('id','>',1)->sort('id','asc');
raw query
$post = $post->rawQuery('FT.SEARCH',...$param);