123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158 |
- <?php
- namespace App\Http\Controllers;
- use App\Models\User;
- use Illuminate\Http\Request;
- use App\Models\Review;
- use App\Models\Product;
- use Auth;
- class ReviewController extends Controller
- {
- /**
- * Display a listing of the resource.
- *
- * @return \Illuminate\Http\Response
- */
- public function index(Request $request)
- {
-
- $reviews = Review::orderBy('created_at', 'desc')->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'));
- }
- }
|