booting(...$arguments); $this->bootCreate(...$arguments); $this->bootUpdate(...$arguments); $this->bootDelete(...$arguments); $this->booted(...$arguments); } public function booting(...$arguments): void { // } public function bootCreate(...$arguments): void { if (! $this instanceof ShouldCreate) { return; } $request = $this->getRequestInstance($arguments[0]); if ($request instanceof Collection) { $this->request = $request; } if ($this instanceof HasOwner) { $this->setOwner(); } if ($this instanceof HasSource) { $this->setSource(); } } public function bootUpdate(...$arguments): void { if (! $this instanceof ShouldUpdate) { return; } if ($arguments[0] instanceof Model) { $this->model = $arguments[0]; } $request = $this->getRequestInstance($arguments[1]); if ($request instanceof Collection) { $this->request = $request; } } public function bootDelete(...$arguments): void { if (! $this instanceof ShouldDelete) { return; } if ($arguments[0] instanceof Model) { $this->model = $arguments[0]; } } public function booted(...$arguments): void { // } /** * Check if request is array and if so, convert to a request class. * * @param mixed $request * @return \Illuminate\Foundation\Http\FormRequest * * @deprecated Request is not serializable so can't use it with queues. */ public function getRequestInstance($request) { return $this->getRequestAsCollection($request); } /** * Covert the request to collection. * * @param mixed $request * @return \Illuminate\Support\Collection */ public function getRequestAsCollection($request) { if (is_array($request)) { $data = $request; $request = new class() extends FormRequest {}; $request->merge($data); } return collect($request->all()); } public function setOwner(): void { if (! $this->request instanceof Collection) { return; } if ($this->request->has('created_by')) { return; } $this->request->merge(['created_by' => user_id()]); } public function setSource(): void { if (! $this->request instanceof Collection) { return; } if ($this->request->has('created_from')) { return; } $this->request->merge(['created_from' => $this->getSourceName($this->request)]); } }