mercredi 18 mars 2020

What could be better way to read spreadsheet (Excel File) in laravel?

I am trying to read excel file and store that data in database. This excel file is kind of template. one would be default template and this template could be changed in future. Currently i am reading that file with many if conditions .I personally think that it isn't best way to read excel file so looking for better way. this procedure is divided into two functions

  public function importManifestFile(Request $request)
    {
        $path = $request->file('manifest_file')->getRealPath();

        $spreadsheet = IOFactory::load($path);
        $sheetData = $spreadsheet->getActiveSheet()->toArray(null, true, true, true);



        foreach ($sheetData as $rows => $ManifestConsignments) {


            if ($rows >= 9 && $rows <= 37) {
                $this->manifestConsignment($ManifestConsignments);
            }
        }
    }

//import file manifest consignment function


    public function manifestConsignment($ManifestConsignments)
    {

        $consignment = new Consignment;
        foreach ($ManifestConsignments as $key => $ManifestConsignment) {

            if ($key == 'A') {
            }
            if ($key == 'B') {
                $consignment->delivery_date = $ManifestConsignment;
            }
            if ($key == 'C') {
                $addressId = $ManifestConsignment;
            }
            if ($key == 'D') {
                $companyName = $ManifestConsignment;
            }
            if ($key == 'E') {
                $streetAddress = $ManifestConsignment;
            }
            if ($key == 'F') {
                $suburb = $ManifestConsignment;
            }
            if ($key == 'G') {
                $state = $ManifestConsignment;
            }
            if ($key == 'H') {
                $postCode = $ManifestConsignment;
            }

            if (isset($postCode)) {
                if (Address::where('company_name', $companyName)->where('street_address', $streetAddress)->where('suburb', $suburb)->where('state', $state)->where('postcode', $postCode)->exists()) {
                    $deliveryAddress = Address::where('company_name', $companyName)->where('street_address', $streetAddress)->where('suburb', $suburb)->where('state', $state)->where('postcode', $postCode)->first();
                    $deliveryAddressId = $deliveryAddress->id;
                    $consignment->delivery_address = $deliveryAddressId;

                    unset($postCode);
                } else {
                    $address = new Address;
                    $address->company_name = $companyName;
                    $address->street_address = $streetAddress;
                    $address->suburb = $suburb;
                    $address->postcode = $postCode;
                    $address->state = $state;
                    $address->save();

                    $consignment->delivery_address = $address->id;
                    unset($postCode);
                }
                if ($key == 'I') {
                    $consignment->carton = $ManifestConsignment;
                }
                if ($key == 'J') {
                    $consignment->pallet = $ManifestConsignment;
                }
                if ($key == 'K') {
                    $consignment->weight = $ManifestConsignment;
                }
                if ($key == 'L') {
                    $consignment->invoice_value = $ManifestConsignment;
                }
                if ($key == 'M') {
                    if (!empty($ManifestConsignment)) {
                        $consignment->cash_on_delivery = $ManifestConsignment;
                    }
                }
                if ($key == 'N') {
                    $consignment->product_type_id = 1;
                }
                if ($key == 'O') {
                    $consignment->comment = $ManifestConsignment;
                }
                $consignment->customer_id =1;
                $consignment->status = 'In Warehouse';
                $consignment->product_type_id = 1;

                $consignment->save();
            }
        }
    }

$key in code is column name of excel file . i am checking what is column name and storing data according to that.



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire