0

ImportController

<?php

namespace App\Http\Controllers;

use App\Models\User;
use App\Talent\ImportData\Requests\ImportDataRequest;
use Illuminate\Support\Facades\Hash;
use Rap2hpoutre\FastExcel\FastExcel;

class ImportController extends Controller
{
    public function __construct(private User $user)
    {

    }

    public function import(ImportDataRequest $request)
    {
        $validated = $request->validated();
        $collection = (new FastExcel)->import($validated['file'], function ($rows) {
            $user = $this->user->create([
                'name' => $rows['First Name'] . " " . $rows['Last Name'],
                'email' => $rows['Email'],
                'password' => Hash::make('Introcept@123'),
                'role' => $rows['Role']
            ]);
            $employee = $user->employees()->create([
                'status' => $rows['Status'],
                'first_name' => $rows['First Name'],
                'last_name' => $rows['Last Name'],
                'email' => $rows['Email'],
                'contact_number' => $rows['Contact Number'],
                'date_of_birth' => $rows['Date of Birth'],
                'current_address' => $rows['Current Address'],
                'pan_number' => $rows['Pan Number'],
                'bank_account_number' => $rows['Bank Account Number'],
            ]);
            $education = $employee->education()->create([
                'education_level' => $rows['Education Level'],
                'passed_year' => $rows['Passed Year'],
                'institution' => $rows['Institution'],
            ]);
            $employment = $employee->employment()->create([
                'organization' => $rows['Organization'],
                'join_date' => $rows['Join Date'],
                'current_position' => $rows['Current Position'],
                'work_schedule' => $rows['Work Schedule'],
                'team' => $rows['Team'],
                'manager' => $rows['Manager'],
                'superpowers' => $rows['Superpower'],
            ]);
            $employment->manages()->create([
                'employee_id' => $rows['Manages'],
            ]);
        });
    }
}

ImportData Request

<?php

namespace App\Talent\ImportData\Requests;

use Illuminate\Foundation\Http\FormRequest;

class ImportDataRequest extends FormRequest
{
    /**
     * Determine if the user is authorized to make this request.
     *
     * @return bool
     */
    public function authorize()
    {
        return true;
    }

    /**
     * Get the validation rules that apply to the request.
     *
     * @return array<string, mixed>
     */
    public function rules()
    {
        return [
            'file'=>'required|mimes:csv,xlsx'
        ];
    }
}

I am creating an API where the admin can import data from an excel file. Here, I am using the Fast Excel package in order to import data. Everything is working fine, but I have no idea how I can validate excel data before saving it into the database. I am pretty new to Laravel and any help will be really appreciated. Thank you

0 Answers0