我在表服务器中有2列。
我有列ip
和hostname
。
我有验证:
'data.ip' => ['required', 'unique:servers,ip,'.$this->id]
这仅适用于列ip
。但是如何做到这一点,以及列hostname
?
我想要使用列ip
和hostname
验证data.ip。
因为当用户写ip时,可以在列ip和hostname中重复。
答案 0 :(得分:13)
您可以使用Rule::unique
来实现验证规则
$messages = [
'data.ip.unique' = 'Given ip and hostname are not unique',
];
Validator::make($data, [
'data.ip' => [
'required',
Rule::unique('servers')->where(function ($query) use($ip,$hostname) {
return $query->where('ip', $ip)
->where('hostname', $hostname);
}),
],
],
$messages
);
答案 1 :(得分:5)
以下内容适用于创建
'data.ip' => ['required', 'unique:servers,ip,'.$this->id.',NULL,id,hostname,'.$request->input('hostname')]
以及更新的以下内容
'data.ip' => ['required', 'unique:servers,ip,'.$this->id.','.$request->input('id').',id,hostname,'.$request->input('hostname')]
我假设id
是您表中的主键。将其替换为您的环境。
唯一规则的(未记录的)格式为:
表 [,列 [,忽略值 [,忽略列 [, where列, where value ] ...]]]
可以指定多个“where”条件,但只能检查相等性。任何其他比较都需要关闭(如在接受的答案中)。
答案 2 :(得分:5)
主键(在我的情况下)是两列(名称, guard_name )的组合
我在控制器的 create 和 update 方法( PermissionsController )
PermissionsController.php
<?php
namespace App\Http\Controllers;
use App\Permission;
use Illuminate\Http\Request;
use Illuminate\Validation\Rule;
use App\Http\Controllers\Controller;
class PermissionsController extends Controller
{
/**
* Store a newly created resource in storage.
*/
public function store(Request $request)
{
request()->validate([
'name' => 'required|max:255',
'guard_name' => [
'required',
Rule::unique('permissions')->where(function ($query) use ($request) {
return $query
->whereName($request->name)
->whereGuardName($request->guard_name);
}),
],
],
[
'guard_name.unique' => __('messages.permission.error.unique', [
'name' => $request->name,
'guard_name' => $request->guard_name
]),
]);
Permission::create($request->all());
flash(__('messages.permission.flash.created'))->success();
return redirect()->route('permission.index');
}
/**
* Update the specified resource in storage.
*/
public function update(Request $request, Permission $permission)
{
request()->validate([
'name' => 'required|max:255',
'guard_name' => [
'required',
Rule::unique('permissions')->where(function ($query) use ($request, $permission) {
return $query
->whereName($request->name)
->whereGuardName($request->guard_name)
->whereNotIn('id', [$permission->id]);
}),
],
],
[
'guard_name.unique' => __('messages.permission.error.unique', [
'name' => $request->name,
'guard_name' => $request->guard_name
]),
]);
$permission->update($request->all());
flash(__('messages.permission.flash.updated'))->success();
return redirect()->route('permission.index');
}
}
在更新方法中的通知我添加了一个附加的查询约束[ whereNotIn('id',[$ permission-> id])]以忽略当前模型。
resources / lang / zh-CN / messages.php
<?php
return [
'permission' => [
'error' => [
'unique' => 'The combination [":name", ":guard_name"] already exists',
],
'flash' => [
'updated' => '...',
'created' => '...',
],
]
]
flash()方法来自laracasts/flash包。
答案 3 :(得分:2)
尝试此规则:
'data.ip' => 'required|unique:servers,ip,'.$this>id.'|unique:servers,hostname,'.$this->id
答案 4 :(得分:1)
这是演示代码。这将帮助您更好。我尝试涵盖插入和更新方案。
内部 app / Http / Providers / AppServiceProvider.php
Validator::extend('uniqueOfMultiple', function ($attribute, $value, $parameters, $validator)
{
$whereData = [
[$attribute, $value]
];
foreach ($parameters as $key => $parameter) {
//At 0th index, we have table name
if(!$key) continue;
$arr = explode('-', $parameter);
if($arr[0] == 'except') {
$column = $arr[1];
$data = $arr[2];
$whereData[] = [$column, '<>', $data];
} else {
$column = $arr[0];
$data = $arr[1];
$whereData[] = [$column, $data];
}
}
$count = DB::table($parameters[0])->where($whereData)->count();
return $count === 0;
});
在 app / Http / Requests / Something / StoreSometing.php
内部/**
* Get the validation rules that apply to the request.
*
* @return array
*/
public function rules()
{
return [
'name' => 'required|max:225|uniqueOfMultiple:menus,location_id-' . $this->get('location_id', 'NULL') . ',language_id-' . $this->get('language_id', 1),
'location_id' => 'required|exists:menu_location,id',
'order' => 'digits_between:0,10'
];
}
内部 app / Http / Requests / Something / UpdateSomething.php
/**
* Get the validation rules that apply to the request.
*
* @return array
*/
public function rules()
{
return [
'name' => 'required|max:225|uniqueOfMultiple:menus,location_id-' . $this->get('location_id', 'NULL') . ',language_id-' . $this->get('language_id', 'NULL') . ',except-id-' . $this->route('id', 'NULL'),
'location_id' => 'required|exists:menu_location,id',
'order' => 'digits_between:0,10'
];
}
在 resources / lang / zh-CN / validation.php
内部'unique_of_multiple' => 'The :attribute has already been taken under it\'s parent.',
在此代码中,使用的自定义验证为uniqueOfMultiple
。传递的第一个参数是table_name,即menus
,所有其他参数是column_name,并且用逗号分隔。此处使用的列是name
(主列),location_id
,language_id
和一个用于更新情况的{-{1}}除外列。为所有三个传递的值是except-id
分隔的。
答案 5 :(得分:0)
表格
服务器
字段
id
primary key
ip
should be unique with hostname
主机名
should be unique with ip
我在这里验证Ip,并且主机名应该是唯一的。
use Illuminate\Validation\Rule;
$ip = '192.168.0.1';
$host = 'localhost';
Validator::make($data, [
'ip' => [
'required',
Rule::unique('server')->where(function ($query) use($ip,$host) {
return $query->where('ip', $ip)->where('hostname', $host);
});
],
]);
在RULE
之后添加忽略
Validator::make($data, [
'ip' => [
'required',
Rule::unique('server')->where(function ($query) use($ip,$host) {
return $query->where('ip', $ip)->where('hostname', $host);
})->ignore($serverid);
],
]);
答案 6 :(得分:0)
这对我来说既可以创建也可以更新。
[
'column_1' => 'required|unique:TableName,column_1,' . $this->id . ',id,colum_2,' . $this->column_2
]
注意:已在Laravel 6中测试。
答案 7 :(得分:0)
这对我有用(由movie_id,actor_id,name唯一): (Laravel版本7.x)
/**
* Get the validation rules that apply to the request.
*
* @return array
*/
public function rules()
{
return [
'movie_id' => 'required',
'actor_id' => 'required',
'name' => [
'required',
'string',
'min:1',
'max:255',
Rule::unique('movie_characters')
->where('movie_id',$this->request->get('movie_id'))
->where('actor_id',$this->request->get('actor_id'))
]
];
}
答案 8 :(得分:0)
使用表单请求:
public function rules() {
'ip' => [
'required',
Rule::unique('server')->where(function ($query) {
$query->where('ip', $this->ip)
->where('hostname', $this->host);
})
],
}
public function messages() {
return [
'ip.unique' => 'Combination of IP & Hostname is not unique',
];
}
在最后添加忽略
public function rules() {
'ip' => [
'required',
Rule::unique('server')->where(function ($query) {
$query->where('ip', $this->ip)
->where('hostname', $this->host);
})->ignore($this->server->id)
],
}
答案 9 :(得分:0)
对我来说,laravel 8 是有效的
$req->验证([ 'house_no' => [ Rule::unique('house')->where('house_no', $req->input('house_no'))->where('ward_no', $req->input('ward_no')) ], ]);
答案 10 :(得分:-1)
public function store(Request $request)
{
$this->validate($request, [
'first_name' => 'required|regex:/^[\pL\s\-]+$/u|max:255|unique:contacts,first_name, NULL,id,first_name,'.$request->input('last_name','id'),
'last_name'=>'required|regex:/^[\pL\s\-]+$/u|max:255|unique:contacts,last_name',
'email' => 'required|email|max:255|unique:contacts,email',
'job_title'=>'required',
'city'=>'required',
'country'=>'required'],
[
'first_name.regex'=>'Use Alphabets Only',
'email.unique'=>'Email is Already Taken.Use Another Email',
'last_name.unique'=>'Contact Already Exist!. Try Again.',
]
);