I have developing the ticket system. First table data fetch and display the view page but reply result is showing the last records. How to solve this issue. Please support me.
Controller
$ticketdetails = TicketDetails::where('ticket_id', '=', $ticketID)
->orderBy('ticket_id', 'asc')->get();
if($ticketdetails->count()){
foreach ($ticketdetails as $ticketItem) {
$ticketID = $ticketItem->ticket_id;
$reply_check = Reply::where('reply_ticket_id', '=', $ticketID)->count();
if($reply_check!="")
{
$reply = Reply::where('reply_ticket_id', '=', $ticketID)->first();
}
} //Close foreach
} // Close if loop
return view('view-ticket',compact('data','ticketdetails','reply'));
View Page
if($ticketdetails->count())
@foreach($ticketdetails as $ticketdetails)
<p>{{$ticketdetails->ticket_details}}</p>
$replyid = $reply->reply_ticket_id;
$ticketdetailsid = $ticketdetails->ticket_id;
@php
if($replyid==$ticketdetailsid)
{
@endphp
<p>{{$reply->reply_ticket_comments}}</p>
@php
}
@endphp
@endforeach
@endif
Expecting View page- For example
Ticket Case : Printer not working
Reply:Restart Printer - this is first reply
Ticket Case : After restart same issue.
Reply:okay, we will check now -- this is second reply
Display For view page
Ticket Case : Printer not working
Reply:okay, we will check now -- this is second reply
Ticket Case : After restart same issue.
Reply:okay, we will check now -- this is second reply
Note:Ticket case data display is correct but reply data only showing the last record.
CodePudding user response:
Initialize reply
as array and with each iteration push new value to that array. It will look like following
$ticketdetails = TicketDetails::where('ticket_id', '=', $ticketID)
->orderBy('ticket_id', 'asc')->get();
$reply = [];
if(count($ticketdetails) > 0){
foreach ($ticketdetails as $ticketItem) {
$ticketID = $ticketItem->ticket_id;
$reply_check = Reply::where('reply_ticket_id', '=', $ticketID)->count();
if($reply_check!=""){
//Code change below
$reply[$ticketID] = Reply::where('reply_ticket_id', '=', $ticketID)->first() ?? [];
}
} //Close foreach
} // Close if loop
return view('view-ticket',compact('data','ticketdetails','reply'));
reply
will consist of all the records with matching ticketID
found in ticketdetails
And in view page you can do following
@if(count($ticketdetails) > 0)
@foreach($ticketdetails as $ticketItem) //Change in name of variable
<p>{{$ticketItem->ticket_details}}</p>
//You don't need to check for id as new reply array consist of all records mapped with ticket ID
@if(count($reply) >0)
@if(count($reply[$ticketItem->ticket_id]) >0)
<p>{{$reply[$ticketItem->ticket_id]['reply_ticket_comments']}}</p>
@endif
@endif
@endforeach
@endif