Reputation: 98
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');
}
Upvotes: 0
Views: 816
Reputation: 8168
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. This would be what I would expect in a store()
method based on a user entering a single item (with name, price, tax, etc). One form for one new Qt. And I think you have it almost set up for this.
However, Laravel makes the storage of a new model much easier. 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. 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.
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). 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. 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.
Upvotes: 1