Sid Gifari File Manager
🏠 Root
/
home2
/
meumer25
/
painebistroemcasa.meumercado.app
/
app
/
CentralLogics
/
Editing: product.php
<?php namespace App\CentralLogics; use App\Model\Product; use App\Model\Review; class ProductLogic { public static function get_product($id) { return Product::active()->withCount(['wishlist'])->with(['rating'])->where('id', $id)->first(); } public static function get_latest_products($limit = 10, $offset = 1) { $paginator = Product::active()->withCount(['wishlist'])->with(['rating'])->latest()->paginate($limit, ['*'], 'page', $offset); /*$paginator->count();*/ return [ 'total_size' => $paginator->total(), 'limit' => $limit, 'offset' => $offset, 'products' => $paginator->items() ]; } public static function get_related_products($product_id) { $product = Product::find($product_id); return Product::active()->withCount(['wishlist'])->with(['rating'])->where('category_ids', $product->category_ids) ->where('id', '!=', $product->id) ->limit(10) ->get(); } public static function search_products($name, $limit = 10, $offset = 1) { $key = explode(' ', $name); $paginator = Product::active()->withCount(['wishlist'])->with(['rating'])->where(function ($q) use ($key) { foreach ($key as $value) { $q->orWhere('name', 'like', "%{$value}%"); } })->paginate($limit, ['*'], 'page', $offset); return [ 'total_size' => $paginator->total(), 'limit' => $limit, 'offset' => $offset, 'products' => $paginator->items() ]; } public static function get_product_review($id) { $reviews = Review::where('product_id', $id)->get(); return $reviews; } public static function get_rating($reviews) { $rating5 = 0; $rating4 = 0; $rating3 = 0; $rating2 = 0; $rating1 = 0; foreach ($reviews as $key => $review) { if ($review->rating == 5) { $rating5 += 1; } if ($review->rating == 4) { $rating4 += 1; } if ($review->rating == 3) { $rating3 += 1; } if ($review->rating == 2) { $rating2 += 1; } if ($review->rating == 1) { $rating1 += 1; } } return [$rating5, $rating4, $rating3, $rating2, $rating1]; } public static function get_overall_rating($reviews) { $totalRating = count($reviews); $rating = 0; foreach ($reviews as $key => $review) { $rating += $review->rating; } if ($totalRating == 0) { $overallRating = 0; } else { $overallRating = number_format($rating / $totalRating, 2); } return [$overallRating, $totalRating]; } }
Save
Cancel