PageRenderTime 47ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/app/Http/Controllers/Admin/ProductsController.php

https://gitlab.com/marksuner/swann-portal
PHP | 192 lines | 120 code | 37 blank | 35 comment | 5 complexity | c17af344cf611ff1d77011a97d308a4d MD5 | raw file
  1. <?php
  2. namespace App\Http\Controllers\Admin;
  3. use Illuminate\Http\Request;
  4. use App\Models\Photo;
  5. use App\Models\Product;
  6. use App\Models\Category;
  7. use App\Models\Document;
  8. use App\Models\SpecificationCategory;
  9. use App\Http\Controllers\Controller;
  10. use App\Http\Requests\ProductRequest;
  11. use App\SwannPortal\ImageUpload;
  12. use App\SwannPortal\DocumentUpload;
  13. class ProductsController extends Controller
  14. {
  15. protected $products;
  16. protected $categories;
  17. protected $specificationCategories;
  18. public function __construct(
  19. Product $products,
  20. Category $categories,
  21. SpecificationCategory $specificationCategories
  22. )
  23. {
  24. $this->products = $products;
  25. $this->categories = $categories;
  26. $this->specificationCategories = $specificationCategories;
  27. }
  28. /**
  29. * Display a listing of the resource.
  30. *
  31. * @return \Illuminate\Http\Response
  32. */
  33. public function index(Request $request)
  34. {
  35. $q = $request->input('q')?: null;
  36. $model = $this->products->with('categories');
  37. if (!empty($q)) {
  38. $model = $model
  39. ->where('model_no', 'like', '%'. $q .'%')
  40. ->orWhere('name', 'like', '%' . $q . '%');
  41. }
  42. $products = $model->paginate(10);
  43. return view('admin.products.index', compact('products', 'q'));
  44. }
  45. /**
  46. * Show the form for creating a new resource.
  47. *
  48. * @return \Illuminate\Http\Response
  49. */
  50. public function create()
  51. {
  52. $categories = $this->categories->all();
  53. $specificationCategories = $this->specificationCategories->all();
  54. return view('admin.products.create', compact('categories', 'specificationCategories'));
  55. }
  56. /**
  57. * Store a newly created resource in storage.
  58. *
  59. * @param \Illuminate\Http\Request $request
  60. * @return \Illuminate\Http\Response
  61. */
  62. public function store(ProductRequest $request, Photo $photos, Document $documents)
  63. {
  64. $model = $request->input('model_no');
  65. $photo = (new ImageUpload($request, $photos))->handle();
  66. $document = (new DocumentUpload($request, $documents))->handle();
  67. $product = $this->products->create([
  68. 'photo_id' => $photo ? $photo->id : null,
  69. 'document_id' => $document ? $document->id : null,
  70. 'name' => $request->input('name'),
  71. 'model_no' => $model,
  72. 'description' => $request->input('description'),
  73. 'featured' => $request->input('featured')
  74. ]);
  75. $product->categories()->sync( $request->input('categories') );
  76. $product->specificationCategories()->sync( $request->input('specification_category') );
  77. $tags = collect();
  78. if (strpos($model, ',')) {
  79. $models = explode(',', $model);
  80. $tags = collect($models);
  81. } else {
  82. $tags = collect([$model]);
  83. }
  84. $tags->push($request->input('name'));
  85. $this->saveTag($tags, $product);
  86. return redirect(route('admin.products.index'))->with('status', 'Success on Creating new Product');
  87. }
  88. /**
  89. * Show the form for editing the specified resource.
  90. *
  91. * @param int $id
  92. * @return \Illuminate\Http\Response
  93. */
  94. public function edit($id)
  95. {
  96. $product = $this->products->findOrFail($id);
  97. $categories = $this->categories->all();
  98. $specificationCategories = $this->specificationCategories->all();
  99. return view('admin.products.edit', compact('product', 'categories', 'specificationCategories'));
  100. }
  101. /**
  102. * Update the specified resource in storage.
  103. *
  104. * @param \Illuminate\Http\Request $request
  105. * @param int $id
  106. * @return \Illuminate\Http\Response
  107. */
  108. public function update(ProductRequest $request, Photo $photos, Document $documents, $id)
  109. {
  110. $product = $this->products->findOrFail($id);
  111. $model = $request->input('model_no');
  112. $photoId = !is_null($product->photo_id) ? $product->photo_id : false;
  113. $documentId = !is_null($product->document_id) ? $product->document_id : false;
  114. $photo = (new ImageUpload($request, $photos, $photoId))->handle();
  115. $document = (new DocumentUpload($request, $documents, $documentId))->handle();
  116. $product->update([
  117. 'photo_id' => $photo ? $photo->id : null,
  118. 'document_id' => $document ? $document->id : null,
  119. 'name' => $request->input('name'),
  120. 'model_no' => $request->input('model_no'),
  121. 'description' => $request->input('description'),
  122. 'featured' => $request->input('featured')
  123. ]);
  124. $product->categories()->sync( $request->input('categories') );
  125. $product->specificationCategories()->sync( $request->input('specification_category') );
  126. $tags = collect();
  127. if (strpos($model, ',')) {
  128. $models = explode(',', $model);
  129. $tags = collect($models);
  130. } else {
  131. $tags = collect([$model]);
  132. }
  133. $tags->push($request->input('name'));
  134. $this->saveTag($tags, $product);
  135. return redirect(route('admin.products.index'))->with('status', 'Success on Updating Product');
  136. }
  137. /**
  138. * Remove the specified resource from storage.
  139. *
  140. * @param int $id
  141. * @return \Illuminate\Http\Response
  142. */
  143. public function destroy($id)
  144. {
  145. $this->products->findOrFail($id)->delete();
  146. return redirect(route('admin.products.index'))->with('status', 'Success on Deleting Product');
  147. }
  148. public function removeDocument($id)
  149. {
  150. $this->products->findOrFail($id)->update([
  151. 'document_id' => null
  152. ]);
  153. return redirect()->back()->with('status', 'Succesfully removed document');
  154. }
  155. }