1pppp

Untitled

Aug 6th, 2020
46
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 13.87 KB | None | 0 0
  1.  
  2. Почему картинка не сохраняется в другой таблице? У меня связь многие ко многим. Товар сохраняется , а картинка не сохраняется в банзе
  3.  
  4.  
  5. <?php
  6.  
  7. namespace App\Http\Controllers;
  8.  
  9. use App\Category;
  10. use App\Image;
  11. use App\Product;
  12. use Illuminate\Http\Request;
  13. use Intervention\Image\Facades\Image as ImageInt;
  14. use League\Flysystem\File;
  15.  
  16. class AdminProductsController extends Controller
  17. {
  18. //
  19.  
  20. public function index()
  21. {
  22. return view('product.index', [
  23. 'products' => Product::orderBy('created_at', 'desc')->paginate(10)
  24. ]);
  25. }
  26.  
  27.  
  28. public function create(Request $request)
  29. {
  30. $categories = Category::whereNull('category_id')->with('childrenCategories')->get();
  31.  
  32. return view('product.create', compact('categories' ));
  33. }
  34.  
  35.  
  36. public function store(Request $request)
  37. {
  38. // dd('stop');
  39.  
  40. $this->validate($request, [
  41. 'title' => 'required',
  42. 'slug' => 'required|unique:products',
  43. 'text' => 'required',
  44. 'path' => 'nullable|image',
  45.  
  46. ]);
  47.  
  48. $product = new Product();
  49. $product->title = $request->input('title');
  50. $product->slug = $request->input('slug');
  51. $product->text = $request->input('text');
  52. $product->keywords = $request->input('keywords');
  53. $product->description = $request->input('description');
  54. $product->published = $request->input('published');
  55. $product->category_id = $request->input('category_id');
  56. // $product->product_id = $request->input('product_id');
  57. $product->price = $request->input('price');
  58. $product->authorized_price = $request->input('authorized_price');
  59. // $product->path = $request->input('path');
  60. $product->short_description = $request->input('short_description');
  61. $product->save();
  62. // dd('stop');
  63.  
  64.  
  65. $path =public_path().'uploads/product_images';
  66. $file = $request->file('file');
  67. // dd($path);
  68.  
  69. foreach ($file as $f) {
  70. $filename = str_random(20) .'.' . $f->getClientOriginalExtension() ?: 'png';
  71. $img = ImageInt::make($f);
  72. $img->resize(500,500)->save($path . $filename);
  73. Image::create(['title' => $request->title, 'path' => $filename]);
  74. }
  75.  
  76.  
  77.  
  78.  
  79. return redirect('/product/create')->with('info', 'Данные сохранены');
  80. }
  81.  
  82.  
  83. }
  84.  
  85.  
  86.  
  87.  
  88.  
  89.  
  90.  
  91.  
  92. @extends('admin.index')
  93. @section('content')
  94. @if (Session::has('info'))
  95. <div class="alert alert-success" role="alert">
  96. {{ Session::get('info') }}
  97. </div>
  98. @endif
  99. <form action="/products/product/store" method="POST">
  100. {{ csrf_field() }}
  101. <?php
  102. use Illuminate\Support\MessageBag;
  103. /** @var MessageBag $errors */
  104. ?>
  105. <div class="p-4 mb-2"><h1>Создание товара</h1>
  106. <div class="form-group"><label for="inputDesription" class="col-xs-2 control-label">Введите описание(desription)</label>
  107. <div class="col-xs-10">
  108. <input type="text" name="description" class="form-control" placeholder="введите описание" id="description"></div
  109.  
  110. <? if($errors->first("description") != "") echo "<div class='alert'>".$errors->first("description")."</div>"; ?>
  111. </div>
  112. </div>
  113. <div class="form-group"><label for="inputPrice" class="col-xs-2 control-label">Введите цену</label>
  114. <div class="col-xs-10">
  115. <input type="text" name="price" class="form-control" placeholder="введите описание" id="price"></div
  116.  
  117. <? if($errors->first("price") != "") echo "<div class='alert'>".$errors->first("price")."</div>"; ?>
  118. </div>
  119. </div>
  120. {{-- <p><input type="file" class="my-pond" multiple name="product_image"/></p>--}}
  121. {{-- --}}
  122. {{-- <div class="form-group">--}}
  123. {{-- <label for="img">Выберите файл</label>--}}
  124. {{-- <input id="img" type="file" multiple name="file[]">--}}
  125. {{-- </div>--}}
  126.  
  127. {{-- <div class="container">--}}
  128. {{-- <div class="row">--}}
  129. {{-- <div class="col-md-8">--}}
  130. {{-- <form action="{{route('product')}}" class="dropzone" method="post" enctype="multipart/form-data">@csrf</form>--}}
  131. {{-- </div>--}}
  132. {{-- </div>--}}
  133. {{-- </div>--}}
  134.  
  135. <input type="file" id="path" multiple>
  136. <div id="for_preview_uploads">
  137. </div>
  138.  
  139. {{-- <div>--}}
  140. {{-- строка ниже--}}
  141. {{-- </div>--}}
  142. <script>
  143. function resizeImage(img) {
  144.  
  145. const W = parseInt(img.width / 4);
  146. const H = parseInt(img.height / 4);
  147.  
  148. const canvas = document.createElement("canvas");
  149. canvas.width = W;
  150. canvas.height = H;
  151.  
  152. const ctx = canvas.getContext("2d");
  153. ctx.drawImage(img, 0, 0, W, H);
  154.  
  155. const resizedImg = new Image();
  156. resizedImg.src = canvas.toDataURL('image/jpeg', 1);
  157. //document.body.append(resizedImg);
  158. document.querySelector("#for_preview_uploads").append(resizedImg);
  159.  
  160. }
  161.  
  162. function handleFiles(e) {
  163.  
  164. for (const file of this.files) {
  165.  
  166. const img = document.createElement("img");
  167. const reader = new FileReader();
  168.  
  169. reader.addEventListener("load", (e) => {
  170. img.addEventListener("load", (e) => {
  171. resizeImage(img);
  172. });
  173. img.src = e.target.result;
  174. });
  175.  
  176. reader.readAsDataURL(file);
  177.  
  178. }
  179.  
  180. }
  181.  
  182. const fileInput = document.getElementById("path");
  183.  
  184. fileInput.addEventListener("change", handleFiles, false);
  185.  
  186. </script>
  187.  
  188. <div class="form-group"><label for="inputAuthorized_price" class="col-xs-2 control-label">Введите цену со скидкой</label>
  189. <div class="col-xs-10">
  190. <input type="text" name="authorized_price" class="form-control" placeholder="Введите цену со скидкой" id="authorized_price"></div
  191.  
  192. <? if($errors->first("authorized_price") != "") echo "<div class='alert'>".$errors->first("authorized_price")."</div>"; ?>
  193. </div>
  194. </div>
  195. <div><div class="form-group"><label for="inputshort_descriptionl" class="col-xs-2 control-label">Введите короткое описание товара</label>
  196. <div class="col-xs-10">
  197. <textarea rows="10" cols="45" name="short_description" placeholder="Введите короткое описание товара"></textarea></div>
  198. <? if($errors->first("short_description") != "") echo "<div class='alert'>".$errors->first("short_description")."</div>"; ?>
  199. </div>
  200. </div>
  201.  
  202. {{--product_image--}}
  203. <div> <label for="inputKeywords" class="col-xs-2 control-label">Введите keywords</label>
  204. <div class="col-xs-10">
  205. <input type="text" name="keywords" placeholder="введите ключевые слова" id="inputKeywords">
  206. <? if($errors->first("keywords") != "") echo "<div class='alert'>".$errors->first("keywords")."</div>"; ?>
  207. </div>
  208. </div>
  209.  
  210. <div> <div class="form-group"><label for="inputtitle" class="col-xs-2 control-label">Введите заголовок</label>
  211. <div class="col-xs-10">
  212. <input type="text" name="title" placeholder="введите заголовок" id="title"></div>
  213. <? if($errors->first("title") != "") echo "<div class='alert'>".$errors->first("title")."</div>"; ?>
  214. </div>
  215. </div>
  216.  
  217. {{-- <div class="form-group">--}}
  218. {{-- <label>Подкатегории</label>--}}
  219. {{-- <select class="form-control input-sm" name="category_id">--}}
  220. {{-- <option value="">--select--</option>--}}
  221. {{-- @foreach ($categories as $category)--}}
  222. {{-- <option value="{{$category->id}}">{{$category->title}}</option>--}}
  223. {{-- @if ($category->categories)--}}
  224. {{-- @foreach ($category->categories as $category)--}}
  225. {{-- <option value="{{$category->id}}">-----{{$category->title}}</option>--}}
  226. {{-- @endforeach--}}
  227. {{-- @endif--}}
  228. {{-- @endforeach--}}
  229. {{-- </select>--}}
  230. {{-- </div>--}}
  231. <!-- --><?php //dd($categories); ?>
  232.  
  233. <div class="form-group">
  234. <label>Категории</label>
  235. <select class="form-control input-sm" name="category_id">
  236. {{-- <!-- --><?php //dd($categories); ?>--}}
  237. <option selected value="$products->id">--select--</option>
  238. @foreach ($categories as $firstcategory)
  239. <option value="{{$firstcategory->id}}">{{$firstcategory->title}}</option>
  240.  
  241. @if($firstcategory->categories)
  242. @foreach ($firstcategory->categories as $subcategory)
  243. <option value="{{$subcategory->id}}">-{{$subcategory->title}}</option>
  244. @if($subcategory->categories)
  245. @foreach ($subcategory->categories as $sbcategory)
  246. <option value="{{$sbcategory->id}}">---{{$sbcategory->title}}</option>
  247. @endforeach
  248. @endif
  249. @endforeach
  250. @endif
  251. @endforeach
  252. </select>
  253. </div>
  254. <div><div class="form-group"><label for="inputslug" class="col-xs-2 control-label">Введите урл страницы</label>
  255. <div class="col-xs-10">
  256. <input type="text" name="slug" placeholder="Укажите slug"></div>
  257. <? if($errors->first("slug") != "") echo "<div class='alert'>".$errors->first("slug")."</div>"; ?>
  258. </div>
  259. </div>
  260.  
  261. <div><div class="form-group"><label for="inputtextl" class="col-xs-2 control-label">Введите описание товара</label>
  262. <div class="col-xs-10">
  263. <textarea rows="10" cols="45" name="text" placeholder="введите пост статьи"></textarea></div>
  264. <? if($errors->first("text") != "") echo "<div class='alert'>".$errors->first("text")."</div>"; ?>
  265. </div>
  266. </div>
  267.  
  268. <label for="">Статус</label>
  269. <select class="form-control" name="published">
  270. <option value="0">Не опубликовано</option>
  271. <option value="1">Опубликовано</option>
  272. </select>
  273. <input type="submit" value="Отправить">
  274. </form>
  275. <script src="https://cdn.ckeditor.com/4.14.1/standard/ckeditor.js"></script>
  276. <script>
  277. CKEDITOR.replace( 'text' );
  278. </script>
  279. <script>
  280. CKEDITOR.replace( 'short_description' );
  281. </script>
  282.  
  283.  
  284. {{-- <!-- include jQuery library -->--}}
  285. {{-- <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.js"></script>--}}
  286.  
  287. {{-- <!-- include FilePond library -->--}}
  288. {{-- <script src="https://unpkg.com/filepond/dist/filepond.min.js"></script>--}}
  289.  
  290. {{-- <!-- include FilePond plugins -->--}}
  291. {{-- <script src="https://unpkg.com/filepond-plugin-image-preview/dist/filepond-plugin-image-preview.min.js"></script>--}}
  292.  
  293. {{-- <!-- include FilePond jQuery adapter -->--}}
  294. {{-- <script src="https://unpkg.com/jquery-filepond/filepond.jquery.js"></script>--}}
  295.  
  296. {{-- <script>--}}
  297. {{-- $(function(){--}}
  298.  
  299. {{-- // First register any plugins--}}
  300. {{-- $.fn.product_image.registerPlugin(FilePondPluginImagePreview);--}}
  301.  
  302. {{-- // Turn input element into a pond--}}
  303. {{-- $('.my-pond').product_image();--}}
  304.  
  305. {{-- // Set allowMultiple property to true--}}
  306. {{-- $('.my-pond').product_image('allowMultiple', true);--}}
  307.  
  308. {{-- // Listen for addfile event--}}
  309. {{-- $('.my-pond').on('FilePond:addfile', function(e) {--}}
  310. {{-- console.log('file added event', e);--}}
  311. {{-- });--}}
  312.  
  313. {{-- // Manually add a file using the addfile method--}}
  314. {{-- $('.my-pond').first().product_image('addFile', 'index.html').then(function(file){--}}
  315. {{-- console.log('file added', file);--}}
  316. {{-- });--}}
  317.  
  318. {{-- });--}}
  319. {{-- </script>--}}
  320.  
  321.  
  322.  
  323. <!-- Scripts -->
  324. {{-- <script src="{{ asset('js/dropzone.js') }}" defer></script>--}}
  325. {{-- <script src="{{ asset('js/1.js') }}" defer></script>--}}
  326. {{-- <script src="{{ asset('js/dashboard.js') }}" defer></script>--}}
  327.  
  328.  
  329. <!-- Styles -->
  330. <link href="{{ asset('css/dropzone.css') }}" rel="stylesheet">
  331. @endsection
  332.  
  333.  
  334.  
  335.  
  336.  
  337.  
  338.  
  339.  
  340.  
  341.  
  342.  
  343.  
  344.  
  345. <?php
  346.  
  347. namespace App;
  348.  
  349. use Illuminate\Database\Eloquent\Model;
  350.  
  351. class Product extends Model
  352. {
  353. public function category()
  354. {
  355. return $this->belongsTo(Category::class);
  356. }
  357.  
  358.  
  359. public function images()
  360. {
  361. return $this->hasMany(Image::class);
  362. }
  363.  
  364. }
  365.  
  366.  
  367.  
  368.  
  369.  
  370.  
  371.  
Add Comment
Please, Sign In to add comment