paginate(15); return view('backend.product.reviews.index', compact('reviews')); } /** * Show the form for creating a new resource. * * @return \Illuminate\Http\Response */ public function create() { // } /** * Store a newly created resource in storage. * * @param \Illuminate\Http\Request $request * @return \Illuminate\Http\Response */ public function store(Request $request) { $review = new Review; $review->product_id = $request->product_id; $review->user_id = Auth::user()->user_type == 'admin'?$request->user_id:Auth::user()->id; $review->rating = $request->rating; $review->comment = $request->comment; $review->viewed = '0'; $review->save(); $product = Product::findOrFail($request->product_id); if(Review::where('product_id', $product->id)->where('status', 1)->count() > 0){ $product->rating = Review::where('product_id', $product->id)->where('status', 1)->sum('rating')/Review::where('product_id', $product->id)->where('status', 1)->count(); } else { $product->rating = 0; } $product->save(); if($product->added_by == 'seller'){ $seller = $product->user->shop; $seller->rating = (($seller->rating*$seller->num_of_reviews)+$review->rating)/($seller->num_of_reviews + 1); $seller->num_of_reviews += 1; $seller->save(); } flash(translate('Review has been submitted successfully'))->success(); return back(); } /** * Display the specified resource. * * @param int $id * @return \Illuminate\Http\Response */ public function show($id) { // } /** * Show the form for editing the specified resource. * * @param int $id * @return \Illuminate\Http\Response */ public function edit($id) { // } /** * Update the specified resource in storage. * * @param \Illuminate\Http\Request $request * @param int $id * @return \Illuminate\Http\Response */ public function update(Request $request, $id) { // } /** * Remove the specified resource from storage. * * @param int $id * @return \Illuminate\Http\Response */ public function destroy($id) { // } public function updatePublished(Request $request) { $review = Review::findOrFail($request->id); $review->status = $request->status; $review->save(); $product = Product::findOrFail($review->product->id); if(Review::where('product_id', $product->id)->where('status', 1)->count() > 0){ $product->rating = Review::where('product_id', $product->id)->where('status', 1)->sum('rating')/Review::where('product_id', $product->id)->where('status', 1)->count(); } else { $product->rating = 0; } $product->save(); if($product->added_by == 'seller'){ $seller = $product->user->shop; if ($review->status) { $seller->rating = (($seller->rating*$seller->num_of_reviews)+$review->rating)/($seller->num_of_reviews + 1); $seller->num_of_reviews += 1; } else { $seller->rating = (($seller->rating*$seller->num_of_reviews)-$review->rating)/max(1, $seller->num_of_reviews - 1); $seller->num_of_reviews -= 1; } $seller->save(); } return 1; } public function product_review_modal(Request $request){ $product = Product::where('id',$request->product_id)->first(); $review = Review::where('user_id',Auth::user()->id)->where('product_id',$product->id)->first(); return view('frontend.user.product_review_modal', compact('product','review')); } public function product_admin_review_modal(Request $request){ $product = Product::where('id',$request->product_id)->first(); $customers = User::where('user_type', 'customer')->where('email_verified_at', '!=', null)->orderBy('created_at', 'desc')->get(); return view('frontend.user.product_admin_review_modal', compact('product','customers')); } }