Что может быть лучше для чтения электронных таблиц (файл Excel) в laravel? - PullRequest
0 голосов
/ 18 марта 2020

Я пытаюсь прочитать файл Excel и сохранить эти данные в базе данных. Этот файл Excel является своего рода шаблоном. один будет шаблоном по умолчанию, и этот шаблон может быть изменен в будущем. В настоящее время я читаю этот файл со многими if условиями. Лично я считаю, что это не лучший способ прочитать файл Excel, поэтому ищу лучший способ. 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);
            }
        }
    }

// функция импорта файла манифеста файла


    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 в коде - это имя столбца файла Excel. я проверяю имя столбца и сохраняю данные в соответствии с этим.

...