Php laravel中的批处理
我想知道如何访问具有id或名称代码的输入元素,例如row_1_firstname和row_2_firstname-当您不确切地知道您有多少行时,如何进行批插入,以及如何为插入和更新过程查询行数未知的Input::get或Input::all()Php laravel中的批处理,php,laravel,laravel-4,Php,Laravel,Laravel 4,我想知道如何访问具有id或名称代码的输入元素,例如row_1_firstname和row_2_firstname-当您不确切地知道您有多少行时,如何进行批插入,以及如何为插入和更新过程查询行数未知的Input::get或Input::all() public function store() { $array = Input::all(); foreach($array as $element) { // do stuff in here
public function store()
{
$array = Input::all();
foreach($array as $element) {
// do stuff in here
// store data in a array for batch insert and update processing
}
}
我会将输入命名为数组
firstname[]
和隐藏的输入ids[]
,其中id要编辑
public function store()
{
$names = Input::get('firstname');
$ids = Input::get('ids');
for ($i=0, $c = count($ids); $i<$c; ++$i) {
echo $names[$i].' was set for id '.$ids[$i];
}
}
公共函数存储()
{
$names=Input::get('firstname');
$ids=Input::get('ids');
对于($i=0,$c=count($id);$i分隔firstnames[]
和ids[]
以及您需要的任何其他字段是一种方法,但肯定不是最简单的方法
我建议您添加以下输入:
// assuming you add users
users[1][firstname]
users[1][lastname]
users[2][firstname]
users[2][lastname]
...
当然,users[1]
是通过js控制的,它是动态的users[i]
然后在PHP代码中,您可以简单地执行以下操作:
$users = Input::get('users');
// validate input according to your needs, then:
DB::table('users')->insert($users);
那么,您将如何创建包含所有包含插入信息的数组的$data\u数组呢?比如构建数组,这样我就可以使用我的模型来实现类似ProductVariations::insert的功能($data\u数组);构建该数组是一个问题,因为我从未做过这种类型的代码before@KDM我添加了插入数据库的示例感谢您的回答
$users = Input::get('users');
// validate input according to your needs, then:
DB::table('users')->insert($users);