'Checking if a model contains an ID in Laravel
I have something like this:
$item = Item::all();
And something like this:
$shop->items(); //Return a list of item
Now I want to check in blade if the shop has the item (example):
@foreach($items as $item)
<input type="checkbox" @if($shop->hasItem()) checked @endif>
Is there an easy way to do this? I could do another @foreach(shop->items) and check if $item->id is in there but I feel there must be a clean way to do this
Solution 1:[1]
One of many options is to query the relation:
@if($shop->items->where('id', $item->id)->count()) checked @endif
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Gert B. |
