Home > OS >  Checking if a model contains an ID in Laravel
Checking if a model contains an ID in Laravel

Time:03-31

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

CodePudding user response:

One of many options is to query the relation:

@if($shop->items->where('id', $item->id)->count()) checked @endif
  • Related