1

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.

SSarangi
  • 11
  • 6
  • 1
    you are setting `$reply` in a loop, so every iteration you are overwriting the value so it will only have the value of the last iteration ... – lagbox Sep 20 '22 at 13:52
  • Okay, but I need to collect the $ticketID = $ticketItem->ticket_id; that id's for search the reply table. how to solve the issue. – SSarangi Sep 20 '22 at 14:11

2 Answers2

0

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
Ankit.Z
  • 740
  • 8
  • 20
  • Thanks for the reply, I have updated the code and data stored in $reply array. but view page showing the error message. "ErrorException Undefined variable: reply. $reply is undefined." how to solve the issue. please support me. – SSarangi Sep 20 '22 at 14:45
  • @SSarangi I have updated my answer. `reply` is declare before if condition so we pass array when condition is not met and in view we check whether `reply` array has count more than 0 if it does we show `reply_ticket_comments` – Ankit.Z Sep 20 '22 at 17:06
  • I have updated the code but still showing the error in view page. ErrorException Undefined variable: reply (View: E:\xamp\htdocs\laravel\project\resources\views\ticket\pages\case\ticket-view.blade.php) – SSarangi Sep 21 '22 at 06:46
  • In view file on which line do you have error? `reply` is initialized and passed to the view in compact. I have updated conditions in answer please check once. – Ankit.Z Sep 21 '22 at 07:21
  • error issue solved. I have follow your updated code. Thanks for your support. – SSarangi Sep 21 '22 at 10:11
  • I have faced another issue. If ticket ID not found in the reply table then view page error. ErrorException Undefined index: 3 (View: E:\xampp\htdocs\laravel\project\resources\views\pages\case\ticket-view.blade.php) Data records is ticket_id - 1 support team replyed. ticket_id - 2 support team replyed. ticket_id - 3 no reply record – SSarangi Sep 21 '22 at 13:18
  • push in `reply` if record exists using [null coalescing](https://www.w3schools.com/php/phptryit.asp?filename=tryphp_oper_null_coalescing) and in view add 1 more if condition `count($reply[ticketId])` before `ptag`. – Ankit.Z Sep 21 '22 at 13:31
  • Controller page result Ticket id:1 Count:1 REPLY:reply test Ticket id:2 Count:1 REPLY:reply mail test Ticket id:3 Count:0 if(count($reply) >0) if(count($reply[ticketId]) >0)

    {{$reply[$ticketItem->ticket_id]['reply_ticket_comments']}}

    endif endif this syntax is correct?
    – SSarangi Sep 21 '22 at 13:52
  • Updated my ans for controller and view please check – Ankit.Z Sep 21 '22 at 13:56
  • yes I have updated, but showing the error in view page ErrorException count(): Parameter must be an array or an object that implements Countable (View: E:\xampp\htdocs\laravel\project\resources\views\pages\case\ticket-view.blade.php) – SSarangi Sep 21 '22 at 14:08
  • Please support me, I am stuck in this error.. Controller page result example Ticket id:1 Count=1 REPLY=Restart Printer, Ticket id:2 Count=1 REPLY=okay, we will check now, Ticket id:3 Count=0 REPLY=. The view page check the Ticket id:3 and going to Error (Undefined index: 3 )

    {{$reply[$ticketItem->ticket_id]['reply_ticket_comments']}}

    – SSarangi Sep 21 '22 at 15:20
  • Please check the use cases and syntax declarations of `count()`, you are using it wrong in `reply_check` i have updated code please check `controller` and `view`. – Ankit.Z Sep 22 '22 at 07:09
  • always check variables to be collections before using `count`. Take a look at [answer](https://stackoverflow.com/a/73413504/7006384) – Ankit.Z Sep 22 '22 at 12:19
-1

Thanks for the support. I have stored the reply count in array variable $reply_display[ ] =$reply_check; Then view page check the array values

      @php 
      $replyck = $reply_display[$i];
      if($replyck==1){  @endphp 
        <p>{{$reply[$ticketItem->ticket_id]['reply_ticket_comments']}}</p>  
      @php  }  
       $i = $i+1;
      @endphp   

:)- my error issue solved.

SSarangi
  • 11
  • 6