As name suggested this package will let you use Redis as a database instead of using it just for caching purpose. It works almost the same way as using Laravel Eloquent but with some differences. With this package forget the pain of naming keys and managing them in Redis. Some of the core features are as follows:
- No need to create migration files, just provide required columns in
$fillable
and this package will take care of the rest - Perform CRUD operations just like doing them in Laravel
- Search model functionality
- Managing relations
- Laravel auth backed by Redis
You can install the package via composer:
composer require bilaliqbalr/laravel-redis
You can publish the config file with:
php artisan vendor:publish --provider="Bilaliqbalr\LaravelRedis\LaravelRedisServiceProvider" --tag="laravel-redis-config"
To create new redis model run this command
php artisan redis:model Post
Prefixes are used to maintain the key structure for specific model, this package creates prefixes by default using Model name, and in case you want to change it you can do this as follow
public function prefix() : string
{
return 'post';
}
You can change redis connection in model as well
protected $connection = "redis";
In case you need to get model based on specific field, you can do this by using $searchBy
where you just need to specify column names in the list and this package will store a new key value pair
where key is {model}:column:%s
(%s
is the column value) where value will be the model id to fetch required model.
protected $searchBy = [
'title',
];
To get model based on that title field, you can do this as follows
$post = Post::searchByTitle("iphone");
# This works the same way as we do while using eloquent
# Post::where('title', 'iphone')->first();
Other operations like create, update and delete works same as in Laravel
# Creating new post
$post = Post::create([
'user_id' => 1,
'title' => 'iPhone 13 release',
'description' => 'Lorem ipsum dolor',
]);
# Getting data is a bit different
$post = Post::get(1); // 1 is the model id
# Get post by title
$post = Post::searchByTitle('iphone');
# Update post info
$post->update([
'description' => 'Lorem ipsum dolor sat amet'
]);
# Delete post
$post->delete();
// or
Post::destroy(1);
// return list of all records keys from redis
$post->getAllKeys();
// Return list of all records ids
$post->getAllKeys(true);
If you need to add new searchBy field after you have records in your redis database, then you need to run this command to make old records searchable with new column
php artisan refresh:search_by App\\Redis\\Post
With this package you can even create relation between models but that is not like the one in Laravel, as redis is not a relational database.
# User.php
# Adding post relation
public function posts() {
return $this->relation(new \App\Models\Redis\Post);
}
# Creating post under user
# Below will automatically add user_id field in the Post model.
$post = $user->posts()->create([
'title' => 'iPhone 13 pro',
'description' => 'Lorem ipsum dolor',
]);
# Getting all user posts
$posts = $user->post()->get();
$paginatedPosts = $user->post()->paginate();
# In config/auth.php
'guards' => [
'web' => [
'driver' => 'session',
'provider' => 'redis', // use provider name as in laravel-redis.php config file
],
],
Please see CHANGELOG for more information on what has changed recently.
Please see CONTRIBUTING for details.
The MIT License (MIT). Please see License File for more information.