ReviewController.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. <?php
  2. namespace App\Http\Controllers;
  3. use App\Models\User;
  4. use Illuminate\Http\Request;
  5. use App\Models\Review;
  6. use App\Models\Product;
  7. use Auth;
  8. class ReviewController extends Controller
  9. {
  10. /**
  11. * Display a listing of the resource.
  12. *
  13. * @return \Illuminate\Http\Response
  14. */
  15. public function index(Request $request)
  16. {
  17. $reviews = Review::orderBy('created_at', 'desc')->paginate(15);
  18. return view('backend.product.reviews.index', compact('reviews'));
  19. }
  20. /**
  21. * Show the form for creating a new resource.
  22. *
  23. * @return \Illuminate\Http\Response
  24. */
  25. public function create()
  26. {
  27. //
  28. }
  29. /**
  30. * Store a newly created resource in storage.
  31. *
  32. * @param \Illuminate\Http\Request $request
  33. * @return \Illuminate\Http\Response
  34. */
  35. public function store(Request $request)
  36. {
  37. $review = new Review;
  38. $review->product_id = $request->product_id;
  39. $review->user_id = Auth::user()->user_type == 'admin'?$request->user_id:Auth::user()->id;
  40. $review->rating = $request->rating;
  41. $review->comment = $request->comment;
  42. $review->viewed = '0';
  43. $review->save();
  44. $product = Product::findOrFail($request->product_id);
  45. if(Review::where('product_id', $product->id)->where('status', 1)->count() > 0){
  46. $product->rating = Review::where('product_id', $product->id)->where('status', 1)->sum('rating')/Review::where('product_id', $product->id)->where('status', 1)->count();
  47. }
  48. else {
  49. $product->rating = 0;
  50. }
  51. $product->save();
  52. if($product->added_by == 'seller'){
  53. $seller = $product->user->shop;
  54. $seller->rating = (($seller->rating*$seller->num_of_reviews)+$review->rating)/($seller->num_of_reviews + 1);
  55. $seller->num_of_reviews += 1;
  56. $seller->save();
  57. }
  58. flash(translate('Review has been submitted successfully'))->success();
  59. return back();
  60. }
  61. /**
  62. * Display the specified resource.
  63. *
  64. * @param int $id
  65. * @return \Illuminate\Http\Response
  66. */
  67. public function show($id)
  68. {
  69. //
  70. }
  71. /**
  72. * Show the form for editing the specified resource.
  73. *
  74. * @param int $id
  75. * @return \Illuminate\Http\Response
  76. */
  77. public function edit($id)
  78. {
  79. //
  80. }
  81. /**
  82. * Update the specified resource in storage.
  83. *
  84. * @param \Illuminate\Http\Request $request
  85. * @param int $id
  86. * @return \Illuminate\Http\Response
  87. */
  88. public function update(Request $request, $id)
  89. {
  90. //
  91. }
  92. /**
  93. * Remove the specified resource from storage.
  94. *
  95. * @param int $id
  96. * @return \Illuminate\Http\Response
  97. */
  98. public function destroy($id)
  99. {
  100. //
  101. }
  102. public function updatePublished(Request $request)
  103. {
  104. $review = Review::findOrFail($request->id);
  105. $review->status = $request->status;
  106. $review->save();
  107. $product = Product::findOrFail($review->product->id);
  108. if(Review::where('product_id', $product->id)->where('status', 1)->count() > 0){
  109. $product->rating = Review::where('product_id', $product->id)->where('status', 1)->sum('rating')/Review::where('product_id', $product->id)->where('status', 1)->count();
  110. }
  111. else {
  112. $product->rating = 0;
  113. }
  114. $product->save();
  115. if($product->added_by == 'seller'){
  116. $seller = $product->user->shop;
  117. if ($review->status) {
  118. $seller->rating = (($seller->rating*$seller->num_of_reviews)+$review->rating)/($seller->num_of_reviews + 1);
  119. $seller->num_of_reviews += 1;
  120. }
  121. else {
  122. $seller->rating = (($seller->rating*$seller->num_of_reviews)-$review->rating)/max(1, $seller->num_of_reviews - 1);
  123. $seller->num_of_reviews -= 1;
  124. }
  125. $seller->save();
  126. }
  127. return 1;
  128. }
  129. public function product_review_modal(Request $request){
  130. $product = Product::where('id',$request->product_id)->first();
  131. $review = Review::where('user_id',Auth::user()->id)->where('product_id',$product->id)->first();
  132. return view('frontend.user.product_review_modal', compact('product','review'));
  133. }
  134. public function product_admin_review_modal(Request $request){
  135. $product = Product::where('id',$request->product_id)->first();
  136. $customers = User::where('user_type', 'customer')->where('email_verified_at', '!=', null)->orderBy('created_at', 'desc')->get();
  137. return view('frontend.user.product_admin_review_modal', compact('product','customers'));
  138. }
  139. }