【问题标题】:laravel Rule::exists by connection and columnlaravel Rule::exists by connection and column
【发布时间】: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


    【解决方案1】:

    可能有点晚了,但是如果你使用 Rule::exists() 你必须这样传递它: Rule::exists(App\Models\External\Catalogs\Catalog::class, 'name');

    【讨论】:

      猜你喜欢
      • 2020-01-19
      • 1970-01-01
      • 2021-10-21
      • 1970-01-01
      • 2019-11-04
      • 2014-04-04
      • 1970-01-01
      • 2021-07-03
      • 1970-01-01
      相关资源
      最近更新 更多