%PDF- %PDF-
Mini Shell

Mini Shell

Direktori : /var/www/html/hrsys/api/app/Imports/
Upload File :
Create Path :
Current File : /var/www/html/hrsys/api/app/Imports/UsersImport.php

<?php

namespace App\Imports;

use App\Models\Role;
use App\Models\User;
use Maatwebsite\Excel\Concerns\Importable;
use Maatwebsite\Excel\Concerns\ToModel;
use Maatwebsite\Excel\Concerns\WithHeadingRow;
use Maatwebsite\Excel\Concerns\WithValidation;
use Throwable;

class UsersImport extends BaseImportModel implements ToModel, WithHeadingRow, WithValidation
{
    use Importable;

    /**
     * @param array $row
     *
     * @return mixed
     * @throws Throwable
     */
    public function model(array $row)
    {
        if ($row['name']) {
            $rolesSlugs = explode(',', str_replace(' ', '', $row['role']));
            $roles = Role::query()
                         ->whereIn('name', $rolesSlugs)
                         ->pluck('id');
            /** @var User $user */
            $user = User::createItem([
                'name'               => $row['name'],
                'surname'            => $row['surname'],
                'email'              => $row['email'],
                'phone'              => $row['phone'],
                'birthday'           => $this->transformDate($row['birthday']),
                'role_ids'           => $roles,
                'configurations'     => [
                    'valid_from'                   => $this->transformDate($row['started_working_date']),
                    'expected_daily_working_hours' => $row['expected_daily_working_hours'],
                ],
                'external_reference' => array_key_exists('external_reference', $row)
                    ? $row['external_reference']
                    : null,
            ]);
            return $user;
        }
    }

    public function rules(): array
    {
        return [
            'name'                         => 'string',
            'surname'                      => 'string',
            'email'                        => 'email|unique:users,email',
            'phone'                        => 'unique:users,phone',
            'role'                         => 'string',
            'expected_daily_working_hours' => 'integer',
        ];
    }

    public function customValidationMessages()
    {
        return [
            'name'                                 => 'Name column can not be empty',
            'surname'                              => 'Surname column can not be empty',
            'phone'                                => 'Phone column can not be empty',
            'birthday'                             => 'Birthday column can not be empty',
            'role.require'                         => 'Role column can not be empty',
            'expected_daily_working_hours.integer' => 'Expected daily hours column must be a number',
            'email.unique'                         => 'Email must be unique',
            'phone.unique'                         => 'Phone must be unique',
        ];
    }

}

Zerion Mini Shell 1.0