请帮帮我。我有这三个表,而且我在如何基于restaurant id (从Controller到view )的基础上调用Controller有问题。提前谢谢你。
表items

表categories

表restorant

这是我的Controller
public function index()
{
if (auth()->user()->hasRole('owner')) {
$items = Items::with('category')->get();
$restorant_id = auth()->user()->restorant->id;
$category = Categories::where(['restorant_id' => $restorant_id]);
}
return view('point-of-sale::index', [
'category' => $category,
'items' => $items,
]);
}发布于 2021-06-14 09:41:43
如果将get()添加到$category = Categories::where(['restorant_id' => $restorant_id]);语句的末尾,将返回一个雄辩的集合:
$category = Categories::where(['restorant_id' => $restorant_id])->get();按照当前的情况将$category变量传递给您的视图,考虑将它重命名为$categories,不过只是为了推断可能存在多个变量。
然后,在您的view中,您可以遍历Category结果并访问name属性:
@forelse ($category as $cat)
{{ $cat->name }}
@empty
No categories.
@endforelse更新
如果您想通过他们的items获得category_id,您可以按照您对$categories所做的那样做
$items = Items::where(['category_id' => $category_id])->get();或者,如果在您的items模型上有一个Categories关系,您可以这样访问它们:
$category = Categories::with('items')
->where(['restorant_id' => $restorant_id])
->get();上面的内容将急切地加载与items相关的category,然后您可以在视图中访问该category,例如:
@forelse ($category as $cat)
{{ $cat->name }}
@foreach ($cat->items as $item)
{{ $item->name }}
@endforeach
@empty
No categories.
@endforelsehttps://stackoverflow.com/questions/67967048
复制相似问题