我已经开发了票证系统。第一个表数据获取并显示查看页面,但回复结果显示最后的记录。如何解决这个问题。请支持我。
控制器
$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'));
查看页面
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
应为视图页-例如
票据箱:打印机不工作
回复:重新启动打印机-这是第一个回复
票箱:重启后同样的问题。
回复:好的,我们现在就检查一下——这是的第二次回复
显示查看页面
票据箱:打印机不工作
回复:好的,我们现在就检查一下——这是的第二次回复
票箱:重启后同样的问题。
回复:好的,我们现在就检查一下——这是的第二次回复
注:票箱数据显示正确,但回复数据仅显示最后一条记录。
将reply
初始化为数组,并在每次迭代时向该数组推送新值。它看起来像是跟随
$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
将由在ticketdetails
中找到的具有匹配ticketID
的所有记录组成
在视图页面中,您可以执行以下
@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
感谢您的支持。我已将回复计数存储在数组变量$reply_display[]=$reply_check中;然后查看页面检查数组值
@php
$replyck = $reply_display[$i];
if($replyck==1){ @endphp
<p>{{$reply[$ticketItem->ticket_id]['reply_ticket_comments']}}</p>
@php }
$i = $i+1;
@endphp
:(-我的错误问题已解决。