Excel file Import to database laravel with Validation

PHOTO EMBED

Sun Apr 09 2023 15:47:50 GMT+0000 (Coordinated Universal Time)

Saved by @ahmad007

1. install package by using following command or for latest instructions check laravel excel websitecomposer require maatwebsite/excel
2. Then in providers write the following code
'providers' => [
    /*
     * Package Service Providers...
     */
    Maatwebsite\Excel\ExcelServiceProvider::class,
]
3. Then in aliases write the following code
'aliases' => [
    ...
    'Excel' => Maatwebsite\Excel\Facades\Excel::class,
]
4. To publish the config, run the vendor publish command:
php artisan vendor:publish --provider="Maatwebsite\Excel\ExcelServiceProvider" --tag=config
5. Then make a controller called BulkEmployeeController
6. The write the following routes
Route::get('/bulk-employee-create', [BulkEmployeeController::class, 'create_bulk'])->name('bulk.employee.create');
Route::get('/export-employees', [BulkEmployeeController::class, 'export_employees'])->name('export.employees');
Route::post('/import-employees', [BulkEmployeeController::class, 'import_employees'])->name('import.employees');

7. Then create import and export classes by using commands
php artisan make:import EmployeeImport --model=Employee
php artisan make:export EmployeeExport --model=Employee
Then in import class write the following code while in export class no need to write anything
 'first_name' => $row['0'],
            'last_name' => $row['1'],
            'company_id' => $row['2'],
            'email' => $row['3'],
            'phone' => $row['4'],
8. Then in the model Employee write the folloing code for bulk insert fillable
protected $fillable = ['first_name', 'last_name', 'company_id', 'email', 'phone'];
9. Then in import class write the following methods for import and export
 public function import_employees(Request $request)
    {
        Excel::import(new EmImport, $request->file('file'));

        return redirect()->route('employees.index');
    }

    public function export_employees()
    {
        return Excel::download(new EmployeemExport, 'employees.xlsx');
    }
here please remember to import import, export and Excel classes in the header section
use App\Imports\EmImport;
use App\Exports\EmployeemExport;
use Maatwebsite\Excel\Facades\Excel;


-------------------Validation---------------
  use the followings in the head of import Class
  
use Maatwebsite\Excel\Concerns\WithHeadingRow;
use Maatwebsite\Excel\Concerns\WithValidation;
use Maatwebsite\Excel\Concerns\SkipsErrors;
use Maatwebsite\Excel\Concerns\SkipsOnError;

then edit the code to that 
class EmImport implements ToModel,WithHeadingRow,WithValidation, SkipsOnError

then inside the class
  use Importable, SkipsErrors;

Now write the rules with code like following
public function rules(): array
    {
        return [
            'first_name' => 'required',
            '*.first_name' => 'required',
            'last_name' => 'required',
            '*.last_name' => 'required',
            'email' => 'required',
            '*.email' => 'required',
        ];
    }
then in create bulk page write the following code before bulk upload form to show errors
@if (isset($errors) && $errors->any())
            <div class="alert alert-danger">
                @foreach ($errors->all() as $error)
                    {{$error}}
                @endforeach
            </div>
        @endif
content_copyCOPY

Programming Experiance and Qiro Lab Youtube Channels