1pppp

Untitled

Aug 7th, 2020
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.23 KB | None | 0 0
  1.  
  2. public function create(Request $request)
  3. {
  4. $categories = Category::whereNull('category_id')->with('childrenCategories')->get();
  5.  
  6. return view('product.create', compact('categories' ));
  7. }
  8.  
  9.  
  10. public function store(Request $request)
  11. {
  12. // dd('stop');
  13.  
  14. $this->validate($request, [
  15. 'title' => 'required',
  16. 'slug' => 'required|unique:products',
  17. 'text' => 'required',
  18. 'path' => 'nullable|image',
  19.  
  20. ]);
  21.  
  22. $product = new Product();
  23. $product->title = $request->input('title');
  24. $product->slug = $request->input('slug');
  25. $product->text = $request->input('text');
  26. $product->keywords = $request->input('keywords');
  27. $product->description = $request->input('description');
  28. $product->published = $request->input('published');
  29. $product->category_id = $request->input('category_id');
  30. // $product->product_id = $request->input('product_id');
  31. $product->price = $request->input('price');
  32. $product->authorized_price = $request->input('authorized_price');
  33. // $product->path = $request->input('path');
  34. $product->short_description = $request->input('short_description');
  35. $product->save();
  36. // dd('stop');
  37.  
  38.  
  39. $path =public_path().'uploads/product_images';
  40. $file = $request->file('file');
  41. // dd($path);
  42.  
  43. foreach ($file as $f) {
  44. $filename = str_random(20) .'.' . $f->getClientOriginalExtension() ?: 'png';
  45. $img = ImageInt::make($f);
  46. $img->resize(500,500)->save($path . $filename);
  47. Image::create(['title' => $request->title, 'path' => $filename]);
  48. }
  49.  
  50.  
  51.  
  52.  
  53. return redirect('/product/create')->with('info', 'Данные сохранены');
  54. }
  55.  
  56. <?php
  57.  
  58. namespace App;
  59.  
  60. use Illuminate\Database\Eloquent\Model;
  61.  
  62. class Product extends Model
  63. {
  64. public function category()
  65. {
  66. return $this->belongsTo(Category::class);
  67. }
  68.  
  69.  
  70. public function images()
  71. {
  72. return $this->hasMany(Image::class);
  73. }
  74.  
  75. }
  76.  
  77.  
  78.  
  79.  
  80.  
  81.  
  82. ErrorException
  83. Invalid argument supplied for foreach()
  84. http://bossphp.x:8080/products/product/store
  85.  
  86. Сейчас такая ошибка появилась
  87.  
  88. public function store(Request $request)
  89. {
  90. // dd('stop');
  91.  
  92. $this->validate($request, [
  93. 'title' => 'required',
  94. 'slug' => 'required|unique:products',
  95. 'text' => 'required',
  96. 'path' => 'nullable|image',
  97.  
  98. ]);
  99.  
  100. $product = new Product();
  101. $product->title = $request->input('title');
  102. $product->slug = $request->input('slug');
  103. $product->text = $request->input('text');
  104. $product->keywords = $request->input('keywords');
  105. $product->description = $request->input('description');
  106. $product->published = $request->input('published');
  107. $product->category_id = $request->input('category_id');
  108. // $product->product_id = $request->input('product_id');
  109. $product->price = $request->input('price');
  110. $product->authorized_price = $request->input('authorized_price');
  111. $product->short_description = $request->input('short_description');
  112. $product->save();
  113. // dd('stop');
  114.  
  115. $image = new Image();
  116.  
  117. $path =public_path().'uploads/product_images';
  118. $file = $request->file('file');
  119. // dd($path);
  120.  
  121. foreach ($file as $f) {
  122. $filename = str_random(20) .'.' . $f->getClientOriginalExtension() ?: 'png';
  123. $img = ImageInt::make($f);
  124. $img->resize(500,500)->save($path . $filename);
  125. Image::create(['title' => $request->title, 'path' => $filename]);
  126. }
  127. $image->path = $request->input('path');
  128. $image->save();
  129.  
  130.  
  131.  
  132.  
  133. return redirect('/product/create')->with('info', 'Данные сохранены');
  134. }
  135.  
  136.  
  137. Добавил новый объект в метод . Ошибка пропала, но картинка не сохраняется. Как найти причину не сохранения картинки?
  138.  
Add Comment
Please, Sign In to add comment