// Look at the DB::transaction being used inside the job, to avoid half-failures during the job
// Also, in this project, all Jobs interact with the Request
// You can take a look deeper into getRequestInstance in app/Abstracts/Job.php
// Final note: this job, in turn, calls several more jobs for the Taxes of each created item
use App\Abstracts\Job;
use App\Jobs\Common\CreateItemTaxes;
use App\Models\Common\Item;
class CreateItem extends Job
{
protected $item;
protected $request;
public function __construct($request)
{
$this->request = $this->getRequestInstance($request);
}
public function handle()
{
\DB::transaction(function () {
$this->item = Item::create($this->request->all());
if ($this->request->file('picture')) {
$media = $this->getMedia($this->request->file('picture'), 'items');
$this->item->attachMedia($media, 'picture');
}
$this->dispatch(new CreateItemTaxes($this->item, $this->request));
});
return $this->item;
}
}