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

DaveWalker's avatar

Controller Index : Parent -> Child / Child -> Parent question

I have a three tables. "Customers", "Tickets" & "SubTickets".

I have a Tickets page which shows all Tickets and in my Controller index at the moment is a simple:

$tickets = Ticket::all();
return view('tickets.index', compact('tickets'));

In my Tickets "index.blade.php" I want to display the actual Customer name from the Customer table, but at the moment I only have the "tickets->customer_id".

What's the best way to retrieve the Customer name for each instance of my tickets in the index?

Many thanks, Dave

0 likes
4 replies
pmall's avatar

I think you may read the eloquent's relationship part of the docs :)

DaveWalker's avatar

I got confused when I did. I can do the individual records no problem, but on the index where I'm grabbing all the Ticket records - I couldn't figure out what to do in that instance.

DaveWalker's avatar
DaveWalker
OP
Best Answer
Level 5

So the answer to the question (just to save anyone time) is:

Models are set as :

Customer hasMany (Ticket)

Ticket belongsTo (Customer)

In my Controller I retrieve the ticket

$tickets = Ticket::all();
return view('tickets.index', compact('tickets'));

and in my Blade template I can access the parent customer field "companyname" simply like so:

{{ $ticket->customer->companyname }}

Hope that helps!

Please or to participate in this conversation.