简体   繁体   English

使用laravel将数组数据存储在数据库中

[英]storing array data in database using laravel

I want to store the array data in a database, but in my database I'm getting only the first input tag of the form: here is my controller:我想将数组数据存储在数据库中,但在我的数据库中,我只得到表单的第一个输入标签:这是我的控制器:

public function store(Request $request)
{
    $qt = Qt::all();
    $Itemname = $request->input('Itemname');
    $Quantity = $request->input('Quantity');
    $Price = $request->input('Price');
    $Tax = $request->input('Tax');
    $Total = $request->input('Total');
    $GrandTotal = $request->input('GrandTotal');
    $data = array('Itemname'=>$Itemname,'Quantity'=>$Quantity,'Price'=>$Price,'Tax'=>$Tax,'Total'=>$Total);
    dd($data);
    Qt::table('qts')->insert($data);

    return redirect()->route('quotes.index');
}

I'm not sure what you mean by 'one data' - if you are only storing one field, that shouldn't be happening.我不确定你所说的“一个数据”是什么意思——如果你只存储一个字段,那不应该发生。 The way you have this set up, it is data for one row .您设置的方式是一行的数据。 Thus, the expected output to the database would be a single Qt model saved.因此,数据库的预期输出将是保存的单个Qt模型。 This would be what I would expect in a store() method based on a user entering a single item (with name, price, tax, etc).这将是我在store()方法中所期望的基于用户输入单个项目(名称、价格、税收等)的方法。 One form for one new Qt .一种新Qt 的形式。 And I think you have it almost set up for this.我认为你几乎已经为此做好了准备。

However, Laravel makes the storage of a new model much easier.然而,Laravel 使新模型的存储变得更加容易。 You don't need pretty much any of that code, because it looks like you have your incoming form fields set to correctly match the name of the fields on the Qt model in the database.您几乎不需要任何代码,因为看起来您已将传入的表单字段设置为与数据库中Qt模型上的字段名称正确匹配。 Though, you might want to make them lowercase on both to match convention.但是,您可能希望将它们都设为小写以匹配约定。

If the form fields are a match to the database fields on the model, you can remove almost everything in that method and replace it with:如果表单字段与模型上的数据库字段匹配,您可以删除该方法中的几乎所有内容并将其替换为:

public function store(Request $request)
{
    Qt::create($request->all());

    return redirect()->route('quotes.index');
}

The create method will take the $request object as is, and automatically set the right elements to the right fields if they are set to fillable on the model. create 方法将按原样获取$request对象,如果它们在模型上设置为可填充,则自动将正确的元素设置为正确的字段。

EDIT:编辑:

I understand you are looking to make multiple rows of items for a single customer based on many items coming in through your form.我了解您希望根据通过您的表单输入的许多项目为单个客户制作多行项目。 However, I don't know where that information is coming from -- I don't know where the array of items is within your form (or $request object).但是,我不知道这些信息来自哪里——我不知道项目数组在您的表单(或$request对象)中的位置。 This might be your reason why you are having an error in the first place: I don't see how to loop or find multiple items, I just see one item coming in, which would produce one row into the database based on that form.这可能是您首先出现错误的原因:我不知道如何循环或查找多个项目,我只看到一个项目进入,这将根据该表单在数据库中生成一行。 The above code is correct based on what you have said is coming from the form.根据您所说的来自表单的内容,上述代码是正确的。

However, based on the parameterize() error you mention in the comments, you likely have an array somewhere that is causing the parameter issue, and which would help you to create multiple rows.但是,根据您在评论中提到的parameterize()错误,您可能在某处有一个导致参数问题的数组,这将帮助您创建多行。 You don't show what your array is, but you mention that these many items would be attached to a single customer.您没有显示您的阵列是什么,但您提到这些许多项目将附加到一个客户。

Here is a possible way to do this.这是一种可能的方法来做到这一点。 I will make up a few variables based on what you've said (some array to loop on, that there is a 'customer' object or similar, etc.)我将根据您所说的内容组成一些变量(一些要循环的数组,有一个“客户”对象或类似对象,等等)

public function store(Request $request)
{
     $customer = Customer::find($request->get('customer_id'));  
     // I don't know how you bring in the customer's id - maybe as a $request item or perhaps through GET?

     $newItemsArray = [];  // Store the rows of new items here

    // Whatever the array of items is, pull it and loop on it to create rows of items:
    foreach($request->get('someItemsArray') as $newItem){
         // Assuming the name of the array fields match the database field names:
         // Assumes $newItem is an associative array
         $newItemsArray['Itemname'] = $newItem['Itemname'];
         $newItemsArray['Quantity'] = $newItem['Quantity'];
         $newItemsArray['Price'] = $newItem['Price'];
         $newItemsArray['Tax'] = $newItem['Tax'];
         $newItemsArray['Total'] = $newItem['Total'];
    }

   // Here is where you can create all the rows and attach them at the same time to your customer
   $customer->Qts()->createMany($newItemsArray);   

   return redirect()->route('quotes.index');
}

This will allow for creating multiple rows of items and attach to a customer all at once.这将允许创建多行项目并一次附加到客户。

You'll need to fill in the blanks - how are you sending the multiple rows from your form, how are you sending the customer who is adding the items, how to deal with GrandTotal (is it an array item, or is it a single field passed from the form, based on a calculation), etc. But, this will answer your question on the controller side to get multiple rows in for a customer.您需要填写空白 - 您如何从表单中发送多行,您如何发送正在添加项目的客户,如何处理 GrandTotal(它是一个数组项目,还是单个项目)从表单传递的字段,基于计算)等。但是,这将在控制器端回答您的问题,以便为客户获取多行。

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

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