Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/295.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/58.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php Laravel验证控制器存储中的变量_Php_Mysql_Laravel_Validation - Fatal编程技术网

Php Laravel验证控制器存储中的变量

Php Laravel验证控制器存储中的变量,php,mysql,laravel,validation,Php,Mysql,Laravel,Validation,我找不到关于我的问题的答案,所以我希望有人能帮助我 我想验证我是否添加了一个新的约会,即所选员工在约会当天没有被选中。所以我不能一天两次预订一个人。 我正在使用laravel 5.6和MySQL,并使用以下行进行表预约: id、日期、员工id和资源id 我的控制器是一个资源控制器(具有索引、创建、存储等功能) 因此,如果$appointmentExists为1,我需要抛出一个错误并停留在创建表单的同一页上 public function store(Request $request) {

我找不到关于我的问题的答案,所以我希望有人能帮助我

我想验证我是否添加了一个新的约会,即所选员工在约会当天没有被选中。所以我不能一天两次预订一个人。 我正在使用laravel 5.6和MySQL,并使用以下行进行表预约: id、日期、员工id和资源id

我的控制器是一个资源控制器(具有索引、创建、存储等功能)

因此,如果$appointmentExists为1,我需要抛出一个错误并停留在创建表单的同一页上

public function store(Request $request)
{
    $appointmentExist = \DB::table('appointments')
        ->where([
            ['day','=',$request->day],
            ['employee_id','=',$request->employee_id],
        ])
        ->exists();
    $request->validate([
        'day' => 'required|min:1|max:10',
        'employee_id' => 'required',
        'resource_id' => 'required',
        $appointmentExist => 'in:0',
    ]);
    $appointment = Appointment::create(['day' => $request->day, 'employee_id' => $request->employee_id, 'resource_id' => $request->resource_id]);
    return redirect('/appointments/' . $appointment->id);
}

我希望有人能帮助我,所以我自己找到了答案,也许其他人可以使用它:

if(\DB::table('appointments')
    ->where([
        ['day','=',$request->day],
        ['employee_id','=',$request->employee_id],
    ])
    ->exists()){
        return redirect()->back()->withErrors(['This employee is already busy for that day, select another employee or another day.']);
    };
所以现在我的回答是错误的“这名员工那天已经很忙了,…”。
我还没有找到如何从$request->validate()返回错误,但在这种情况下我不需要。如果你知道,请随时告诉我。

所以我自己找到了答案,也许其他人可以使用它:

if(\DB::table('appointments')
    ->where([
        ['day','=',$request->day],
        ['employee_id','=',$request->employee_id],
    ])
    ->exists()){
        return redirect()->back()->withErrors(['This employee is already busy for that day, select another employee or another day.']);
    };
所以现在我的回答是错误的“这名员工那天已经很忙了,…”。
我还没有找到如何从$request->validate()返回错误,但在这种情况下我不需要。如果您知道,请随时告诉我。

您的问题是这一行:

$appointmentExist => 'in:0',
这是在检查数组($request->input($appointmentExist),[0])中的
,但是
$request->input($appointmentExist)
将检查
$request->input(0)
$request->input(1)
,这两者在技术上都不存在

我将更改为使用请求添加:

$exists = \DB::table(...)->exists(); // Same query, just assigned to a variable
$request->request->add(["exists", $exists]);

$request->validate([
  ...,
  "exists" => "in:0"
]);
通过向请求有效负载添加键
“exists”
,您可以像验证请求中发送的实际数据一样验证它,并立即返回所有错误

根据@N.B.的评论,上述内容只会阻止这种情况下的双重预订;如果验证失败,将永远不会调用
Appointment::create()
,也不会插入数据

考虑到这一点,如果验证意外通过,最好有一个回退,在这种情况下,
员工id
的组合上有一个
唯一的
约束,如果您真的想防止重复预订,并这样处理:

try {
  Appointment::create(...);
catch (\Illuminate\Database\QueryException $qex){
  \Log::error("Unable to Create Appointment: ".$qex->getMessage());

  // Handle individual codes
  if($qex->getCode() == "23000"){
    return redirect()->back()->withErrors(...);
  }
}

您的问题是这一行:

$appointmentExist => 'in:0',
这是在检查数组($request->input($appointmentExist),[0])
中的
,但是
$request->input($appointmentExist)
将检查
$request->input(0)
$request->input(1)
,这两者在技术上都不存在

我将更改为使用请求添加:

$exists = \DB::table(...)->exists(); // Same query, just assigned to a variable
$request->request->add(["exists", $exists]);

$request->validate([
  ...,
  "exists" => "in:0"
]);
通过向请求有效负载添加键
“exists”
,您可以像验证请求中发送的实际数据一样验证它,并立即返回所有错误

根据@N.B.的评论,上述内容只会阻止这种情况下的双重预订;如果验证失败,将永远不会调用
Appointment::create()
,也不会插入数据

考虑到这一点,如果验证意外通过,最好有一个回退,在这种情况下,
员工id
的组合上有一个
唯一的
约束,如果您真的想防止重复预订,并这样处理:

try {
  Appointment::create(...);
catch (\Illuminate\Database\QueryException $qex){
  \Log::error("Unable to Create Appointment: ".$qex->getMessage());

  // Handle individual codes
  if($qex->getCode() == "23000"){
    return redirect()->back()->withErrors(...);
  }
}
这是一个无效的代码。验证程序将在请求数据中搜索1或0($appointmentExist)。这些密钥永远不会包含在此请求中

尝试使用规则类。例如:

$day = $request->day;

$request->validate([
    'day' => 'required|min:1|max:10',
    'employee_id' => [
        'required',
            Rule::unique('appointments')->where(function ($query) use ($day) {
               return $query->where('day', $day);
            })
        ],
    'resource_id' => 'required'
]);
这是一个无效的代码。验证程序将在请求数据中搜索1或0($appointmentExist)。这些密钥永远不会包含在此请求中

尝试使用规则类。例如:

$day = $request->day;

$request->validate([
    'day' => 'required|min:1|max:10',
    'employee_id' => [
        'required',
            Rule::unique('appointments')->where(function ($query) use ($day) {
               return $query->where('day', $day);
            })
        ],
    'resource_id' => 'required'
]);

如果您询问数据库记录是否存在,这并不意味着jack。双倍预订仍然是可能的。您需要做的是在
[day,employee\u id]
上放置一个唯一的约束,然后数据库肯定不会允许超过1条记录。下一步是插入数据。若记录存在,Laravel将抛出异常。代码23000表示重复记录。你用它来告诉你的用户约会已经被预订了。如果你问数据库记录是否存在,那并不意味着jack。双倍预订仍然是可能的。您需要做的是在
[day,employee\u id]
上放置一个唯一的约束,然后数据库肯定不会允许超过1条记录。下一步是插入数据。若记录存在,Laravel将抛出异常。代码23000表示重复记录。你可以用它来告诉你的用户约会已经被预订。这不会阻止双重预订。@N.B.我看到了你上面的评论。当我执行验证时,我执行
$validator=\validator::make(…)。。。如果($validator->passes()){…};我从未实际使用过
$request->validate()
。这里是否缺少一个步骤?验证本身没有问题,保存该部分时询问数据库是否存在该记录。OP应验证基本信息,如员工id是否存在等。下一部分,验证记录是否存在不进入验证逻辑。只需插入(并预先放置唯一约束)。如果抛出异常,记录就在那里,此时您可以捕捉到消息为“约会存在”的重新抛出
ValidationException
。这样你就100%确信没有“两个用户同时”的问题会咬到你。@N.B.我同意你所说的一切;绝对有更好的方法来处理重复预防。也就是说,这个答案是询问者关于如何一次返回所有验证消息的原始答案的地址,更重要的是关于为什么
$appointmentExist=>'in:0'
的逻辑错误是“无效验证”。不过谢谢你的反馈。请记住,OP还写道:
,所以我不能一天预订两次。
。如果你包括防止双重预订的部分,我会比