Skip to content
Advertisement

Multiple form submit in Laravel

Good day everyone,

I have a form with multiple inputs in it and I want to pass it onto the database on submit.

Here’s what my blade.php looks like:

enter image description here

on code:

<form method="post" action="/report" enctype="multiple/form-data">
<input type="file" name="image" multiple style="display: block;">
    <input type="checkbox" name="remark" > Remark 1
    <input type="checkbox" name="remark"> Remark 2

    <ul>
        <li><input type="checkbox" name="remark"> Remark 3</li>
        <ul>
            <li><input type="checkbox" name="remark"> sub remarks</li>
            <li><input type="checkbox" name="remark"> sub remarks</li>
            <li><input type="checkbox" name="remark"> sub remarks</li>
            <li><input type="checkbox" name="remark"> sub remarks</li>
            <li><input type="checkbox" name="remark"> sub remarks</li>
            <li><input type="checkbox" name="remark"> sub remarks</li>
        </ul>
    </ul>
    @foreach($products as $product)
<input type="number" name="product_id[]" value="{{ $product->id}}" hidden>
       <input class="input" type="number" placeholder="Display" name="display">
       <input class="input" type="number" placeholder="Storage" name="storage">
</form>

Here’s my attempt to find a solution:

on my controller:

public function store(Request $request) {
     $user_id = Auth::user()->id;
        $new_report = new Report();

        $forminputs = $request->all();

        foreach($forminputs as $forminput) {

        $request->image->move('images/report_images', $request->image->getClientOriginalName());
        $new_report->user_id = $user_id;
        $new_report->remark = $request->remark;
        $new_report->sub_remark = $request->sub_remark;
        $new_report->area_id = $request->area_id;
        $new_report->store_id = $request->store_id;
        $new_report->product_id = $request->product_id;
        $new_report->display = $request->display;
        $new_report->storage = $request->storage;
        $new_report->image = 'images/report_images/'.$request->image->getClientOriginalName();
        $new_report->save();

        }
}

I’m having the following errors:

ErrorException Array to string conversion

and when adding an image

call to a member function move() on null

I want to save them as

enter image description here

Advertisement

Answer

I have managed to get the following to work by doing the following:

$user_id = Auth::user()->id;
    $inputs = $request->input('display');
    $new_report = new Report();

    $data = array();
    $now = Carbon::now();

    foreach($inputs as $key => $value) {

        $data[] = [
            'user_id' => $user_id,
            'display' => $request->input('display')[$key] ?: 0,
            'storage' => $request->input('storage')[$key] ?: 0,
            'product_id' => $request->product_id[$key],
            'created_at' => $now,
            'updated_at' => $now,
            'area_id' => $request->area_id,
            'store_id' => $request->store_id,
            'remark' => $request->remark[$key] ?? null,
        ];
    } 

    Report::insert($data);  


    if($request->hasFile('image_name')) {
        $image_array = $request->file('image_name');
        $array_len = count($image_array);

        for ($i = 0; $i < $array_len; $i++) {
        $destination_path = 'images/report_images/';    
        $new_image_name = $destination_path . "-" . $image_array[$i]->getClientOriginalName();


        $image_array[$i]->move($destination_path, $new_image_name);

        $new_image = new Image();
        $new_image->image_name = $new_image_name;
        $new_image->store_id = $request->store_id;
        $new_image->report_id = $new_report->id;
        $new_image->save();

        }
    }
User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement