bilaliqbalr / laravel-redis

This package will help you use Redis as base database within the Laravel environment

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Laravel Redis

Latest Version on Packagist GitHub Tests Action Status GitHub Code Style Action Status Total Downloads


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:

  1. No need to create migration files, just provide required columns in $fillable and this package will take care of the rest
  2. Perform CRUD operations just like doing them in Laravel
  3. Search model functionality
  4. Managing relations
  5. Laravel auth backed by Redis

Installation

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"

Usage

To create new redis model run this command

php artisan redis:model Post

Prefixes

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';
}

Change connection

You can change redis connection in model as well

protected $connection = "redis";

Searching model by specific column

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);

Get all records keys

// return list of all records keys from redis
$post->getAllKeys();

// Return list of all records ids
$post->getAllKeys(true);

Add new searchBy fields

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

Managing model relations

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();

Auth

# In config/auth.php
'guards' => [
    'web' => [
        'driver' => 'session',
        'provider' => 'redis',  // use provider name as in laravel-redis.php config file
    ],
],

Changelog

Please see CHANGELOG for more information on what has changed recently.

Contributing

Please see CONTRIBUTING for details.

Credits

License

The MIT License (MIT). Please see License File for more information.

About

This package will help you use Redis as base database within the Laravel environment

License:MIT License


Languages

Language:PHP 100.0%