I have two tables: Users and Question and I have a form to create questions and an index.blade.php view to show all the questions created. But I have this error, when I want to show the index view with all the questions created:
我有两个表:USERS和QUEST,我有一个用于创建问题的表单和一个用于显示创建的所有问题的index.blade.php视图。但是,当我想要显示包含创建的所有问题的索引视图时,我遇到了这个错误:
Undefined variable $questions
This is my UserModel:
这是我的UserModel:
protected $fillable = [
'name',
'email',
'password',
];
protected $hidden = [
'password',
'remember_token',
'two_factor_recovery_codes',
'two_factor_secret',
];
protected $casts = [
'email_verified_at' => 'datetime',
];
protected $appends = [
'profile_photo_url',
];
//Relationship onetomany with Questions table
public function questions(){
return $this->hasMany(Question::class);
}
This is my Question Model:
这是我的问题模型:
use HasFactory;
protected $fillable = ['question', 'answer', 'user_id'];
//Relationship with User table
public function user(){
return $this->belongsTo(User::class);
}
This is my Controller to send the index view which is in a folder called admin, the collection of created questions
这是我的控制器,用于发送名为admin的文件夹中的索引视图,即创建的问题的集合
public function index()
{
$questions = Question::all();
return view('admin.index', compact('questions'));
}
And this is the foreach in the index to show the questions created, the foreach is inside of a table:
这是索引中的Foreach,用来显示创建的问题,Foreach位于表的内部:
@foreach($questions as $question)
<tr>
<td>1</td>
<td>{{$question->question}}</td>
<td>{{$question->answer}}</td>
</tr>
@endforeach
And this is the route in web.php
这是web.php中的路径
Route::resource('admin/question', QuestionController::class)->middleware('auth')->names('admin.question');
更多回答
Please give more informaton about the error
请提供有关该错误的更多信息
The error message will include a stack trace showing exactly what the problem is. Since the $questions
variable is clearly passed to the view in the controller method you've shown us, the error is likely to be in another view or controller method.
错误消息将包括堆栈跟踪,以准确显示问题所在。因为在您向我们展示的控制器方法中,$Questions变量被清楚地传递给了视图,所以错误很可能出现在另一个视图或控制器方法中。
Go back to basics, rule out the issue being caused by compact
.
If the following works:
回到最基本的问题,排除这个问题是由紧凑型汽车引起的。如果执行以下操作:
return view('admin.index', ['questions' => Question::all()]);
Then further investigation required.
然后需要进一步调查。
- How would
compact
handle a null
assignment, does it skip them?
- Is
Question::all()
returning null
or an empty array
?
- How would
@foreach
handle a null
value?
- Should I ensure an empty array is presented instead of
null
value?
Ask your self, why am I using compact
at all?
Why assign data to a var and cast it to an array item at the cost of performance (creating unnecessary memory pointers makes life for memory garbage collection harder negatively effecting concurrent user performance).
Try Removing the eloquent relationship that's in the User and Question models
尝试删除用户和问题模型中的雄辩关系
- Before using loop, first check the data is empty or containing data
- In case of null you can add ternary operator (eg:
$data->isEmpty()
in case of collection, empty($data)
in case of array)
更多回答
Null and undefined are 2 very different and unrelated things. The error quoted in the question specifically says the variable is undefined so it's likely the error is in some code not shown in the question.
NULL和UNDEFING是两个截然不同且互不相关的事物。问题中引用的错误明确表示变量未定义,因此该错误很可能存在于问题中未显示的某些代码中。
compact()
does not skip null variables. If $questions
were null or empty, the error would not say "undefined variable" it would be a warning from foreach
"argument must be of type array|object, null given." Regardless, Eloquent collection functions always return a Collection object, either empty or populated.
COMPACT()不跳过空变量。如果$Questions为NULL或空,则错误不会显示为“UNDEFINED VARIABLE”,而是来自Foreach的警告:“参数必须是类型ARRAY|OBJECT,给定为NULL。”无论如何,雄辩的集合函数总是返回一个集合对象,要么是空的,要么是填充的。
Please add some explanation to your answer such that others can learn from it
请在你的回答中加上一些解释,以便其他人能从中学习
Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.
您的答案可以通过其他支持信息来改进。请编辑以添加更多详细信息,如引用或文档,以便其他人可以确认您的答案是正确的。你可以在帮助中心找到更多关于如何写出好答案的信息。
我是一名优秀的程序员,十分优秀!