What about this:
@if ($link->categories->contains($category->id) ? 'checked' : '') @endif
Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.
I have a database with 3 tables: links, categories and category_link. Each Link can have many Categories, each Category can have many Links and the lookup is in the category_link table.
When I come to edit a Link I want to be able to see what Categories it already has and then edit them. But, I cannot seem to get the ticks to show in the boxes, this is my code for the checkboxes (I've removed the layouts and css for readability):
@foreach ($categories as $category)
<input
id="{{$category->id}}"
value="{{$category->id}}"
aria-describedby="category-description"
name="categories[]"
type="checkbox"
@if (in_array($category->id, $link->categories->id) ? 'checked' : '') @endif
>
<label for="{{$category->id}}">{{$category->category}}</label>
@endforeach
I currently get this error:
Exception
Property [id] does not exist on this collection instance.
I'm not sure what it should be to get it to work. Thanks
Please or to participate in this conversation.