I think you may read the eloquent's relationship part of the docs :)
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
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.