0

I am using the Laravel Excel package to import data from an Excel spreadsheet file. During the importing, I need to verify that some column values exist in an external database table at a specific column.

The external model Catalog.php looks like:

<?php

namespace App\Models\External\Catalogs;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

class Catalog extends Model
{
    protected $connection='external';

    protected $table='catalogs';
}

Now the InformationImport.php MaatWebsite class looks like:

<?php

namespace App\Imports;

use App\Models\External\Catalogs\Catalog;
use Illuminate\Validation\Rule;
use Maatwebsite\Excel\Concerns\Importable;
use Maatwebsite\Excel\Concerns\OnEachRow;
use Maatwebsite\Excel\Concerns\SkipsUnknownSheets;
use Maatwebsite\Excel\Concerns\WithHeadingRow;
use Maatwebsite\Excel\Concerns\WithMultipleSheets;
use Maatwebsite\Excel\Concerns\WithValidation;
use Maatwebsite\Excel\Row;

class InformationImport implements OnEachRow, WithValidation, WithHeadingRow, SkipsUnknownSheets, WithMultipleSheets
{
    //...

    public function rules(): array
    {
        return [
            'catalog_type'=>'required|max:255',Rule::exists('App\Models\External\Catalogs\Catalog,name'),
        ];
    }

    //...
}

The problem is that this part Rule::exists('App\Models\External\Catalogs\Catalog,name') is not validating correctly. If there's a value that does not actually exist in that Catalog, there's no error concerning the exists.

What am I missing?

Pathros
  • 10,042
  • 20
  • 90
  • 156

2 Answers2

0

Probably a little bit late, but if you use Rule::exists() you have to pass it this way: Rule::exists(App\Models\External\Catalogs\Catalog::class, 'name');

peta
  • 11
  • 2
0
return [
            'catalog_type'=>'required|max:255|' . Rule::exists('catalogs_table', 'name')
        ];