Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/66.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 5动态添加表单输入字段并存储值_Php_Mysql_Html_Laravel_Laravel 5.4 - Fatal编程技术网

Php 使用Laravel 5动态添加表单输入字段并存储值

Php 使用Laravel 5动态添加表单输入字段并存储值,php,mysql,html,laravel,laravel-5.4,Php,Mysql,Html,Laravel,Laravel 5.4,我正在使用产品和价格模型。我的products表有一个id和name列,而prices表有一个id、product\u id、cost和date列。价格表上的产品id引用产品表上的id。“我的表单”为每个产品显示一个字段,以便用户随时输入价格。我的挑战是如何处理请求数据,以使价格与产品标识相对应。下面是我迄今为止编写的代码 形式 这是我在转储请求数据时得到的 当然,我的代码会抛出这个错误 at Builder->insertGetId(数组('product\u id'=>array('1',

我正在使用产品和价格模型。我的products表有一个id和name列,而prices表有一个id、product\u id、cost和date列。价格表上的产品id引用产品表上的id。“我的表单”为每个产品显示一个字段,以便用户随时输入价格。我的挑战是如何处理请求数据,以使价格与产品标识相对应。下面是我迄今为止编写的代码

形式

这是我在转储请求数据时得到的

当然,我的代码会抛出这个错误
at Builder->insertGetId(数组('product\u id'=>array('1','2','3'),'date'=>object(Carbon),'cost'=>'14.05','trend'=>0','updated\u'at'=>'2017-08-07 11:21:47','id')


如何解决此问题?

您可以在输入数组中使用索引或键/值对的概念,如下所示:

试试这个

foreach ($request->input('cost') as $key=>$cost) {
    Price::create([
        'product_id' => $request->product_id[$key],
        'date' => Carbon::now(),
        'cost' => $cost,
        'trend' => 0
    ]);
}
这将保存您的数据,如

1 -> 14.05
2 -> 13.75
3 -> 12.99

希望您理解。

您可以在输入数组中使用索引或键/值对的概念,如下所示:

foreach ($request->input('cost') as $key=>$cost) {

    Price::create([
        'product_id' => $request->product_id[$key],
        'date' => Carbon::now(),
        'cost' => $cost,
        'trend' => 0
    ]);
}
试试这个

foreach ($request->input('cost') as $key=>$cost) {
    Price::create([
        'product_id' => $request->product_id[$key],
        'date' => Carbon::now(),
        'cost' => $cost,
        'trend' => 0
    ]);
}
这将保存您的数据,如

1 -> 14.05
2 -> 13.75
3 -> 12.99
希望你能理解

foreach ($request->input('cost') as $key=>$cost) {

    Price::create([
        'product_id' => $request->product_id[$key],
        'date' => Carbon::now(),
        'cost' => $cost,
        'trend' => 0
    ]);
}
正如您所看到的,整个数组都在产品id中传递,因此您需要提到一个需要插入的特定id

正如您所看到的,整个数组都在产品id中传递,因此您需要提到一个需要插入的特定id