属性[纯素]在此集合实例上不存在。 Laravel

问题描述 投票:0回答:2

我试图根据表中的特定列是1还是0显示标题。在我的控制器中,我已经(编辑了一些不相关的代码:]]

 public function show(Company $id){

        $vegan = Company::find($id);
        $vegetarian = Company::find($id);

        return view('allProducts')->with([

            'vegan' => $vegan,
            'vegetarian' => $vegetarian,
        ]);
    }

并且在我看来:

  @if($vegan->vegan == 1)
    <h3 class="text-center">Vegan</h3>
  @endif

但是我收到错误消息

ErrorException (E_ERROR)
Property [vegan] does not exist on this collection instance. (View: C:\xampp\htdocs\EdenBeauty\resources\views\allProducts.blade.php)

我尝试过以下操作,但每次都会出错:

@if($vegan[0]->vegan == 1)

这会产生未定义的偏移量错误

我正在尝试根据表中的特定列是1还是0显示标题。在我的Controller中,我(编辑了一些不相关的代码):public function show(Company $ id){$ vegan =。 ..

php laravel eloquent laravel-5.8
2个回答
1
投票

问题是您在查询后缺少first()


0
投票

在此行中,您正在通过URL参数将Company注入到show方法中:

© www.soinside.com 2019 - 2024. All rights reserved.