laravel 4验证在两列中是唯一的 [英] laravel 4 validation unique on two columns

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

问题描述

我正在Laravel中进行一些验证,我想确保字段uid与project_id唯一.例如,我的数据库表如下:

Hi I'm working on some validation in Laravel and I want to ensure that the field uid is unique with a project_id. For example my database table is as follows:

id | project_id | uid        |
1  | 3          | task_uid   |
2  | 4          | task_uid2  |
3  | 4          | task_uid   |

uid在project_id中可以存在两次,但如果project_id相同,则不能存在.

A uid can exist twice within a project_id but not if the project_id is the same.

我尝试了以下方法;

public static $rules = array(
    'UID' => 'required|unique:uid,project_id'
);

但是这没有用,并返回以下sql错误;

However this hasn't worked and returns the following sql error;

"SQLSTATE[42S02]: Base table or view not found: 1146 Table 'project.uid' doesn't exist (SQL: select count(*) as aggregate from `uid` where `project_id` = task_uid)"

有什么想法可以检查任务表以确保project_id和uid是唯一值吗?

Any ideas how I may be able to check the tasks table to ensure that the project_id and uid are unique values??

推荐答案

**请注意:这是针对Laravel 4 **

** PLEASE TAKE NOTE: This is for Laravel 4 **

独特工作方式(在 laravel 4 中):

The way unique works (In laravel 4):

unique:{tableName},{columnName}

尝试

public static $rules = array(
    'UID' => 'required|unique:{tableName},uid|unique:{tableName},project_id'
);

**请注意:这是针对Laravel 4 ****

** PLEASE TAKE NOTE: This is for Laravel 4 ****

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

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