models/ExcelImportForm.php
public function import($modelName, $defaultValues = [], $updateMultipleModel = false, $queryConfigs = [])
	{
		$isSuccessful = true;

		$sheetData = $this->arrayFromExcel;
		$this->header = array_shift($sheetData);

		foreach ($sheetData as $record) {

			if ($updateMultipleModel) {

				$parentId = $this->getParentId($queryConfigs['parentModel'], $queryConfigs['parentKey'],
				$record, $modelName, $defaultValues);

				$childModel = new $queryConfigs['childModel'];
				foreach ($queryConfigs['childModelArray'] as $key=>$value) {
					if (!is_array($value)) {
						$childModel->$value = $this->getCellValue($value, $childModel, $record, $key, false);
					} else {
						$childModel->$value['attribute'] = $this->getCellValue($value, $childModel, $record, $key, false);
					}
				}

				$childModel->$queryConfigs['childForeignKey'] = $parentId;
				$childModel->save();
			} else {

				$isSuccessful = $this->createModel($modelName, $defaultValues, $record);
			}
		}
		return $isSuccessful;
	}
public function import($modelName, $defaultValues = [], $updateMultipleModel = false, $queryConfigs = [])
	{
		$isSuccessful = true;

		$sheetData = $this->arrayFromExcel;
		$this->header = array_shift($sheetData);

		foreach ($sheetData as $record) {

			if ($updateMultipleModel) {

				$parentId = $this->getParentId($queryConfigs['parentModel'], $queryConfigs['parentKey'],
				$record, $modelName, $defaultValues);

				$childModel = new $queryConfigs['childModel'];
				foreach ($queryConfigs['childModelArray'] as $key=>$value) {
					if (!is_array($value)) {
						$childModel->$value = $this->getCellValue($value, $childModel, $record, $key, false);
					} else {
						$childModel->$value['attribute'] = $this->getCellValue($value, $childModel, $record, $key, false);
					}
				}

				$childModel->$queryConfigs['childForeignKey'] = $parentId;
				$childModel->save();
			} else {

				$isSuccessful = $this->createModel($modelName, $defaultValues, $record);
			}
		}
		return $isSuccessful;
	}
public function import ( $ modelName , $ defaultValues = [ ] , $ updateMultipleModel = false , $ queryConfigs = [ ] ) { $ isSuccessful = true ; $ sheetData = $ this -> arrayFromExcel ; $ this -> header = array_shift ( $ sheetData ) ; foreach ( $ sheetData as $ record ) { if ( $ updateMultipleModel ) { $ parentId = $ this -> getParentId ( $ queryConfigs [ 'parentModel' ] , $ queryConfigs [ 'parentKey' ] , $ record , $ modelName , $ defaultValues ) ; $ childModel = new $ queryConfigs [ 'childModel' ] ; foreach ( $ queryConfigs [ 'childModelArray' ] as $ key => $ value ) { if ( ! is_array ( $ value ) ) { $ childModel -> $ value = $ this -> getCellValue ( $ value , $ childModel , $ record , $ key , false ) ; } else { $ childModel -> $ value [ 'attribute' ] = $ this -> getCellValue ( $ value , $ childModel , $ record , $ key , false ) ; } } $ childModel -> $ queryConfigs [ 'childForeignKey' ] = $ parentId ; $ childModel -> save ( ) ; } else { $ isSuccessful = $ this -> createModel ( $ modelName , $ defaultValues , $ record ) ; } } return $ isSuccessful ; }
Import the excel to the model using active record
@param string $model The model name to save the excel rows into
@param array $defaultValues The default values to be assigned
@return Return true if all records is saved
Import the excel to the model using active record