Laravel Blade文件中的未定义变量
我是新手Laravel,请帮助我解决这个错误。
这是我的控制器函数“编辑”
public function edit($id)
{
$user = User::find($id);
return view('users.edit-profile',['user'=>$user]);
}
我的视图用户
<div class=" mb-4" style="border: 1px solid #979797; border-radius: 4px;width: 360px;height: 40px;margin-left: 200px;" >
<input type="text" name="name" id="name" onkeyup="isEmpty()" value="{{$user->name}}" class="form-control" >
</div>
这
Route::get('/edit_profile',[profileController::class,'index']);
Route::get('/edit_profile/{id}',[profileController::class,'edit'])->name('edit_profile');
是
Undefined variable: user (View: C:\xampppp\htdocs\clipboard_nation\resources\views\users\edit-profile.blade.php)
。
I am new to laravel kindly help me out to solve this error.
This is my controller function "edit"
public function edit($id)
{
$user = User::find($id);
return view('users.edit-profile',['user'=>$user]);
}
This is my view users.edit-profile
<div class=" mb-4" style="border: 1px solid #979797; border-radius: 4px;width: 360px;height: 40px;margin-left: 200px;" >
<input type="text" name="name" id="name" onkeyup="isEmpty()" value="{{$user->name}}" class="form-control" >
</div>
This is the route
Route::get('/edit_profile',[profileController::class,'index']);
Route::get('/edit_profile/{id}',[profileController::class,'edit'])->name('edit_profile');
This is the error
Undefined variable: user (View: C:\xampppp\htdocs\clipboard_nation\resources\views\users\edit-profile.blade.php)
this error display that the $user variable that I use in blade file is not defined.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
尝试一下
Try this
由于$用户不存在,因此错误被丢弃在刀片文件中。
一个选项是,如果不存在该变量,则通过给出默认值,以防止直接在刀片文件中发生错误。
The error is being thrown in the blade file as $user does not exist.
An option would be to prevent that error occurring directly in the blade file, by giving a default value if the variable does not exist.
尝试一下
try this
我也有类似的问题,尝试将用户数据添加到索引方法
I had similar problem, try to add the user data to index method too