简体   繁体   English

如何在更新laravel Lighthouse中的内容时添加@unique验证?

[英]how to add @unique validation while updating content in laravel Lighthouse?

I have Laravel 8 with Lighthouse package for Graph QL,我有 Laravel 8 和用于 Graph QL 的 Lighthouse 包,

I have added @unique directive for "name" attribute, but I am unable to apply this while updating any record, as I want to skip the currently updating record for checking unique,我为“name”属性添加了@unique 指令,但我无法在更新任何记录时应用它,因为我想跳过当前更新的记录以检查唯一性,

Below is schema for update以下是更新的架构

updateRole(id: ID, name: String! @trim @rules(apply : ["unique:roles,name"]), isActive: Int!): Role! updateRole(id: ID, name: String!@trim @rules(apply : ["unique:roles,name"]), isActive: Int!): 角色! @update @更新

When I manually added "id" with rule, it works fine, but I don't know how to make it dynamic rule.当我用规则手动添加“id”时,它工作正常,但我不知道如何使它成为动态规则。

Pass the ID you are receiving from the database to update the data with your Validation rule something like that.传递您从数据库接收到的 ID,以使用类似的验证规则更新数据。

public function update(Request $request, Model $ModelVarible)
{
    $request->validate(["name"=> "unique:tablename,fieldname,{$ModelVarible->id},id"]);
    return redirect('route name');
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM