1pppp

Untitled

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