search

Home  >  Q&A  >  body text

How to post json data and form data into a single request in Laravel 9

I have a form, inside the form I can input multiple images and convert them to json

My form html create.blade.php

<form method="post" action="{{ route('m_announcement.store') }}" enctype="multipart/form-data">
                @csrf
/////// many input just not included 
//////// my image input
                 <div class="row mb-4">
                    <label class="col-sm-2 col-label-form">Upload Images</label>
                    <div class="col-sm-10">
                        <input type="file" name="files[]" id="files" multiple
                            accept="image/jpeg, image/png, image/gif," onchange="timeFunction()"><br />
                    </div>
                </div>

///////// my imagae preview
                <output id="Filelist"></output>

                <div class="text-center">
                    <input type="submit" class="btn btn-primary" value="Add" />
                </div>

</form>

This function in the script outputs the image to a var AttachmentArray in json format, I also want to post it via the form

function printvalues() {
            console.log(AttachmentArray); all image converted in base64 output in json format with imahe details

Is there a way to connect the json to the form, when I press submit json the data will be output as per the request

laravel 9 announcement controller.php

public function store(Request $request)
    {
      
        //dd($request);
        $jsonimages = $request->jsonimages;

      // decode the json a create loop to upload to database image
      // some code to upload the form to announcement database

        //image upload
}

I'm not just on the frontend, most of the time I develop on the backend first, this is my first web application project, I tried searching for ajax or something but I don't understand it yet.

P粉083785014P粉083785014335 days ago539

reply all(1)I'll reply

  • P粉518799557

    P粉5187995572024-01-03 00:13:52

    You can get the name of the field input using the following method

    dd($request->file->('files'));

    See here for more informationRetrieve uploaded files in laravel 9

    reply
    0
  • Cancelreply