deleted
May 19, 2018
7
Level 4
How to insert array of data into single table?
Hello, I am currently trying to upload images to my application through a form, but want to allow the ability to upload multiple images for each "event". Here is my controller code:
$event = Event::create(request(['name', 'location', 'address', 'dateAndTime', 'shortDescription', 'longDescription']));
$images[] = request('imageName');
foreach($images as $image) {
Image::create(['imageName' => $image]);
$event->images()->attach($image);
}
And here is my form:
<form method="post" action="/events">
{{ csrf_field() }}
<div class="form-group">
<label for="nameInput">Name</label>
<input type="text" name="name" class="form-control" id="nameInput" placeholder="Event Name" required autofocus>
</div>
<div class="form-group">
<label for="locationInput">Location</label>
<input type="text" name="location" class="form-control" id="locationInput" placeholder="Event Location">
</div>
<div class="form-group">
<label for="addressInput">Address</label>
<input type="text" name="address" class="form-control" id="addressInput" placeholder="Event Address" required>
</div>
<div class="form-group">
<label for="datetimeInput">Date and Time</label>
<input type="datetime-local" name="dateAndTime" id="datetimeInput" class="form-control" placeholder="Event Date and Time" required>
</div>
<div class="form-group">
<label for="shortDescriptionInput">Short Description</label>
<input type="text" name="shortDescription" id="shortDescriptionInput" class="form-control" placeholder="Short Event Descrition">
</div>
<div class="form-group">
<label for="longDescriptionInput">Long Description</label>
<textarea name="longDescription" id="longDescriptionInput" class="form-control" placeholder="Long Event Description" rows="5"></textarea>
</div>
<div class="form-group">
<label for="fileInput">Event images</label>
<input type="file" name="imageName[]" class="form-control-file" id="fileInput" multiple>
</div>
<button type="submit" class="btn btn-primary">Submit</button>
</form>
How do I upload multiple images to the images table in my database through each form submission?
Please or to participate in this conversation.