dimanche 23 mai 2021

check if a field is empty while importing data from excel file in Laravel?

I am trying to import data from excel file into database tables in Laravel. I have successfully imported the data but If I mistakenly leave a field empty then I get error that column cannot be null. So I need check if the all necessary data is provided. I use this code.

IndependentContractor::create(['staff_id' => $staff->id, 'address' => $excel_row['address'],  'longitude' => $excel_row['longitude'], 'latitude' => $excel_row['latitude'], 'tax_id' => $excel_row['tax_id'], 'business_name' => $excel_row['business_name'], 'person_incharge' => $excel_row['person_incharge'], 'phone_of_person_incharge' => $excel_row['phone_of_person_incharge'], 'general_manager' => $excel_row['general_manager'], 'phone_of_general_manager' => $excel_row['phone_of_general_manager']]);

I can use If() to check the data but I will have to repeat this in almost 7 places because there are 7 different tables in which data is being stored. also if statement will look like this.

if(!empty($excel_row['address']) && !empty($excel_row['longitude']) && !empty($excel_row['latitude']) && !empty($excel_row['bussiness_name']) and so on )

So is there any better way to achieve this?

Thanks.

Aucun commentaire:

Enregistrer un commentaire