Skip to content

Commit

Permalink
feat(AirportsImport): add better validation to airport import
Browse files Browse the repository at this point in the history
In case icao or iata are empty, skip it
  • Loading branch information
daveroverts committed Jan 2, 2022
1 parent bb0859f commit 6b1ae83
Showing 1 changed file with 19 additions and 5 deletions.
24 changes: 19 additions & 5 deletions app/Imports/AirportsImport.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,27 @@
namespace App\Imports;

use App\Models\Airport;
use Illuminate\Validation\Rule;
use Maatwebsite\Excel\Concerns\Importable;
use Maatwebsite\Excel\Concerns\SkipsFailures;
use Maatwebsite\Excel\Concerns\SkipsOnFailure;
use Maatwebsite\Excel\Concerns\ToModel;
use Maatwebsite\Excel\Concerns\WithBatchInserts;
use Maatwebsite\Excel\Concerns\WithChunkReading;
use Maatwebsite\Excel\Concerns\WithHeadingRow;
use Maatwebsite\Excel\Concerns\WithUpserts;
use Maatwebsite\Excel\Concerns\WithValidation;

class AirportsImport implements ToModel, WithBatchInserts, WithChunkReading, WithHeadingRow, WithUpserts
class AirportsImport implements ToModel, WithBatchInserts, WithChunkReading, WithHeadingRow, WithUpserts, WithValidation, SkipsOnFailure
{
use Importable;
use SkipsFailures;

/**
* @param array $row
*
* @return \Illuminate\Database\Eloquent\Model|null
*/
* @param array $row
*
* @return \Illuminate\Database\Eloquent\Model|null
*/
public function model(array $row)
{
return new Airport([
Expand All @@ -28,6 +33,15 @@ public function model(array $row)
]);
}

public function rules(): array
{
return [
'icao' => ['required', 'string', Rule::unique('airports', 'icao')],
'iata' => ['required', 'string', Rule::unique('airports', 'iata')],
'name' => ['required', 'string'],
];
}

public function uniqueBy()
{
return 'icao';
Expand Down

0 comments on commit 6b1ae83

Please sign in to comment.