我正在尝试显示一个餐厅简介的评论和评级。
<div>
<h1>Reviews</h1>
@foreach($reviews as $review)
<hr>
<div class="row">
<div class="col-md-12">
@for ($i=1; $i <= 5 ; $i++)
<span class="glyphicon glyphicon-star{{ ($i <= $review->rating) ? '' : '-empty'}}"></span>
@endfor
{{ $review->user ? $review->user->name : 'Anonymous'}}
<p>{{{$review->value}}}</p>
</div>
</div>
@endforeach
</div>
错误
查看未定义的变量:评论(查看: C:\xampp\htdocs\restaurantFinder\resources\views\restaurants\viewer.blade.php):
审阅控制器
class ReviewsController extends Controller
{
public function index()
{
//
}
public function create()
{
//
}
public function store(Request $request)
{
if (!Auth::check()) {
return redirect('/index');
}
$review = new Review;
$review->user_id = auth()->user()->id;
$review->restaurant_id = $request->get('restaurant_id');
$review->value = $request->input('value');
$review->rating = $request->input('rating');
$review->save();
}
public function show($restaurant)
{
// $restaurant=Restaurant::find($id);
return view('restaurants.review', compact('restaurant'));
}
public function edit($id)
{
//
}
public function update(Request $request, $id)
{
//
}
public function destroy($id)
{
//
}
}
发布于 2019-07-21 22:31:46
你找错地方了。要跟踪问题,请从您的路由文件开始。搜索web.php文件中的URL,您将在那里找到路由。如果你得到了路径,你也会得到哪个控制器和动作在渲染这个页面。
当您在刀片模板中使用变量,但没有在控制器中设置该变量以供使用时,就会出现此问题。在控制器中,你可以设置这个变量,然后你就可以开始了。
希望这能有所帮助。如果有任何问题,请随时询问。
发布于 2019-07-22 00:14:54
更新你的ReviewsController.php
public function show($restaurantID)
{
$reviews = Reviews::find($restaurantID); // Find all reviews model by restaurantID
return view('viewer',compact('reviews ')); // This will do $reviews = reviews
// now you can foreach over reviews in your blade
}
此Link非常有用
https://stackoverflow.com/questions/57133887
复制相似问题