public function store2(Request $request) { $validator = Validator::make($request->all(), ['drama_id' => 'required', 'content' => 'required_with:title', 'visible' => 'required_with:content', 'type' => 'required|in:0,1,2,3,4', 'rating' => 'in:0,0.5,1,1.5,2,2.5,3,3.5,4,4.5,5', 'title' => 'max:255']); if ($validator->fails()) { return response($validator->messages(), 422); } $favorite = new Favorite(); $favorite->user_id = $request->user()->id; $favorite->drama_id = $request->input('drama_id'); $favorite->type = $request->input('type'); if ($favorite->type == 0) { $favorite->rating = 0; } else { $favorite->rating = $request->input('rating'); } $favorite->tags = $request->input('tags'); if ($favorite->save()) { DB::table('users')->where('id', $favorite->user_id)->increment('favorite' . $favorite->type); DB::table('dramas')->where('id', $favorite->drama_id)->increment('favorites'); if ($request->input('tags')) { $tagmaps = []; $tags = explode(',', $request->input('tags')); foreach ($tags as $tag) { $tagmaps[] = ['drama_id' => $favorite->drama_id, 'user_id' => $favorite->user_id, 'tag_id' => Tag::firstOrCreate(['name' => $tag])->id]; } DB::table('tagmaps')->insert($tagmaps); } if ($request->has('content')) { $review = new Review(); $review->user_id = $favorite->user_id; $review->drama_id = $favorite->drama_id; $review->title = $request->input('title'); $review->content = $request->input('content'); $review->visible = $request->input('visible'); if ($review->save()) { DB::table('users')->where('id', $review->user_id)->increment('reviews'); DB::table('dramas')->where('id', $review->drama_id)->increment('reviews'); } else { return response('收藏添加成功,评论添加失败', 422); } } return ['result' => 'success']; } else { return response('添加失败', 422); } }
public function create($id) { $favo_product = \DB::table('favorite')->where('product_id', '=', $id)->where('user_id', '=', Auth::user()->id)->first(); if (is_null($favo_product)) { $created_at = Carbon::now('Asia/Kuala_lumpur'); $updated_at = Carbon::now('Asia/Kuala_lumpur'); $favorite = new Favorite(); $favorite->user_id = Auth::user()->id; $favorite->product_id = $id; $favorite->created_at = $created_at; $favorite->updated_at = $updated_at; $favorite->save(); Session::flash('alert-success', 'Product successfully add to your favorite list'); return Redirect::home(); } else { return 'exist'; } }
public function store2(Request $request) { $this->validate($request, ['drama_id' => 'required', 'content' => 'required_with:title', 'visible' => 'required_with:content', 'type' => 'required|in:0,1,2,3,4', 'rating' => 'in:0,0.5,1,1.5,2,2.5,3,3.5,4,4.5,5', 'title' => 'max:255']); $favorite = new Favorite(); $favorite->user_id = $request->user()->id; $favorite->drama_id = $request->input('drama_id'); $favorite->type = $request->input('type'); if ($favorite->type == 0) { $favorite->rating = 0; } else { $favorite->rating = $request->input('rating'); } $favorite->tags = $request->input('tags'); if ($favorite->save()) { DB::table('users')->where('id', $favorite->user_id)->increment('favorite' . $favorite->type); DB::table('dramas')->where('id', $favorite->drama_id)->increment('favorites'); if ($request->input('tags')) { $tagmaps = []; $tags = explode(',', $request->input('tags')); foreach ($tags as $tag) { $tagmaps[] = ['drama_id' => $favorite->drama_id, 'user_id' => $favorite->user_id, 'tag_id' => Tag::firstOrCreate(['name' => $tag])->id]; } DB::table('tagmaps')->insert($tagmaps); } //评论内容不为空则新建评论 if ($request->has('content')) { $review = new Review(); $review->user_id = $favorite->user_id; $review->drama_id = $favorite->drama_id; $review->title = $request->input('title'); $review->content = $request->input('content'); $review->visible = $request->input('visible'); if ($review->save()) { DB::table('users')->where('id', $review->user_id)->increment('reviews'); DB::table('dramas')->where('id', $review->drama_id)->increment('reviews'); } else { return redirect()->back()->withInput()->withErrors('收藏添加成功,评论添加失败'); } } return redirect()->route('drama.show', [$favorite->drama_id]); } else { return redirect()->back()->withInput()->withErrors('添加失败'); } }
/** * Show the form for creating a new resource. * * @return Response */ public function create($pid) { $this->msg = "添加失败"; //如果购物车不存在该物品,则添加到购物车 if (!$this->favoriteService->exsitObjByUPid($this->user->id, $pid)) { $now = time(); $fid = $pid . $now . rand(10000, 99999); $favorite = new Favorite(); $favorite->pid = $pid; $favorite->uid = $this->user->id; $favorite->fid = $fid; $favorite->ftime = $now; $favorite->save(); $this->msg = "添加成功"; } if (view()->exists('favorite.list')) { // return redirect('/favorite/list')->with('msg',$this->msg); return redirect()->back()->with('msg', $this->msg); } else { return redirect('home'); } }