0

I tried to save multiple rows to the database with one query, without success. I used a for loop but it can't retrieve the value of the inputs that are generated using jQuery after setting the number of rows.

Controller

public function store(Request $request)
{
    $nbrrowsol = $request->get('nbrrowsol');
    for ($i = 1; $i < $nbrrowsol; $i++) {
        $commande = new Commande();
        $commande->nom_client = $request->get('nomclient');
        $commande->organisme = $request->get('organisme');
        $commande->adresse = $request->get('adresse');
        $commande->email = $request->get('email');
        $commande->tel1 = $request->get('tel');
        $commande->tel2 = $request->get('tel2');
        $commande->fax = $request->get('fax');
        $commande->commercial = $request->get('commercial');
        $commande->date_reception = $request->get('datereception');
        $commande->date_prelevement = $request->get('dateprelev');
        $commande->saved_by = $request->get('savedby');
        $commande->code = $request->get('codesol' + $i);
        $commande->nature = $request->get('naturesol' + $i);
        $commande->reference_cli = $request->get('reference_clisol' + $i);
        $commande->profondeur = $request->get('profondeursol' + $i);
        $commande->culture = $request->get('culturesol' + $i);
        $commande->variete = $request->get('varietesol' + $i);
        $commande->gps = $request->get('gpssol' + $i);
        $commande->analyse_demande = $request->get('analysedemandesol' + $i);
        $commande->valide = $request->get('checkvalidee');
        $commande->save();
    }

    return redirect('gestion_commandes/create');
}

I get the following error after the user submits for saving data.

A non-numeric value encountered

I think that I didn't use (for) correctly or the form of:

$commande->code = $request->get('codesol' + $i);

Error image

1
  • have you tried passing index to your inputs like, $request->get('nomclient[ '. $i . ']') Commented May 8, 2019 at 22:16

2 Answers 2

1

enter image description here

You seem to be fetching input using + addition operator which is for javascript, in PHP . operator is used for concatenation, also you'll need to fetch inputs it using the index of your inputs array, for example when your <input name="nomClient[]">

$request->get('nomclient['. $i . ']')

Sign up to request clarification or add additional context in comments.

Comments

0

First of all to concat a string in PHP is done using . (dot) notation, not +. So this 'codesol'+$i should become 'codesol' . $i and so on for the rest.

If you want to insert multiple rows directly, then take a look at this example.

3 Comments

yes you are right, now the data are registered in the database but only with the value i = 1, the value of i does not increment
it won't if your $nbrrowsol is 2.
Because you say $i < 2 which means all below 2, try $i <= $nbrrowsol.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.