1. Go to this page and download the library: Download nailfor/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/ */
'connections' => [
...
'redis' => [ //the name of connection in your models(default)
'driver' => 'redis',
],
namespace App\Models;
use App\Models\DbCategory;
use App\Models\RdbBrand;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\HasOne;
class DbProduct extends Model
{
protected $table = 'product';
//sql to sql relationship
public function Category(): BelongsTo
{
return $this->belongsTo(DbCategory::class, 'category_id');
}
//sql to redis relationship
public function Brand(): HasOne
{
return $this->hasOne(RdbBrand::class, 'brand_id');
}
}
namespace App\Models;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use nailfor\Redis\Eloquent\Model;
class RdbProduct extends Model
{
protected $table = 'product';
protected $fillable = [
'id',
'name',
'article',
'brand_id',
'category_id',
];
//Since the model type is HSET, all fields are stored as a string.
protected $casts = [
'id' => 'integer',
'brand_id' => 'integer',
];
//redis to redis relationship
public function Brand(): BelongsTo
{
return $this->belongsTo(RdbBrand::class, 'brand_id');
}
//redis to sql relationship
public function Category(): BelongsTo
{
return $this->belongsTo(DbCategory::class, 'category_id');
}
}
namespace App\Models;
use nailfor\Redis\Eloquent\Model;
class RdbBrand extends Model
{
//without var $table the key will be "rdb_brand"
}
//get all products
$products = RdbProduct::with([
'Brand',
])
->get()
;
//get only id 1,2,3...
$products = RdbProduct::with([
'Brand',
'Category',
])
->whereIn('id', [1,2,3])
->get()
;
foreach($products as $product) {
$brand = $product->Brand; //relation to Redis model RdbBrand
$category = $product->Category; //relation to SQL model DbCategory
//of course u can modify this models here
$brand->type = 'sometype';
$brand->save();
}
//truncate all product:*
RdbProduct::delete();
//deleting by condition
RdbProduct::where('brand_id', 4)->delete();
RdbProduct::where('id', 2)
->orWhere('brand_id', 5)
->delete();
RdbProduct::where('article', 'VGX-01')
->whereIn('brand_id', [1,2,4])
->delete()
;
//deleting single model
$model = RdbProduct::find(1);
$model->delete();