Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/270.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php laravel中的批处理_Php_Laravel_Laravel 4 - Fatal编程技术网

Php laravel中的批处理

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

我想知道如何访问具有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
          // 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);