Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

viper75x's avatar

Checkbox and select box in HTML table...

I'd like to present a list of records in a table view, if possible, utilizing one field as a link to access the details of said record.

I would like to use a couple of checkboxes and select boxes to display the data...unless it would be better to retrieve the related data and display in the table row.

<thead>
                            <tr>
                                <th>Catalog Number</th>
                                <th>Description</th>
                                <th>Stocked</th>
                                <th>Category</th>
                                <th>Review</th>
                                <th>In Use</th>
                                <th>Discontinued</th>
                                <th>Classification</th>
                            </tr>
                            @foreach ($items as $item)
                            <tr>
                                <td><a href="{{ $item->path() }}">{{ $item->catalogNumber }}</a></td>
                                <td>{{ $item->description }}</td>
                                <td>{{ $item->stocked == 1 ? 'Yes' : 'No'}}</td>
                                
                                <td>{{ $item->category_id}}</td>
                                
                                <td>{{ $item->review }}</td>
                                <td>{{ $item->inUse }}</td>
                                <td>{{ $item->discontinued }}</td>
                                <td>{{ $item->classification_id }}</td>
                            </tr>
                            @endforeach
                        </thead>

I used the ?: to change 1 and 0 to Yes and No, but how would I get a Category name instead of the id?

Thanks.

0 likes
3 replies
MichalOravec's avatar

If you use relationship it could be like this

$items = Item::with('category')->get();
<td>{{ $item->category->name }}</td>
Snapey's avatar

when you get items, use with('category')

In the loop

<td>{{ $item->category->name }}</td>
viper75x's avatar

Model:

public function categories()
    {
        return $this->belongsTo(InvCategory::class);
    }

Controller:

$items = InvMaterial::orderby('catalogNumber')->sortable()->where('description', 'like', '%'.$filter.'%')->paginate(20)->with('categories');

In both your examples, does 'category' refer to the class name or field name?

Category field in the Item table: category_id Category table name: inv_categories

Yes, I'm using prefixes in my table names, as it helps me to remember their main function.

Please or to participate in this conversation.