However, children are not showing properly with:
Can you share a set of data (extract dump sql) with results and expected results ?
Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.
Hi all,
I am trying to build a relationship for my model, Client. A Client can have a subclient (children) or it can be a child of another client (parent). Or, it can be by itself as just a client.
I have the Client model with columns id and name, and the ClientRelationship model with the columns client_id and parent_id.
ClientRelationship migration:
$table->unsignedBigInteger('client_id');
$table->unsignedBigInteger('parent_id');
$table->foreign('client_id')->references('id')->on('clients');
$table->foreign('parent_id')->references('id')->on('clients');
And within the Client model:
public function parent()
{
return $this->hasOne(ClientRelationship::class);
}
public function children()
{
return $this->hasMany(ClientRelationship::class);
}
When I have this display on my blade template, parent is working and showing what it should be using:
@if ($client->parent)
<li>{{ $client->parent->id }}</li>
@endif
However, children are not showing properly with:
@if ($client->children)
@foreach ($client->children as $child)
<li>{{ $child->id }}</li>
@endforeach
@endif
Also, in a perfect would I'd have it displaying the client names, not IDs, but I wanted to first figure out why I can't get children to display properly.
Thanks for any advice, I feel like I must be missing something silly!
Please or to participate in this conversation.