user3356666 user3356666 - 3 months ago 8
PHP Question

Laravel inserting multiple records in table

I try to do that

$names = $request->input('name');
$forid= $request->input('forid');
$ArrayNames = explode(",",$names);

$dataArrayNames = array();
foreach($ArrayNames as $name)
{
$dataArrayNames[] = array('name'=>$name, 'forId' => $forid);
}

Model::insert(array($dataArrayNames));


But i get the error message
" preg_replace(): Parameter mismatch, pattern is a string while replacement is an array "

NOTE : $request->input('name') = 'nameA,nameB,nameC'

Answer

You are making an array of an array. Try this.

Model::insert($dataArrayNames);
Comments