Код: Выделить всё
Illuminate\Database\Eloquent\ModelNotFoundException: No query results for model [App\Models\Spbu]
Код: Выделить всё
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
{
//
}
}
Код: Выделить всё
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();
}
}
}
Подробнее здесь: https://stackoverflow.com/questions/792 ... n-observer
Мобильная версия