thegrafikstation's avatar

How to input data from a form input array to Laravel database?

I have searched a lot, but with so many results I am just confused about the solutions. I am trying to input data from a form array to laravel database. But how the logic will be built inside the controller is a scary question for me. I am hopeful that I can get a handful amount of help from this forum as I am following its series to learn the code.

0 likes
7 replies
tykus's avatar

It is not possible to help you without more details (or your example code). Generally, if you want to create individual records for each item in the array, you should simply loop over the Request input:

foreach($request->input('names', []) as $name) {
    // do something with name
}
1 like
thegrafikstation's avatar

This is my code with form to create entries...

                <tr id="trow" class="">
                    <td><input type="text" name="title[]" id=""></td>
                    <td><input type="text" name="role[]" id=""></td>
                    <td><input type="text" name="email[]" id=""></td>
                    <td><input type="text" name="phone[]" id=""></td>
                    <td><button type="button" class="btn btn-danger" onclick="btnDel(this)">x</button></td>
                    
                </tr>
                
            </tbody>
        </table>
    </div>

    <div class="col-md-12">
        <button type="submit" class="btn btn-primary">Submit</button>
    </div>

</form>

The logic inside the controller is: public function bulkcreate(Request $request, Worker $worker){

    $title = $request->title;
    $role = $request->role;
    $email = $request->email;
    $phone = $request->phone;

       for($i=0;$i<count($title);$i++){
       
         $datasave = [
            'title'=>$title[$i],
            'role'=>$role[$i],
            'email'=>$email[$i],
            'phone'=>$phone[$i],
            "created_at" =>  \Carbon\Carbon::now(),
            "updated_at" =>  \Carbon\Carbon::now()

            // "created_at" =>  date('Y-m-d H:i:s')

        ];
        
        DB::table('workers')->insert($datasave);
    }
    
     return redirect('/workers')->with('message','Data input success');
    

}

The code has done the job for me. I am seeking something more practical and compact to complete this job. I was advised to put in some code to get assisted in an efficient way so I did this here. I am looking forward to seeing your help.

krisi_gjika's avatar

@thegrafikstation map your data correctly and insert once:

<tr id="trow" class="">
		<td><input type="text" name="rows[i][title]" id=""></td>
        <td><input type="text" name="rows[i][role]" id=""></td>
        <td><input type="text" name="rows[i][email]" id=""></td>
        <td><input type="text" name="rows[i][phone]" id=""></td>
        
		<td><button type="button" class="btn btn-danger" onclick="btnDel(this)">x</button></td>                    
</tr>
public function bulkcreate(Request $request, Worker $worker){

    DB::table('workers')->insert($request->rows);
}
1 like
PovilasKorop's avatar

@thegrafikstation I would build array of arrays and run this statement ONCE after the foreach:

for ...
    $datasave[] = ...
DB::table('workers')->insert($datasave):

There are also other approaches with Livewire/Vue that allow you to assign objects as rows and work with them as rows but it's much more complex, then.

Finally, you may want to use Eloquent Model::create() method instead of Query Builder DB::table()->insert(), then you wouldn't need to set the timestamps manually. But then you would be unable to pass array as in my forst advice.

Snapey's avatar

if you name your filelds as below you can write the data with a simple foreach

You can also use laravels array validation

<tr id="trow" class="">
                    <td><input type="text" name="workers[][title]" id=""></td>
                    <td><input type="text" name="workers[][role]" id=""></td>
                    <td><input type="text" name="workers[][email]" id=""></td>
                    <td><input type="text" name="workers[][phone]" id=""></td>
                    <td><button type="button" class="btn btn-danger" onclick="btnDel(this)">x</button></td>
                    
                </tr>

you can iterate as foreach($request->workers as $worker)

Please or to participate in this conversation.