php 在blade laravel中重复数据

jrcvhitl  于 5个月前  发布在  PHP
关注(0)|答案(1)|浏览(57)

我正在尝试在我的刀片中自动化信息。我有一个合同,这个合同有一个或多个可安装的项目。我正在做一个foreach并生成h6与名称和数量。
在我的数据库中只有一个项目:


的数据
如果我从$itemsInstallation->item返回dd(),则返回以下项目信息:



但当我做我的foreach创建h6与信息,所有的数据它是重复的:



Contract,have relation with itemsContract,and itemsContract have a relation with items.我正在获取好的项目信息,但我不知道为什么我的数据会重复。为了创建foreach,我正在执行:

@foreach ($itemsInstallation as $item)
   <h6>{{ $itemsInstallation->item->name }} -- {{ $itemsInstallation->cuantity }} uds</h6>
@endforeach

字符串
有人能说我做得不好吗?谢谢你的自述,对不起我的英语不好。

我解决了我的问题

回顾我所有的逻辑,我可以证明我的关系是错误的,我有一个hasOne而不是hasMany。现在我的foreach,它是一个简单的for:

@for ($i = 0; $i < count($itemsInstallation->item); $i++)
    <h6>{{ $itemsInstallation->item[$i]->name }} -- {{ $itemsInstallation->item[$i]->cuantity }} uds</h6>
@endfor

tkqqtvp1

tkqqtvp11#

你正在使用foreach循环,但是你调用的不是item而是array。
应该是这样的:

@foreach ($itemsInstallation as $item)
   <h6>{{ $item->name }} -- {{ $item->cuantity }} uds</h6>
@endforeach

字符串
您可以使用Forelse,它具有空条件,当没有数据时可以避免错误。

@forelse ($itemsInstallation as $item)
   <h6>{{ $item->name }} -- {{ $item->cuantity }} uds</h6>
@empty
@endforelse


欲了解更多信息,请访问:https://laravel.com/docs/10.x/blade#loops

相关问题