1pppp

Untitled

Aug 6th, 2020
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.16 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. }
Advertisement
Add Comment
Please, Sign In to add comment