/** * store a resource * @param Request $this->request http request * @param mixed $id id of the resource for updating * @return jsend jsend with newly stored source */ public function store($id = null) { //////////////// // Load Data // //////////////// if ($id) { $data = Model::find($id); if (!$data) { return app()->abort(404); } } else { $data = new Model(); } ///////////////// // Parse Input // ///////////////// $input = $this->request->input(); /////////////////////////////////// // Assign posted data to Data // /////////////////////////////////// $data->fill($this->request->input()); $data->slug = Model::generateSlug($data->name, $data->_id); /////////////////////////////////////////////////////////////////// // Validate data // /////////////////////////////////////////////////////////////////// /////////////////////// // EMBED IMAGES // /////////////////////// foreach ($this->request->input('images') as $x) { $images[] = new Image($x); } if (!$data->syncImages($images)) { return response()->json(JSend::fail($data->getErrors())->asArray())->setCallback($this->request->input('callback')); } /////////////////////// // EMBED ADDRESS // /////////////////////// foreach ($this->request->input('addresses') as $x) { $addresses[] = new Address($x); } if (!$data->syncAddresses($addresses)) { return response()->json(JSend::fail($data->getErrors())->asArray())->setCallback($this->request->input('callback')); } /////////// // Store // /////////// if ($data->save()) { return response()->json(JSend::success(['data' => $data])->asArray())->setCallback($this->request->input('callback')); } else { return response()->json(JSend::fail($data->getErrors())->asArray())->setCallback($this->request->input('callback')); } }