/** * @param array $attributes */ public function saveFromArray(array $attributes = array()) { $this->model->fill($attributes); $this->model->save(); $attributes['customer_id'] = $this->model->id; if (isset($attributes['images'])) { foreach ($attributes['images'] as $image) { $photoModel = new Models\Photo(); $params = ['customer_id' => $this->model->id, 'image' => $image]; $photoModel->getRepository()->saveFromArray($params); } } }
/** * @SWG\Api( * path="/photo/add", * @SWG\Operation( * nickname="Add new photo", * method="POST", * summary="Add new photo", * notes="Returns photo", * type="Photo", * authorizations={}, * @SWG\Parameter( * name="customer_id", * description="Customer ID", * required=true, * type="integer", * format="int64", * paramType="form", * allowMultiple=false * ), * @SWG\Parameter( * name="image", * description="Image", * type="file", * required=true, * allowMultiple=true, * paramType="body" * ), * @SWG\ResponseMessage(code=500, message="Internal server error") * ) * ) */ public function add() { $statusCode = 200; $inputs = \Input::all(); $validator = Validator::make($inputs, ['customer_id' => 'required|numeric|exists:customers,id', 'image' => 'required|image']); if ($validator->fails()) { $response = ['error' => $validator->errors()]; $statusCode = 500; } else { $params = ['customer_id' => $inputs['customer_id'], 'image' => $inputs['image']]; $photo = new Models\Photo(); $photo->getRepository()->saveFromArray($params); $photoView = new ModelViews\Photo($photo); $response = $photoView->get(); } return \Response::json($response, $statusCode); }