Php Laravel验证数组变量并将其与模型集合匹配

Php Laravel验证数组变量并将其与模型集合匹配,php,arrays,laravel,validation,eloquent,Php,Arrays,Laravel,Validation,Eloquent,我在laravel中有问题模型,其数据库结构如下: question_id | is_active ------------+----------- 1 | Y 2 | Y 3 | N ... | ... $QnAs = [ '1' => '3', '2' => '5', '4' => '1', '6' => '4', '7' => '2',

我在laravel中有
问题
模型,其数据库结构如下:

question_id | is_active
------------+-----------
     1      |     Y
     2      |     Y    
     3      |     N
    ...     |    ...
$QnAs = [
 '1' => '3',
 '2' => '5',
 '4' => '1',
 '6' => '4',
 '7' => '2',
 '8' => '0',
]
$collections = Questions::where('is_active','Y')->get();
foreach($collections as $collection){
   if(!array_key_exists($collection->question_id,$QnAs)){
     return false; // doesn't find the question id in array key input
   } elseif(!in_array($QnAs[$collection->$question_id],['1','2','3','4'])){
     return false; // input value for this array does not match with required value
   }
}
我有一个数组形式的输入,我想用
问题
模型验证,该数组将包含作为问题id的键和作为问题答案的值,如下所示:

question_id | is_active
------------+-----------
     1      |     Y
     2      |     Y    
     3      |     N
    ...     |    ...
$QnAs = [
 '1' => '3',
 '2' => '5',
 '4' => '1',
 '6' => '4',
 '7' => '2',
 '8' => '0',
]
$collections = Questions::where('is_active','Y')->get();
foreach($collections as $collection){
   if(!array_key_exists($collection->question_id,$QnAs)){
     return false; // doesn't find the question id in array key input
   } elseif(!in_array($QnAs[$collection->$question_id],['1','2','3','4'])){
     return false; // input value for this array does not match with required value
   }
}
验证系统将要求所有活动(
is\u Active==“Y”
问题
,这些问题在数组中作为
question\u id
作为数组的键来表示,每个数组的值为1、2、3或4

我可以通过循环处理每个像这样活跃的问题来实现这一点:

question_id | is_active
------------+-----------
     1      |     Y
     2      |     Y    
     3      |     N
    ...     |    ...
$QnAs = [
 '1' => '3',
 '2' => '5',
 '4' => '1',
 '6' => '4',
 '7' => '2',
 '8' => '0',
]
$collections = Questions::where('is_active','Y')->get();
foreach($collections as $collection){
   if(!array_key_exists($collection->question_id,$QnAs)){
     return false; // doesn't find the question id in array key input
   } elseif(!in_array($QnAs[$collection->$question_id],['1','2','3','4'])){
     return false; // input value for this array does not match with required value
   }
}

这段代码的问题是,当我处理15k++数据时,它将花费很长时间来处理,因为它必须在每个模型集合中循环,是否有其他方法可以简化代码和过程?

问题是您正在获取所有活动问题

所以关键是要限制数据

首先,在
问题id
上添加复合索引,并且
处于活动状态

$table->index(['question\u id','is\u active']);
然后尝试以下代码:

$QnAs=[
'1' => '3',
'2' => '5',
'4' => '1',
'6' => '4',
'7' => '2',
'8' => '0',
];
//允许使用的滤芯:
$valid_QnAs=array();
foreach($k=>v的QnAs){
if(在_数组中($v[1,2,3,4])){
$valid_QnAs[$k]=$v;
}
}
$not_in_active=Questions::where('is_active','Y'))
->whereNotIn('question_id',array_key($valid_QnAs))
->存在();
如果($not_in_active)返回false;

您的代码无法工作。Beacause return将立即结束当前函数的执行。因此,检查将仅使用集合中的第一个值进行,它将基于数组$collections中的第一个元素返回结果

您可以按以下方式更改代码:

$collections = Questions::where('is_active','Y')->get();

$result = true;
foreach($collections as $collection)
{
   if(!array_key_exists($collection->question_id,$QnAs))
   {
     $result = false; // doesn't find the question id in array key input
   } 
}
return $result;
上述代码将起作用
您可以进行更多研究以获得更高的性能

感谢您的回答,如果$QnAs数组值不包含1、2、3或4,则应返回false,无需将其插入其他数组