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

shiftde's avatar

check rows in blade which has relationship with another table

In Blade file, how to check a result variable that has children table or not?

I have 3 related tables as 'parent' and 'child' which

//parent
id => 1
title => 'accessories' // which connected to a 'child' tables

id => 2
title => 'services' // which connected to a 'child' tables

id = 3
title => 'about' // which has no connected table


// accessories as child
id => 1
title => 'cable'

id => 2
title => 'holder'


// services as child
id => 1
title => 'repair'

id => 2
title => 'change'

l want to print all rows in parent table which has a relationship with another table as child

0 likes
1 reply
tinfoilman's avatar

First you have to make sure your model relationships are setup correctly. Then make sure your queries return the parents and children loaded.

In your blade file you can simply loop through each parent, and then loop through each child within that.

controller:

$parents = Parent::with('children')->get();

Blade file will look something like this:

@foreach ($parents as $parent)
	{{ $parent->title }}
	@foreach ($parent->children as $child)
		{{ $child->title }}
	@endforeach
@endforeach

Please or to participate in this conversation.