我对我的表单请求进行了自定义验证.当用户输入值并且值也存在时,字段DELETED_AT不为空,则该验证应该起作用.

问题是,即使我输入了不存在的值,Delete_at也是空的.验证仍在起作用.

这就是我所做的

自定义验证:

<?php

namespace App\Rules;

use Closure;
use Illuminate\Contracts\Validation\Rule;
use App\Models\Product;

class SkuDeletedValidationRule implements Rule
{
    /**
     * Run the validation rule.
     *
     * @param  \Closure(string): \Illuminate\Translation\PotentiallyTranslatedString  $fail
     */
    public function passes($attribute, $value)
    {
        // Retrieve the model instance based on the SKU value
        $model = Product::where('sku', $value)->first();

        // Check if the model exists and its deleted_at is not null
        if( $model && $model->deleted_at !== null){
            return true;
        }
        return false;
    }

    public function message()
    {
        return 'The :attribute is exists but has been deleted, please restore it.';
    }
}

我的表格请求:

use App\Models\Product;
use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Support\Str;
use App\Rules\SkuDeletedValidationRule;


class ProductRequest extends FormRequest
{
    /**
     * Determine if the user is authorized to make this request.
     */
    public function authorize(): bool
    {
        return auth('cms-admin')->check();
    }

    /**
     * Get the validation rules that apply to the request.
     *
     * @return array<string, \Illuminate\Contracts\Validation\ValidationRule|array|string>
     */
    public function rules(): array
    {
        return [
            'name' => 'required',
            'sku' => [new SkuDeletedValidationRule, 'required' . $this->product?->id],
            'sku_merchant' => 'nullable|unique:products,sku_merchant,' . $this->product?->id,
            'slug' => 'nullable',
            'is_publish' => 'required',
            'price' => 'required',
            'description' => 'required',
            'discount' => 'nullable',
            'images.*' => 'required',
            'category.*' => 'nullable|exists:categories,id',
            'supplier_id' => 'required',
            'buyer_price' => 'required'
        ];
    }
}

我的控制器:

    public function store(ProductRequest $request)
    {
        $this->productService->create($request->validated());

        return redirect(route('admin.product.index'))->with('success', 'product created!');
    }

当我输入不存在的值时

non existing value input

在我的自定义验证中需要修复什么才能正常工作?提前谢谢你了.

推荐答案

要检索已被软删除的数据,您需要使用with Trash()方法.

只需修改以下行:

$model = Product::where('sku', $value)->first();

$model = Product::withTrashed()->where('sku', $value)->first();

根据您的验证消息,我假设当您发现具有已被软删除的相同SKU的数据时,应该返回FALSE.

/**
 * Run the validation rule.
 *
 * @param  \Closure(string): \Illuminate\Translation\PotentiallyTranslatedString  $fail
 */
public function passes($attribute, $value)
{
    // Retrieve the model instance based on the SKU value
    $model = Product::withTrashed()->where('sku', $value)->first();

    // Check if the model exists and its deleted_at is not null
    if( $model && $model->deleted_at !== null){
        return false;
    }
    return true;
}

Laravel相关问答推荐

如何从laravel中的另一个表中获取用户名

Laravel + Plupload 上传到 S3 响应以进行预检无效 - CORS

如何访问 Validator::extend 中的其他输入属性?

判断 Laravel 中是否存在唯一索引键

Laravel 5 - 手动分页

Laravel Auth::attempt() 返回 false

带有消息...必须返回关系实例的 LogicException.

PHP 5.5,什么情况下PHP会导致很高的committed memory

你如何在自定义 Laravel Nova 工具中使用确认对话?

SQLSTATE [42000]:语法错误或访问冲突:1066 Not unique table/alias on relationship

如何在 Laravel 中创建类别的嵌套列表?

调用未定义的方法 Illuminate\Foundation\Application::bindShared()

数组的旧输入?

控制器外部的 Laravel 访问请求对象

如何将对象(模型类型对象)插入到 Laravel 中特定索引号的集合对象中?

Laravel 在保存前生成 slug

如何获取 Laravel 块的返回值?

如何使用 Laravel 迁移在 Mysql 列中添加注释

没有Access-Control-Allow-Origin标头 - Laravel

Laravel 5.1 date_format 验证允许两种格式