【发布时间】:2021-05-05 01:12:14
【问题描述】:
我在 laravel 8 上,当我想根据数据库的类别 ID 显示我的数据时遇到了这个问题。而且我很确定我已经为数据分配了类别。
我的 Index.Blade
@extends('layouts.front')
@section('content')
<div class="container">
<h2>Produk {{ $categoryName ?? null }}</h2>
<div class="custom-row-2">
@foreach ($produks as $produk)
@include('produk._single_product')
@endforeach
</div>
</div>
@endsection
我的控制器
public function index()
{
$keranjangItems = \Cart::session(auth()->id())->getContent();
$categoryId = request('category_id');
$categoryName = null;
if($categoryId){
$category = Category::find($categoryId);
$categoryName = ucfirst($category->name);
$produks = $category->products;
}else{
$produks = Produk::take(10)->get();
}
return view('produk.index', compact('keranjangItems', 'produks', 'categoryName'));
}
我发现了一个几乎相同问题的线程。我也从我在 youtube 上找到的一些教程中获得了这段代码,它应该可以工作。我试图清除缓存,在 foreach 中添加“(数组)”。而且它们都不起作用,有人可以帮助我吗?谢谢
【问题讨论】: