1

I've a form which can be saved to database multiple times depending upon number of entries in to it. To be precise, I have 4 input fields and after filling all the fields I click on Add button. Upon clicking on Add button, I'm creating an entry in a table row inside <form> tag like,

<form>
 <table>
  <tr>
   <td> <input type="hidden" name="a[]" value="xyz" /> xyz</td>
   <td> <input type="hidden" name="b[]" value="123" /> 123</td>
   <td> <input type="hidden" name="c[]" value="456" /> 456</td>
   <td> <input type="hidden" name="d[]" value="abc" /> abc</td>
  </tr>
  <tr>
   <td> <input type="hidden" name="a[]" value="xyz" /> xyz</td>
   <td> <input type="hidden" name="b[]" value="123" /> 123</td>
   <td> <input type="hidden" name="c[]" value="456" /> 456</td>
   <td> <input type="hidden" name="d[]" value="abc" /> abc</td>
  </tr>
 </table>
</form>

Now I want to save each row of the table into database as a row. I'm unable to understand how to do that. I'm getting the data in controller using dd($a = $request->input('a')); for a single field as,

array:2 [▼
  0 => "xyz"
  1 => "xyz"
]

I'm using this to save it into DB using Laravel,

$save=Model::Create(array(
        'a'=>$request->input('a'),
        'b'=>$request->input('b'),
        'c'=>$request->input('c'),
        'd'=>$request->input('d')
));

I'm getting error,

Argument 1 passed to Illuminate\Database\Grammar::parameterize() must be of the type array, string given, called in /var/www/html/quoting/vendor/laravel/framework/src/Illuminate/Database/Query/Grammars/Grammar.php on line 681 and defined

How can I save it into the database using the Laravel command.?

2 Answers 2

2

use this

$input = $request->all();

foreach($request->a as $key => $value) {
    Model::Create(array(
        'a' => $value,
        'b' => $input['b'][$key],
        'c' => $input['c'][$key],
        'd' => $input['d'][$key],
    ));
}
Sign up to request clarification or add additional context in comments.

Comments

0

you need to change name for all inputs

<form>
 <table>
  <tr>
   <td> <input type="hidden" name="myform[a][0]" value="xyz" /> xyz</td>
   <td> <input type="hidden" name="myform[b][0]" value="123" /> 123</td>
   <td> <input type="hidden" name="myform[c][0]" value="456" /> 456</td>
   <td> <input type="hidden" name="myform[d][0]" value="abc" /> abc</td>
  </tr>
  <tr>
   <td> <input type="hidden" name="myform[a][1]" value="xyz" /> xyz</td>
   <td> <input type="hidden" name="myform[b][1]" value="123" /> 123</td>
   <td> <input type="hidden" name="myform[c][1]" value="456" /> 456</td>
   <td> <input type="hidden" name="myform[d][1]" value="abc" /> abc</td>
  </tr>
 </table>
</form>

Then in your controller first dd($request->myform) and check you are getting values properly

if you want to store multidimensional array then you have ot use insert not create

$save=Model::insert(multdimensionl array here);

1 Comment

I think you can leave the numbers out of the last pair of brackets in each input name, they will be automatically added.

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.