Skip to content

BelongsToMany Doesn't Cache When Using Laravel Nova #278

Closed
@mikebronner

Description

@mikebronner

I just want to share my experinece with this issue. I just faced the BelongsToMany relationship issue using laravel and Nova. it doesn't update redis on save or update or create using Nova. I have to flush redis to see changes made by laravel nova. But when i use disableCache() on my models, it's working fine. But almost all my models using BelongsToMany relationships. So no point to use this package if it's not going to cache my models :) It might be related with the events between nova and models? Maybe this package doesn't regionise Nova events to trigger updates on redis.

Details about the issue:
I got Work and Service models. It's many to many relationships. Work has many Services and Services has Many Works.

Models in project looks like:
Work Model

class Work extends Model implements HasMedia
{
    use HasImage, HasTranslate, Cachable;

    public $translatable = ['client', 'project'];
    protected $collection = 'slider';

    protected $casts = [
        'date' => 'datetime',
    ];

    public function services()
    {
        return $this->belongsToMany(Service::class);
    }
}

Service Model

class Service extends Model
{
    use HasTranslate, HasSlug, Cachable;

    public $translatable = ['name', 'description'];

    public function getSlugOptions(): SlugOptions
    {
        return SlugOptions::create()
            ->generateSlugsFrom('name')
            ->saveSlugsTo('slug');
    }

    public function works()
    {
        return $this->belongsToMany(Work::class);
    }
}

Works table

    public function up()
    {
        Schema::create('works', function (Blueprint $table) {
            $table->bigIncrements('id');
            $table->json('client');
            $table->json('project');
            $table->date('date');
            $table->string('country');
            $table->string('url')->nullable();
            $table->timestamps();
        });
    }

Services table

    public function up()
    {
        Schema::create('services', function (Blueprint $table) {
            $table->bigIncrements('id');
            $table->json('name');
            $table->json('description');
            $table->string('slug')->unique();
            $table->string('icon');
            $table->timestamps();
        });
    }

Service Work pivot table

    public function up()
    {
        Schema::create('service_work', function (Blueprint $table) {
            $table->bigIncrements('id');
            $table->integer('service_id')->unsigned();
            $table->integer('work_id')->unsigned();
        });
    }

Laravel : 5.8.30
Redis server v=5.0.5
Laravel Nova 2.0.9

I hope this help you to figure out the issue.

Update:
I just notice the key names in redis is a bit weird.

xxx:tag:genealabs:laravel-model-caching:mysql:nemrut:appservice:key,

i believe there should be : between app and service so it should looks like

xxx:tag:genealabs:laravel-model-caching:mysql:nemrut:app:service:key,

is key naming working correctly? maybe shoud check that out.

Originally posted by @nemrutco in #127 (comment)

Metadata

Metadata

Assignees

Labels

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions