Laravel中唯一的两列 [英] Unique two columns in laravel

查看:87
本文介绍了Laravel中唯一的两列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用laravel 5.6,我有此表

I'm working with laravel 5.6 I have this table

users_groups table

这些列user_id引用users自动增量ID,而group_id引用groups表中的组自动增量ID

with these columns user_id reference to users auto increment id and group_id reference to group auto increment id in groups table

现在,我正在尝试验证数据条目是否为两列的唯一值,但是表中的user_idgroup_id不能相同. 我找到了此代码并进行了尝试:

Now I'm trying to validate the entry of data to be unique value for both columns together, but it can't be user_id and group_id the same in the table. I found this code and tried it:

    $validatedData = $request->validate([
        'user_id' => 'required|unique_with:users_groups,group_id',
        'group_id' => 'required|unique_with:users_groups,user_id',
    ]);

它给了我这个错误:

方法Illuminate \ Validation \ Validator :: validateUniqueWith不存在.

Method Illuminate\Validation\Validator::validateUniqueWith does not exist.

请帮忙吗?

推荐答案

针对数据库中的表进行验证的正确方法是使用unique:table name,column

The correct way to validate against a table in a database is to use unique:table name,column

您的情况应该是

    $validatedData = $request->validate([
    'user_id' => 'required|unique:users_groups,group_id',
    'group_id' => 'required|unique:users_groups,user_id',
]);

请参见 laravel文档

这篇关于Laravel中唯一的两列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆