/** * Captures the Inventory models restored event * and cascades the restore to all of it's stocks. * * @param Inventory $model */ public function restored(Inventory $model) { $stocks = $model->stocks()->onlyTrashed()->get(); if (count($stocks) > 0) { foreach ($stocks as $stock) { $stock->restore(); } } }
/** * Creates an item record. * * @return Inventory */ public function create() { $this->dbStartTransaction(); try { // Set insert data $insert = ['category_id' => $this->getInput('category_id'), 'user_id' => $this->sentry->getCurrentUserId(), 'metric_id' => $this->getInput('metric'), 'name' => $this->getInput('name', null, true), 'description' => $this->getInput('description', null, true)]; // If the record is created, return it, otherwise return false $record = $this->model->create($insert); if ($record) { // Fire created event $this->fireEvent('maintenance.inventory.created', ['item' => $record]); $this->dbCommitTransaction(); return $record; } } catch (\Exception $e) { $this->dbRollbackTransaction(); } return false; }
<?php use Stevebauman\Maintenance\Models\Inventory; use Stevebauman\Maintenance\Models\InventoryStock; // The inventory observers. Inventory::observe(new \Stevebauman\Maintenance\Handlers\Observers\Inventory\Observer()); InventoryStock::observe(new \Stevebauman\Maintenance\Handlers\Observers\Inventory\StockObserver());