【发布时间】:2021-08-04 07:46:45
【问题描述】:
我正在使用Laravel Excel package 从 Excel 电子表格文件中导入数据。在导入过程中,我需要验证外部数据库表中特定列的某些列值是否存在。
外部模型Catalog.php 看起来像:
<?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';
}
现在InformationImport.php MaatWebsite 类看起来像:
<?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'),
];
}
//...
}
问题是这部分Rule::exists('App\Models\External\Catalogs\Catalog,name') 没有正确验证。如果该目录中实际上不存在某个值,则exists 没有错误。
我错过了什么?
【问题讨论】:
标签: laravel-validation laravel-excel