/** * Show the form for creating a new resource. * * @return \Illuminate\Http\Response */ public function create(Request $request) { //读取内容 $photo = $request->file('photo'); $extension = strtolower($photo->getClientOriginalExtension()); $input = $request->all(); //save image //get imageurl //save model if ($request->file('photo')->isValid()) { $name = md5(microtime() . $input['name']) . '.' . $extension; $path = 'img/goods'; $request->file('photo')->move($path, $name); $image_url = $path . '/' . $name; $goods = new Goods(); $goods->name = $input['name']; $goods->description = $input['description']; $goods->user_id = Auth::User()->id; $goods->type = $input['type']; $goods->price = $input['price']; $goods->status = 0; $goods->image_url = $image_url; $goods->save(); return redirect('/goods/' . $goods->id . '/detail')->with('message', 'Information successfully recorded'); } }
private function retrieveGoods($commodityId, $specification) { // match $commodity = Commodity::find($commodityId); $goods = $commodity->goods->where('specification', $specification); // constraint error if ($goods->count() > 0) { throw new \Exception('Commodities and specification unique constraint error!'); } // create Goods if not exist if ($goods->isEmpty()) { $goods = new Goods(); $goods->commodity()->associate($commodity); $goods->specification = $specification; $goods->save(); } return $goods; }