1. Go to this page and download the library: Download bilaliqbalr/laravel-redis 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/ */
bilaliqbalr / laravel-redis example snippets
public function prefix() : string
{
return 'post';
}
protected $connection = "redis";
protected $searchBy = [
'title',
];
$post = Post::searchByTitle("iphone");
# This works the same way as we do while using eloquent
# Post::where('title', 'iphone')->first();
# 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);
# 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
],
],