我有一个遍历数组的 foreach 循环,并以雄辩的方式保存数据。当它像这样时它工作正常:
foreach($questions['questions'] as $question) {
$questionObject = Question::create([
'external_id' => $question['id'],
'text' => $question['question_text'],
'type' => $question['question_type'],
'image' => (array_key_exists('question_image', $question)) ?
$question['question_image'] : ''
]);
}
但是当我添加 if 条件时,我得到未定义的变量问题错误。
foreach($questions['questions'] as $question) {
if(!$question = Question::where('id', $question['id'])->where(
function($query){
$query->where('updated_at','<', $question['updated']);
}})->first()) {
$questionObject = Question::create([
'external_id' => $question['id'],
'text' => $question['question_text'],
'type' => $question['question_type'],
'image' => (array_key_exists('question_image', $question)) ?
$question['question_image'] : ''
]);
} else {
return 'Question: '.$question['external_id'].' already exist.';
}
}
如果有人可以帮助我,将非常感谢,提前谢谢!