diff --git a/app/Imports/AirportsImport.php b/app/Imports/AirportsImport.php index 0be0c359..26b08d87 100644 --- a/app/Imports/AirportsImport.php +++ b/app/Imports/AirportsImport.php @@ -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([ @@ -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';