/** * store a resource * @param Request $request http request * @param mixed $id id of the resource for updating * @return jsend jsend with newly stored source */ function store(Request $request, $id = null) { //////////////// // Load Data // //////////////// if ($id) { $data = Model::find($id); if (!$data) { return app()->abort(404); } } else { $data = new Model(); } /////////////////////////////////// // Assign posted data to Data // /////////////////////////////////// $data->fill($request->input()); /////////////////////////////////////////////////////////////////// // Validate data // /////////////////////////////////////////////////////////////////// if (!$data->is_valid) { return response()->json(JSend::fail($data->getErrors())->asArray()); } /////////// // Store // /////////// if ($data->save()) { return response()->json(JSend::success(['data' => $data])->asArray()); } else { return response()->json(JSend::fail($data->getErrors())->asArray()); } }
/** * Store a newly created resource in storage. * * @param \Illuminate\Http\Request $request * @return \Illuminate\Http\Response */ public function store(Request $request) { try { $inputs = array('des_name', 'des_coors', 'des_instruction', 'related_photos'); $obj = array('message' => 'succeeded'); foreach ($inputs as $field) { $obj[$field] = $request->input($field, ''); } // TODO: cuối cùng vẫn phải chạy Raw SQL -_- $destination = DB::table('Destination')->insertGetId(array('des_name' => $obj['des_name'], 'des_instruction' => $obj['des_instruction'], 'coordinate' => DB::raw("GeomFromText(\"POINT(" . $obj['des_coors']['latitude'] . " " . $obj['des_coors']['longitude'] . ")\")"))); // upload selected images too $images_uploaded = array(); $relPhotos = $obj['related_photos']; if ($relPhotos) { foreach ($relPhotos as $photo) { if ($photo['selected']) { $rq = Request::create("/admin/destination/{$destination}/photo", "POST", [], [], [], [], array('photo_url' => $photo['url'], 'photo_like' => rand(0, 100))); array_push($images_uploaded, Route::dispatch($rq)->getContent()); } } } return response()->json(array('addedObject' => Destination::find($destination), 'addedPhotos' => $images_uploaded)); } catch (\Exception $e) { return response()->json($e); } }
/** * store a resource * @param Request $request http request * @param mixed $id id of the resource for updating * @return jsend jsend with newly stored source */ function store(Request $request, $id = null) { //////////////// // Load Data // //////////////// if ($id) { $data = Model::find($id); if (!$data) { return app()->abort(404); } } else { $data = new Model(); } /////////////////////////////////// // Assign posted data to Data // /////////////////////////////////// $data->fill($request->input()); /////////////////////////////////////////////////////////////////// // Validate data // /////////////////////////////////////////////////////////////////// # Validate Destinations $destination_id = null; if ($request->input('destination_id') && is_string($request->input('destination_id'))) { $destination = Destination::find($request->input('destination_id')); if ($destination) { $destination_id = $request->input('destination_id'); } else { return response()->json(JSend::fail('InvalidDestination : Destination not found')->asArray()); } } elseif (!is_string($request->input('destination_id'))) { return response()->json(JSend::fail('InvalidDestination : Invalid Destination format')->asArray()); } else { return response()->json(JSend::fail('InvalidDestination : Destination is required')->asArray()); } /////////////////////////// // Embeds Other Document // /////////////////////////// $data->images = $request->input('images'); $data->tags = is_array($request->input('tags')) ? $request->input('tags') : [$request->input('tags')]; $data->destination_id = $destination_id; /////////// // Store // /////////// if ($data->save()) { return response()->json(JSend::success(['data' => $data])->asArray()); } else { return response()->json(JSend::fail($data->getErrors())->asArray()); } }