Controller:
if (InvoiceProduct::where('product_name', '=', $file['products'])->exists()) {
}else {
for($x = 0; $x < count($file['products']); $x++)
{
$pstore = new InvoiceProduct;
$pstore->product_name = $file['products'][$x];
// $pstore->invoice_id = $[$x]; (i need to insert this ID from Invoice table)
$pstore->quantity =$file['count'][$x];
$pstore->barcode=$file['scan'][$x];
$pstore->vendor_code =$file['vn'][$x];
$pstore->save();
}
}
I want this row to display in another table
i want this row to display in another table
I want to store that id in invoice_id
here I want to that id in this row
but the thing is I have multiple files in the Invoice table and in each file I have multiple products. I want to assign the id from the invoice table to its respective product. One id can have multiple products.
Thanks for your time!
Complete function:
public function index(){
$path = public_path('files\pending');
$allfiles = scandir($path);
$allfiles = array_diff(scandir($path), array('.', '..'));
$files = array();
foreach ($allfiles as $filename) {
$file= File::get(public_path('files/pending/'.$filename));
$data = explode('~', $file);
$CustomerName = "";
$PO = "";
$address ="";
$city ="";
$products= array();
$id= array();
//
$count= array();
$scan= array();
$ca= array();
$vn= array();
$unit= array();
$sctype= array();
//
foreach($data as $d){
$row = explode("*",$d);
$row = str_replace("\r\n","",str_replace("\r","",str_replace("\n","",$row)));
if($row[0] == "N1") {
$name= $row[2];
$id = $row[4];
}elseif($row[0] == "N9") {
$PO= $row[2];
}
elseif($row[0] == "N3") {
$address= $row[1];
}elseif($row[0] == "N4") {
$city= $row[1];
}elseif($row[0] == "PID") {
$products[] = $row[5];
}elseif($row[0] == "PO1") {
$count[] = $row[2];
$scan[] = $row[7];
$ca[] = $row[4];
$vn[] = $row[9];
$unit[] = $row[3];
$sctype[] = $row[6];
}
}
$files[] = [
"file_name"=>$filename,
"customer_name"=>$name,
"po"=>$PO,
"loc"=>$address,
"city"=>$city,
"products"=> $products,
"id"=>$id,
"count"=> $count,
"scan"=> $scan,
"ca"=> $ca,
"vn"=> $vn,
"unit"=> $unit,
"sctype"=> $sctype,
];
$merge = array_merge_recursive(
array_combine ($products , $count),
array_combine($products , $scan),
array_combine($products , $ca),
array_combine($products , $vn),
array_combine($products , $unit),
array_combine($products , $sctype),
);
}
foreach($files as $file){
if (Invoice::where('file_name', '=', $file['file_name'])->exists()) {
}else {
$store = new Invoice;
$store->file_name = $file['file_name'];
$store->name = $file['customer_name'];
$store->po_no = $file['po'];
$store->address = $file['city'];
$store->total_items = count($file['products']);
$store->save();
}
if (InvoiceProduct::where('product_name', '=', $file['products'])->exists()) {
}else {
for($x = 0; $x < count($file['products']); $x++)
{
$pstore = new InvoiceProduct;
$pstore->product_name = $file['products'][$x];
$pstore->invoice_id = $store->id [$x];
$pstore->quantity =$file['count'][$x];
$pstore->barcode=$file['scan'][$x];
$pstore->vendor_code =$file['vn'][$x];
$pstore->save();
}
}
}
return view('welcome' , compact('files'));
}
My Answer:
To access the ID of the Invoice table and store it in the InvoiceProduct table, you need to make sure that you have the ID available when you are creating the Invoice record. In your code, you are creating the Invoice record inside a loop, so you need to make sure that you have access to the ID of the newly created Invoice record.
Here is an updated version of your code that should work:
php
foreach($files as $file){
if (Invoice::where('file_name', '=', $file['file_name'])->exists()) {
continue;
} else {
$store = new Invoice;
$store->file_name = $file['file_name'];
$store->name = $file['customer_name'];
$store->po_no = $file['po'];
$store->address = $file['city'];
$store->total_items = count($file['products']);
$store->save();
$invoiceId = $store->id;
foreach($file['products'] as $key => $product) {
$pstore = new InvoiceProduct;
$pstore->product_name = $product;
$pstore->invoice_id = $invoiceId;
$pstore->quantity = $file['count'][$key];
$pstore->barcode = $file['scan'][$key];
$pstore->vendor_code = $file['vn'][$key];
$pstore->save();
}
}
}
In this updated code, we are storing the ID of the newly created Invoice record in the `$invoiceId` variable and then using that ID to assign to the `invoice_id` field of each InvoiceProduct record. This way, each product will be associated with the correct Invoice record.
Make sure to adjust the code according to your specific requirements and database structure.
Rate this post
5 of 5 based on 4657 votesComments