/** * Saves all the image attachments to the DB */ public function saveImageAttachments(Request $request) { //get array of all types('context') of attachments for this model $this->attachmentNames = $this->getImageAttachmentNames(); //first of all create the index of all input attachments foreach ($request->all() as $key => $input) { $this->addToInputIndex($key); } //go through index and save them to database foreach ($this->attachmentNames as $context) { if (isset($this->inputIndex[$context])) { $priority = 0; foreach ($this->inputIndex[$context]['identifiers'] as $attachmentID) { $field = $context . '_' . $attachmentID . '_'; //if no id feild, then create a new attachment if ($request->get($field . 'id') == null) { Attachment::create(['context' => $context, 'url' => $request->get($field . 'url'), 'alt' => $request->get($field . 'alt'), 'caption' => $request->get($field . 'caption'), 'status' => 'active', 'model' => get_class(), 'model_id' => $this->id, 'priority' => $priority]); } else { if ($request->get($field . 'url') == '' && $request->get($field . 'caption') == '' && $request->get($field . 'alt') == '') { Attachment::destroy($request->get($field . 'id')); } else { $attachment = Attachment::find($request->get($field . 'id')); $attachment->update(['context' => $context, 'url' => $request->get($field . 'url'), 'alt' => $request->get($field . 'alt'), 'caption' => $request->get($field . 'caption'), 'status' => 'active', 'priority' => $priority], ['id' => $request->get($field . 'id')]); } } $priority++; } } } }