简体   繁体   English

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

[英]Dynamically add form input fields and store values with Laravel 5

I'm working with a Product & Price model. 我正在使用产品和价格模型。 My products table has an id and name column, while my prices table has an id,product_id,cost and date column. 我的产品表有一个id和name列,而我的价格表有一个id,product_id,cost和date列。 The product_id on the prices table references the id on the products table. 价格表上的product_id引用了产品表上的id。 My form displays a field for each product so that the user will then enter the price from time to time. 我的表单为每个产品显示一个字段,以便用户随后不时输入价格。 My challenge is how to handle the request data so that the price will correspond to the product_id. 我的挑战是如何处理请求数据,以便价格与product_id对应。 Below is the code i have written so far 以下是我到目前为止编写的代码

Form 形成

<form class="form-horizontal" method="POST" action="{{ url('/home/prices') }}">
{{ csrf_field() }}
@if(isset($products) && !empty($products))
    @foreach($products as $product)
        <div class="form-group">
            <div>
                <input type="hidden" class="form-control" name="product_id[]" value="{{ $product->id }}">
            </div>
        </div>

        <div class="form-group">
            <label class="col-sm-2" for="{{ $product->name }}">{{ ucwords($product->name) }}</label>

            <div class="col-sm-3">
                <input type="text" class="form-control" name="cost[]">
            </div>
        </div>
    @endforeach
@else
    Sorry, no product available currently.
@endif

<button type="submit" class="btn btn-default">Add</button>
</form>

PriceController 价格控制器

public function store(Request $request)
{
//dd($request);
foreach ($request->input('cost') as $cost) {
    Price::create([
        'product_id' => $request->product_id,
        'date' => Carbon::now(),
        'cost' => $cost,
        'trend' => 0
    ]);
}

return redirect('/home');
}

This what i get when i dump the request data 这是我转储请求数据时得到的 在此处输入图片说明

Of course my code throws up this error at Builder->insertGetId(array('product_id' => array('1', '2', '3'), 'date' => object(Carbon), 'cost' => '14.05', 'trend' => 0, 'updated_at' => '2017-08-07 11:21:47', 'created_at' => '2017-08-07 11:21:47'), 'id') 当然我的代码at Builder->insertGetId(array('product_id' => array('1', '2', '3'), 'date' => object(Carbon), 'cost' => '14.05', 'trend' => 0, 'updated_at' => '2017-08-07 11:21:47', 'created_at' => '2017-08-07 11:21:47'), 'id')

How do i fix this? 我该如何解决?

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

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

as you can see the whole array is getting passed in the id of products, so you need to mention a particular id for which you need to insert 如您所见,整个数组正在传递给产品ID,因此您需要提及一个特定的ID,您需要为其插入

You can use concept of index or key/value pair in array of input like this: 您可以在输入数组中使用索引或键/值对的概念,如下所示:

Try this, 尝试这个,

foreach ($request->input('cost') as $key=>$cost) {
    Price::create([
        'product_id' => $request->product_id[$key],
        'date' => Carbon::now(),
        'cost' => $cost,
        'trend' => 0
    ]);
}

This will save your data like 这将像

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

Hope you understand. 希望你能理解。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM