php - 未定义变量:Laravel 中的访问者
问题描述
你好,我想在博客中添加评论,显示添加的患者姓名,使用会话,所以我收到此错误:Undefined variable: visiteur
这是commentcontroller.php:
public function store (blog $getid , Request $request)
{
$visitor=patient::orderBy('created_at','desc')->get() ;
$patient_id=$request->session()->get('patient_id');
comment::create([
'body' =>request('body'),
'blog_id'=> $getid->id,
'patient_id'=>$patient_id
]);
return back()->with(['visiteur'=>$visitor]);
}
}
这是视图:show.blade.php
<div class="card-body" >
@foreach ($showme->comments as $comment)
<blockquote class="blockquote mb-0">
<p style="font-size:15px;">{{$comment->body}}</p>
@foreach ($visiteur as $viis)
@if ($comment->patient_id == $viis->id)
<p> {{$comment->patient_id}}</p>
@endif
@endforeach
<footer class="blockquote-footer"> {{$comment->created_at}} <cite title="Source Title"> </cite></footer>
</blockquote>
@endforeach
</div>
<!-- si le patient est connceter -->
@if(Session::has('log_in'))
<div class="card-block">
<form method="POST" action="/blog/{{$showme->id}}/store" >
@csrf
<div class="form-group">
<label> Commentaire </label> </br>
<textarea name="body" rows="3" cols="80" cols="form-control"></textarea> </br>
</div>
<div class="form-group">
<button type="submit" class="btn btn-primary"> Ajouter commentaire</button>
</div>
</form>
</div>
@endif
最后这是web.php
Route::Post('/store' , 'patientcontroller@store');
这是患者控制器:
public function welcome(Request $request)
{
$request->validate([
'mail' => 'required|email',
'mdp' => 'required',
]);
// since email is unique. no need to grab all data
$patient = patient::where('Login', $request->input('mail'))->first();
// if patient exists
if ($patient) {
// check hashed password
// assuming $patient->Password hashed
if (Hash::check($request->input('mdp'), $patient->Password)) {
//session
$request->session()->put('patient_id', $patient->Nom);
$request->session()->put('log_in', true);
return redirect ('index');
}
}
return back()->withErrors([
'message' => 'Emails or password not correct!',
]);
}
我想显示添加评论的用户的姓名,但他只显示 ID
解决方案
应该
return back()->with(['visiteur', $visitor]);
推荐阅读
- python - numpy (logical_and vs '&') 语句的顺序导致不同的结果
- django - 从数据库动态设置 Django 设置变量
- php - 在 PHP 网站中使用 Echo标记作为下载链接
- node.js - 使用 Docker 创建 nodejs 映像
- c++ - 使用模板时启用 Visual Studio C++ 语法错误警告
- amazon-web-services - DynamoDB 到 ElasticSearch Geohash
- solr - 具有多值字段和特定订单键和不同权重的 solr boost
- c++ - Media Foundation 将音频流添加到视频文件
- python - 使用 pyinstaller 和 cx_Freeze 破坏 GUI 程序
- python - 在虚拟环境中访问本地文件