jeudi 23 décembre 2021

Insert array data in separate database row in a field not the same Laravel

I am trying to insert an array data into the database, on form submit with the following input field (Name, Age, Amount) but I am getting Array to string conversion Error, so I added json_encode() to the variable to prevent the error, but the issue now is on insertion into the database, the array data is inserting the data in the same row, I want it in different row of same filed, see below image thanks

enter image description here

B is the result I want to get

Controller

    public function store(Request $request)
    {

    $userid = rand(10000,99999);
    $username = $request->get('uname');
    $userage = json_encode($request->get('uage'));
    $useramount = json_encode($request->get('uamount');


    if (isset($username)){
        foreach ( $request->get('uname') as $username) {
            $add[] = [
                'user_id' => $userid,
                'user_name' => $username,
                'user_age' => $userage,
                'user_amount' => $useramount,
            ];
        }
        Person::insert($add);
    }

}


from Newest questions tagged laravel-5 - Stack Overflow https://ift.tt/3sCq7Sq
via IFTTT

Aucun commentaire:

Enregistrer un commentaire