class Product extends Model
{
public function category()
{
return $this->belongsTo(Category::class);
}
}
$product->category->name;
Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.
Hi there, i am fetching data form products table. i can fetch category_id (from products table) successfully by this code but i want to get its Category name from category table.
@foreach($products as $product)
<tr class="odd">
<td class=" sorting_1"> {{ $product->title }} </td>
<td class=" "> {{ $product->category_id }} </td>
<td class=" ">{{ $product->price }}</td>
<td class=" "> {{ $product->availability }} </td>
<td class=" "><a href="#">Edit</a> / <a href="">Delete</a></td>
</tr>
@endforeach
@adeel Import the Category class. Make sure your Category is a model too that links to the category_ids and stuff.
You may also want to retrieve products while eager loading the categories:
$products = Product::all()->load('category');
or
$products = Product::with('category')->get();
Please or to participate in this conversation.