最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

php - No query results for model [AppModelsSpbu] when delete in observer - Stack Overflow

programmeradmin0浏览0评论

Create and update are working properly, but when I try to delete, the queue job always return fails. When I check the failed_jobs table, I see an exception

Illuminate\Database\Eloquent\ModelNotFoundException: No query results for model [App\Models\Spbu]

this is my observer code

class SpbuObserver
{
    /**
     * Handle the Spbu "created" event.
     */
    public function created(Spbu $spbu): void
    {
        SyncSpbu::dispatch($spbu, 'create');
    }

    /**
     * Handle the Spbu "updated" event.
     */
    public function updated(Spbu $spbu): void
    {
        SyncSpbu::dispatch($spbu, 'update');
    }

    /**
     * Handle the Spbu "deleted" event.
     */
    public function deleted(Spbu $spbu): void
    {
        SyncSpbu::dispatch($spbu, 'delete');
    }


    /**
     * Handle the Spbu "restored" event.
     */
    public function restored(Spbu $spbu): void
    {
        //
    }

    /**
     * Handle the Spbu "force deleted" event.
     */
    public function forceDeleted(Spbu $spbu): void
    {
        //
    }
}

and this my job code

class SyncSpbu implements ShouldQueue
{
    use Dispatchable, InteractsWithQueue, Queueable;

    protected $spbu;
    protected $action;

    /**
     * Create a new job instance.
     * @param Spbu $spbu
     * @param string $action (create|update|delete)
     */
    public function __construct(Spbu $spbu, string $action)
    {
        $this->spbu = $spbu;
        $this->action = $action;
    }

    /**
     * Execute the job.
     */
    public function handle()
    {
        $tenants = Tenant::all();

        foreach ($tenants as $tenant) {
            tenancy()->initialize($tenant);

            try {
                switch ($this->action) {
                    case 'create':
                    case 'update':
                        $tenantSpbu = TenantSpbu::updateOrCreate(
                            ['id' => $this->spbu->id],
                            [
                                'id' =>  $this->spbu->id,
                                'nama' => $this->spbu->nama,
                                'alamat' => $this->spbu->alamat,
                                'latitude' => $this->spbu->latitude,
                                'longitude' => $this->spbu->longitude,
                            ]
                        );
                        break;

                    case 'delete':
                        TenantSpbu::find($this->spbu->id)->delete();
                        break;
                }
            } catch (\Exception $e) {
                Log::error("Error syncing SPBU to tenant {$tenant->id}: " . $e->getMessage());
            }

            tenancy()->end();
        }
    }
}

I am using multi-tenant Laravel. I want the data in the central domain to be synchronized across all tenants whenever it is updated or created. However, the issue is that when I try to delete data, the queue job fails.

Create and update are working properly, but when I try to delete, the queue job always return fails. When I check the failed_jobs table, I see an exception

Illuminate\Database\Eloquent\ModelNotFoundException: No query results for model [App\Models\Spbu]

this is my observer code

class SpbuObserver
{
    /**
     * Handle the Spbu "created" event.
     */
    public function created(Spbu $spbu): void
    {
        SyncSpbu::dispatch($spbu, 'create');
    }

    /**
     * Handle the Spbu "updated" event.
     */
    public function updated(Spbu $spbu): void
    {
        SyncSpbu::dispatch($spbu, 'update');
    }

    /**
     * Handle the Spbu "deleted" event.
     */
    public function deleted(Spbu $spbu): void
    {
        SyncSpbu::dispatch($spbu, 'delete');
    }


    /**
     * Handle the Spbu "restored" event.
     */
    public function restored(Spbu $spbu): void
    {
        //
    }

    /**
     * Handle the Spbu "force deleted" event.
     */
    public function forceDeleted(Spbu $spbu): void
    {
        //
    }
}

and this my job code

class SyncSpbu implements ShouldQueue
{
    use Dispatchable, InteractsWithQueue, Queueable;

    protected $spbu;
    protected $action;

    /**
     * Create a new job instance.
     * @param Spbu $spbu
     * @param string $action (create|update|delete)
     */
    public function __construct(Spbu $spbu, string $action)
    {
        $this->spbu = $spbu;
        $this->action = $action;
    }

    /**
     * Execute the job.
     */
    public function handle()
    {
        $tenants = Tenant::all();

        foreach ($tenants as $tenant) {
            tenancy()->initialize($tenant);

            try {
                switch ($this->action) {
                    case 'create':
                    case 'update':
                        $tenantSpbu = TenantSpbu::updateOrCreate(
                            ['id' => $this->spbu->id],
                            [
                                'id' =>  $this->spbu->id,
                                'nama' => $this->spbu->nama,
                                'alamat' => $this->spbu->alamat,
                                'latitude' => $this->spbu->latitude,
                                'longitude' => $this->spbu->longitude,
                            ]
                        );
                        break;

                    case 'delete':
                        TenantSpbu::find($this->spbu->id)->delete();
                        break;
                }
            } catch (\Exception $e) {
                Log::error("Error syncing SPBU to tenant {$tenant->id}: " . $e->getMessage());
            }

            tenancy()->end();
        }
    }
}

I am using multi-tenant Laravel. I want the data in the central domain to be synchronized across all tenants whenever it is updated or created. However, the issue is that when I try to delete data, the queue job fails.

Share Improve this question asked Nov 20, 2024 at 6:17 Ardhan NovealdioArdhan Novealdio 131 silver badge2 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 0

Once your 'deleted' event gets triggered the model is already gone. Hence the model not found error because you are deleting the model and then passing it into the job constructor.

You can solve this by passing the id (assuming the id is the same for all tenants) of your model and retrieving the model in the job itself.

Documentation:

When injecting an Eloquent model into a job, the model is automatically serialized before being placed on the queue and re-retrieved from the database when the job is processed. However, if the model has been deleted while the job was waiting to be processed by a worker, your job may fail with a ModelNotFoundException.

https://laravel/docs/11.x/queues#ignoring-missing-models

发布评论

评论列表(0)

  1. 暂无评论