Laravel 8 Несколько изображений Загрузка и хранить в MySQLMySql

Форум по Mysql
Ответить
Anonymous
 Laravel 8 Несколько изображений Загрузка и хранить в MySQL

Сообщение Anonymous »

Мне нужно сохранить изображение в дм и папке, но мой код хранит только одно изображение, и я хочу сохранить несколько изображений. Я использую Laravel 8.
Это мой конторлер

store public Function Store (запрос $ request)
{
$ request-> validate ([
' brand '=>' требуется | String | max: 255 ',
' model '=>' требуется | String | max: 255 ',
' type '=>' требуется | String ',
'status' => 'обязательно | String',
'location' => 'требуется | String',
'seats' => 'требуется | Integer',
'Год' => 'требуется | integer',
'color' => 'обязательный | String',
'Automatic' => 'обязательный | String',
'fuel_type' => 'требуется | String ',
' description '=>' nullable | String ',
'mages.*' => 'Image | mimes: jpeg, png, jpg, gif | max: 2048', // proverava svaku sliku
)); < /p>
// Prvo kreiramo vozilo u bazi
$vehicle = Vehicle::create($request->only([
'brand', 'model', 'type', 'status', 'location', 'seats', 'year', 'color', 'automatic', 'fuel_type', 'description'
]));

// Proveravamo da li su slike poslate
if ($request->hasFile('images')) {
foreach ($request->file('images') as $image) {
// Pravimo folder sa ID-om vozila
$folderPath = 'vehicles/' . $vehicle->id;
$imagePath = $image->store($folderPath, 'public');

// Čuvamo svaku sliku u bazi
VehicleImage::create([
'vehicle_id' => $vehicle->id,
'path' => $imagePath, // Koristi ispravno ime kolone
]);
}
}

return redirect()->route('admin.vehicles.vehicles')->with('success', 'Vozilo uspešno kreirano!');
}
< /code>
и d это blade.php < /p>


Detalji vozila


@csrf


Brand

@error('brand') {{ $message }} @enderror

Model

@error('model') {{ $message }} @enderror

Tip vozila

Economy class
Standard
SUV
Lux
Convertibles
Van

@error('type') {{ $message }} @enderror

Status

Available
Reserved
In use
In maintenance
Repairing the vehicle

@error('status') {{ $message }} @enderror

Lokacija

@error('location') {{ $message }} @enderror

Broj sjedišta

@error('seats') {{ $message }} @enderror

Godina

@error('year') {{ $message }} @enderror

Boja vozila

@error('color') {{ $message }} @enderror

Mjenjač

Manual
Automatic


@error('automatic') {{ $message }} @enderror

Gorivo

@error('fuel_type') {{ $message }} @enderror

Opis
{{ old('description') }}
@error('description') {{ $message }} @enderror

< /code>

slike

@error ('Images') {{$ message}} @enderror

Kreirajte vozilo



< /code>

document.getElementById ('ImageInput'). AddEventListener ('изменение', function (event) {
let ImagePreview = document.getElementByid ('ImagePreview');

for (let file of event.target.files) {
let reader = new fileReader ();
reader.onload = function (e) {
let ImageContainer = document.createElement ('div');
imageContainer.style.position = "относительный";
ImageContainer.style.display = "inline block";
imageContainer.style.marginright = " 10px ";

let imgelement = document.createElement ('img');
imgelement.src = e.target.result;
imgelement.style.width =" 150px " ;
imgelement.style.height = "100px";
imgelement.style.objectfit = "cover";
imgelement.style.border = "1px solid #ddd";
imgelement.style.borderradius = "5px";

let kensebutton = document.createElement ('span');
closebutton.innerhtml = "×";
closebutton.style. Position = "Absolute";
closebutton.style.top = "5px";
closebutton.style.right = "5px";
closebutton.style.backgroundcolor = "red";
closebutton.style.color = "white";
closebutton.style.borderradius = "50%";
closebutton.style.width = "20px";
closebutton.style.height. = "20px";
closebutton.style.display = "flex";
closebutton.style.justifycontent = "center";
closebutton.style.alignitems = "center";
closebutton.style.cursor = "pointer";
closebutton.style.fontsize = "16px";

closebutton.addeventlistener ('click', function () {
ImageContainer .Remove ();
});

imageContainer.appendChild (imgelement);
imageContainer.appendChild (closebutton);
imagepreview.appendchild (ImageContainer); br />};
reader.readasdataurl (file);
}
});

Чтобы найти решение для нескольких изображений в DB и папке

Подробнее здесь: https://stackoverflow.com/questions/794 ... e-in-mysql
Ответить

Быстрый ответ

Изменение регистра текста: 
Смайлики
:) :( :oops: :roll: :wink: :muza: :clever: :sorry: :angel: :read: *x)
Ещё смайлики…
   
К этому ответу прикреплено по крайней мере одно вложение.

Если вы не хотите добавлять вложения, оставьте поля пустыми.

Максимально разрешённый размер вложения: 15 МБ.

Вернуться в «MySql»