Загрузить фото в проект и дб — Laravel

Мне нужна помощь с загрузкой. Я хочу вставить продукт в БД. Продукт имеет 3 изображения. Я хочу, чтобы изображение было загружено в определенную папку в проекте, и путь должен быть введен в БД.
Папка, в которую я хочу загрузить фотографии: / public / css / img

Мой БД выглядит так: я добавлю пример, добавленный вручную в БД.

id | название | цена | category_id | images1 | изображений2 | | images3 | и т.п.
1 | Диван | 324,0 5 | /css/img/1.jpg | /css/img/2.jpg | /css/img/3.jpg

Это мой взгляд addProductModal.blade.php -> это модал с формой.

<div class="modal fade" id="modalFormaddproduct" role="dialog">
<div class="modal-dialog" id="route">
<div class="modal-content">
<!-- Modal Header -->
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal">
<span aria-hidden="true">&times;</span>
<span class="sr-only">Inchide</span>
</button>
<h4 class="modal-title" id="myModalLabel">Adauga Subcategorie</h4>
</div>

<!-- Modal Body -->
<div class="modal-body" style="text-align: center;">
<p class="statusMsg"></p>
<form role="form" action="{{route('addproduct')}}" method="post">
{{csrf_field()}}
<div class="row">
<div class="col-sm-6">
<div class="form-group">
<label>Nume</label>
<input type="text" class="form-control  text-center" name="name" placeholder="">
</div>
</div>
<div class="col-sm-6">
<div class="form-group">
<label>Pret</label>
<input type="text" class="form-control  text-center" name="price" placeholder="">
</div>
</div>
</div>
<!-- /.row -->
<div class="col-sm-6">
<div class="form-group">
<label>Subcategoria:</label>
<select style="text-align-last:center" class="form-control text-center" name="category_id">
@foreach($categories as $category)
@foreach($category->subcategories as $subcategory)
<option value="{{$subcategory->id}}">{{$subcategory->category}}</option>
@endforeach
@endforeach
</select>
</div>
</div>

<div class="row">
<div class="col-sm-6">
<div class="form-group">
<label>Descriere</label>
<input type="text" class="form-control  text-center" name="description" placeholder="">
</div>
</div>
<div class="col-sm-6">
<div class="form-group">
<label>Marime</label>
<input type="text" class="form-control text-center" name="size" placeholder="">
</div>
</div>
<div class="col-sm-6">
<div class="form-group">
<label>Material</label>
<input type="text" class="form-control  text-center" name="material" placeholder="">
</div>
</div>
<div class="col-sm-6">
<div class="form-group">
<label>Cantitate</label>
<input type="text" class="form-control  text-center" name="quantity" placeholder="">
</div>
</div>
<div class="col-sm-6">
<div class="form-group">
<label>Recomandat:</label>
<select style="text-align-last:center" class="form-control text-center" name="hot">
<option value="0">Nerecomandat</option>
<option value="1">Recomandat</option>
</select>
</div>
</div>
<div class="row">
<div class="col-sm-4">
<div class="form-group">
<label>Imagine 1:</label>
<input type="file" name="file1" id="file1">
<input type="submit" value="Upload1" name="submit1">
<input type="hidden" value="{{ csrf_token() }}" name="_token">
</div>
</div>
<div class="col-sm-4">
<div class="form-group">
<label>Imagine 2:</label>
<input type="file" name="file2" id="file2">
<input type="submit" value="Upload2" name="submit2">
<input type="hidden" value="{{ csrf_token() }}" name="_token">
</div>
</div>
<div class="col-sm-4">
<div class="form-group">
<label>Imagine 3:</label>
<input type="file" name="file3" id="file3">
<input type="submit" value="Upload3" name="submit3">
<input type="hidden" value="{{ csrf_token() }}" name="_token">
</div>
</div>
</div>
<div class="modal-footer">
<button type="button" style="background: gainsboro; border-radius: 8px" class="btn btn-default" data-dismiss="modal">Inchide</button>
<button type="submit" style="background: #10D47D; border-radius: 8px" class="btn btn-primary">Adauga</button>
</div>
</div>
</form>
</div>
</div>
</div>
</div>

Маршрут есть Route::post('/products/add', 'AdminController@addproduct')->name('addproduct');

Контроллер: AdminController.php

public function addproduct(Request $request)
{
$product = new Product();
$product->title = $request->name;
$product->price = $request->price;
$product->category_id =  $request->category_id;
$product->description = $request->description;
$product->size = $request->size;
$product->material = $request->material;
$product->quantity = $request->quantity;
$product->hot = $request->hot;
$product->images1 = $request->file1;
$product->images2 = $request->file2;
$product->images3 = $request->file3;if (Input::hasFile('file1','file2','file3')) {

echo 'Uploaded';
$file = Input::file('file1','file2','file3');
$file->move('uploads', $file->getClientOriginalName());
echo '';
}
$product->save();
return redirect(route('adminproducts'))->with('success', 'The Product was added');
}

0

Решение

Вы должны добавить в свою форму:

enctype="multipart/form-data"

чтобы убедиться, что вы можете загрузить файл так:

<form role="form" action="{{route('addproduct')}}" method="post" enctype="multipart/form-data">
0

Другие решения

Я не думаю, что есть какая-либо причина не использовать какую-то поддерживаемую библиотеку, а написать всю функциональность самостоятельно. Я бы порекомендовал вам взглянуть на: https://github.com/spatie/laravel-medialibrary

0

Я нашел решение.

  public function addproduct(Request $request)
{
$product = new Product();
$product->title = $request->name;
$product->price = $request->price;
$product->category_id = $request->category_id;
$product->description = $request->description;
$product->size = $request->size;
$product->material = $request->material;
$product->quantity = $request->quantity;
$product->hot = $request->hot;
$file1 = Input::file('file1');
$file2 = Input::file('file2');
$file3 = Input::file('file3');
$filename1 = $file1->getClientOriginalName();
$filename2 = $file2->getClientOriginalName();
$filename3 = $file3->getClientOriginalName();
$file1 = $file1->move(public_path().'/img', $filename1);
$file2 = $file2->move(public_path().'/img', $filename2);
$file3 = $file3->move(public_path().'/img', $filename3);
$product->images1 = '/img/'.$filename1;
$product->images2 = '/img/'.$filename2;
$product->images3 = '/img/'.$filename3;
$product->save();
return redirect(route('adminproducts'))->with('success', 'Produsul a fost creat');
}
0
По вопросам рекламы ammmcru@yandex.ru
Adblock
detector