dgoetz's avatar

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?

0 likes
7 replies
dgoetz's avatar

@jlrdw My apologies, should have been more clear about what I'm trying to do here. I'm not actually trying to upload the file to the app just yet (because I may use an external storage system of sorts), I'm simply trying to upload multiple image names to the database so they can be retrieved and used to fetch the files once I do figure out what storage method I will use.

jlrdw's avatar

Ok, deleted reply, I stand by the part where I said you need to study the docs.

dgoetz's avatar

Will do! Always trying to learn more. Thank you.

jlrdw's avatar

hint

event table

id 15

a related image table

id 4 apic.jpg related to event who's id = 15 event_id = 15

id 1005 sue_may.jpg related to event who's id = 15 event_id = 15

If that don't answer, I plum give up, don't insert an array, insert the images (names) in a related table.

dgoetz's avatar

I'm not sure what you're trying to say.. I am trying to insert the image names in a related table. I have an image table, an event table, and a pivot table to connect the two by ids. I'm trying to figure out how to cycle through the array of image names that results from form submission and insert each into the corresponding db table.

Vilfago's avatar

Not a working code, but it will give you a hint.

foreach($request('imageName') as $image)
{
    $image->save();
}

Please or to participate in this conversation.