ProductController.php 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. <?php
  2. namespace App\Http\Controllers\Front;
  3. use App\Shop\Products\Product;
  4. use App\Shop\Products\Repositories\Interfaces\ProductRepositoryInterface;
  5. use App\Http\Controllers\Controller;
  6. use App\Shop\Products\Transformations\ProductTransformable;
  7. class ProductController extends Controller
  8. {
  9. use ProductTransformable;
  10. /**
  11. * @var ProductRepositoryInterface
  12. */
  13. private $productRepo;
  14. /**
  15. * ProductController constructor.
  16. * @param ProductRepositoryInterface $productRepository
  17. */
  18. public function __construct(ProductRepositoryInterface $productRepository)
  19. {
  20. $this->productRepo = $productRepository;
  21. }
  22. /**
  23. * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
  24. */
  25. public function search()
  26. {
  27. if (request()->has('q') && request()->input('q') != '') {
  28. $list = $this->productRepo->searchProduct(request()->input('q'));
  29. } else {
  30. $list = $this->productRepo->listProducts();
  31. }
  32. $products = $list->where('status', 1)->map(function (Product $item) {
  33. return $this->transformProduct($item);
  34. });
  35. return view('front.products.product-search', [
  36. 'products' => $this->productRepo->paginateArrayResults($products->all(), 10)
  37. ]);
  38. }
  39. /**
  40. * Get the product
  41. *
  42. * @param string $slug
  43. * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
  44. */
  45. public function show(string $slug)
  46. {
  47. $product = $this->productRepo->findProductBySlug(['slug' => $slug]);
  48. $images = $product->images()->get();
  49. $category = $product->categories()->first();
  50. $productAttributes = $product->attributes;
  51. return view('front.products.product', compact(
  52. 'product',
  53. 'images',
  54. 'productAttributes',
  55. 'category'
  56. ));
  57. }
  58. }