kp_sahani
2 months ago
213
1
Laravel

Laravel - 7 Excel File Import with Column Key Exist or not Validation

Posted 2 months ago by kp_sahani

Laravel - 7 Excel File Import with Column Key Exist or not Validation why not work?

i had try with Excel:load() method but its not working.

ex. My Excel file in Three columns

| Name | City | State |

| A | B | C |

| AA | BB | CC |

if I changed Column "Name" to "Names" of "City" to "Cityy" Then how to check or validate my Excel data is proper or not or If i Delete One Columns From table then how can i check My Excel Formate is not proper or valid.

my code: Controller

public function import(Request $request) {

    if ($request->hasFile('file')){
        $updateFile = $request->file('file');
        
        $path = $updateFile->getRealPath();
        $fileExtension = $updateFile->getClientOriginalExtension();
        
        $formats = ['xls', 'xlsx', 'ods', 'csv'];
        if (! in_array($fileExtension, $formats)) {
            return response()->json([
                'status' => false,
                'message' => 'Only supports upload .xlsx, .xls files'
            ]);
        } 
        
        $datas = Excel::import(new ProductsImport($request->suppliers_id),request()->file('file'));

}

use Illuminate\Support\Collection; use Illuminate\Support\Facades\Validator; use Maatwebsite\Excel\Concerns\ToCollection;

class ProductsImport implements ToCollection { private $sup_Id; public function __construct(int $supId) { $this->sup_Id = $supId; } public function collection(Collection $rows) {

    foreach ($rows as $key => $row) {

        if( $key == 0 ){
            if($row[0] != 'name' || $row[1] != 'product_code' || $row[2] != 'item_number' || $row[3] != 'description' || $row[4] != 'unit_cost' || $row[5] != 'extended_cost' || $row[6] != 'status'){

                return json_encode(array('importstatus' => false));
                exit();
            }
        } 
        else{

            $where = array(
                'product_code' => $row[1],
                'suppliers_id' => $this->sup_Id
            );
            $Product = Product::where($where)->first();

            if ($Product !== null) {
                $Product->update([
                    'suppliers_id' => $this->sup_Id,
                    'name'    => $row[0], 
                    'product_code' => $row[1],
                    'item_number'     => $row[2],
                    'description'    => $row[3], 
                    'unit_cost' => $row[4],
                    'extended_cost'    => $row[5], 
                    'status' => $row[6],
                ]);
            } else {
                Product::create([
                    'suppliers_id' => $this->sup_Id,
                    'name'    => $row[0], 
                    'product_code' => $row[1],
                    'item_number'     => $row[2],
                    'description'    => $row[3], 
                    'unit_cost' => $row[4],
                    'extended_cost'    => $row[5], 
                    'status' => $row[6],
                ]);
            }


            
        }
    }
}

}

Please sign in or create an account to participate in this conversation.