Advertisement
1pppp

Untitled

Aug 17th, 2020
117
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.59 KB | None | 0 0
  1.  
  2. public function edit($id)
  3. {
  4. $categories = Category::whereNull('category_id')->with('childrenCategories')->get();
  5. $products = Product::where('id',$id)->first();
  6.  
  7. return view('product.edit', compact('categories','products'));
  8.  
  9. }
  10.  
  11.  
  12.  
  13. public function edit_store(Request $request, $id)
  14. {
  15. // dd('stop');
  16.  
  17. $this->validate($request, [
  18. 'title' => 'required',
  19. 'slug' => 'required|unique:products',
  20. 'text' => 'required',
  21. 'path.*' => 'nullable|image',
  22. ]);
  23.  
  24. $product = Product::find($id);
  25. $product->title = $request->input('title');
  26. $product->slug = $request->input('slug');
  27. $product->text = $request->input('text');
  28. $product->keywords = $request->input('keywords');
  29. $product->description = $request->input('description');
  30. $product->published = $request->input('published');
  31. $product->category_id = $request->input('category_id');
  32. $product->price = $request->input('price');
  33. $product->authorized_price = $request->input('authorized_price');
  34. $product->short_description = $request->input('short_description');
  35. $product->save();
  36. $path =public_path().'/uploads/product_images/';
  37. $file = $request->file('path');
  38. foreach ($file as $f) {
  39. $filename = Str::random(20) .'.' . $f->getClientOriginalExtension() ?: 'png';
  40. $img = ImageInt::make($f);
  41. $img->resize(500,500)->save($path . $filename);
  42. $image = new Image();
  43. $image->path = '/uploads/product_images/'.$filename;
  44. $image->title = $request->input('title');
  45. $image->product_id = $product->id;
  46. $image->save();
  47. }
  48. return redirect('/product/edit/'.$product->id)->with('info', 'Данные сохранены');
  49. }
  50.  
  51.  
  52.  
  53. public function destroy(Image $image)
  54. {
  55. Storage::disk('public/uploads/product_images/')->delete($image->path);
  56. $image->delete();
  57. return redirect()->route('product.edit', ['id' => $image->product_id]);
  58. }
  59.  
  60.  
  61.  
  62.  
  63. <input type="file" name="path[]" id="path[]" multiple accept="image/*">
  64. <div id="for_preview_uploads">
  65. </div>
  66. {{-- <div>--}}
  67. {{-- строка ниже--}}
  68. {{-- </div>--}}
  69. <script>
  70. function resizeImage(img) {
  71.  
  72. const W = parseInt(img.width / 4);
  73. const H = parseInt(img.height / 4);
  74.  
  75. const canvas = document.createElement("canvas");
  76. canvas.width = W;
  77. canvas.height = H;
  78.  
  79. const ctx = canvas.getContext("2d");
  80. ctx.drawImage(img, 0, 0, W, H);
  81.  
  82. const resizedImg = new Image();
  83. resizedImg.src = canvas.toDataURL('image/jpeg', 1);
  84. //document.body.append(resizedImg);
  85. document.querySelector("#for_preview_uploads").append(resizedImg);
  86.  
  87. }
  88.  
  89. function handleFiles(e) {
  90.  
  91. for (const file of this.files) {
  92.  
  93. const img = document.createElement("img");
  94. const reader = new FileReader();
  95.  
  96. reader.addEventListener("load", (e) => {
  97. img.addEventListener("load", (e) => {
  98. resizeImage(img);
  99. });
  100. img.src = e.target.result;
  101. });
  102.  
  103. reader.readAsDataURL(file);
  104.  
  105. }
  106.  
  107. }
  108.  
  109. const fileInput = document.getElementById("path[]");
  110.  
  111. fileInput.addEventListener("change", handleFiles, false);
  112.  
  113.  
  114. </script>
  115. {{-- <img src="{{$image->path }} " alt="{{$image->title}}">--}}
  116. @forelse ($products as $product)
  117. <div class="products card" style="width: 18rem;">
  118. <div class="card-body">
  119. <p class="card-images">
  120. <img src="{{$image->path }} " alt="{{$image->title}}">
  121. {{-- @forelse ($product->images as $image)--}}
  122. {{-- <img src="{{$image->path }} " alt="{{$image->title}}">--}}
  123. {{-- @empty--}}
  124. {{-- Нет фотографий--}}
  125. {{-- @endforelse--}}
  126. </p>
  127. </div>
  128. </div>
  129. @empty
  130. <div>Нет товаров</div>
  131. @endforelse
  132.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement