0

I am trying to create a table in my blade template. When i loop through the array once, i get the following error htmlspecialchars() expects parameter 1 to be string, array given .

When i loop twice, i get an incorrect table format.

My blade template looks like this :

current table

@foreach($checkins as $index=>$opt)
          @foreach($checkins as $index=>$opt)
             @foreach($opt as $key => $value)
               <tr> {{$key}} </tr>
               <td> {{$value}}  </td>    
             @endforeach    
                                                        
         @endforeach                                        

My controller looks like this, i converted the latitude and longitude to an actual location using the getAddress function :

if($request->has('search')) {
            $checkins = Checkin::with(['user'])->where('name', 'like', '%'.$request->search.'%')->paginate(setting('record_per_page', 15));
        } else {
            $checkins = Checkin::with(['user'])->get();
        }

        $title = "Manage Checkins";

        
        $checkins = $checkins->map(function ($checkin) {

            $location = $this->getAddress($checkin->lat,$checkin->long);

            return [
                'location' => $location,
                'student' => $checkin->user->first_name. ' ' .$checkin->user->first_name,
                'supervisor' => $checkin->supervisor === null ? '' : $checkin->supervisor->first_name. ' ' .$checkin->supervisor->last_name  ,
                'created_at' => $checkin->created_at,
            ];
        });

        return view('checkins.index', compact('checkins', 'title'));

The array that looks like this :

[
{
location: "Government of Southern Sudan Nairobi Liason Office, 5th Avenue Ngong, Hurlingham, Nairobi, P. O. BOX 41362, Kenya",
student: "Super Admin Super Admin",
supervisor: "Super Admin Admin",
created_at: null
},
{
location: "Kajiado, Kenya",
student: "Test Test",
supervisor: "Super Admin Admin",
created_at: "2021-07-06T12:13:54.000000Z"
},
{
location: "Murang`a, Central Kenya, Kenya",
student: "Test Test",
supervisor: "Super Admin Admin",
created_at: "2021-07-06T12:16:19.000000Z"
}
]

Any assistance/links on how i can display the keys as the values and the actual values below the keys will be appreciated.

1 Answer 1

1

First loop return each individual item of associative array and second value loop each associative array to key value pair

     @foreach($checkins as $key=>$value)
           @foreach($value as $iKey=>$iValue)
           <tr>          
              <td> {{$iKey}} </td>
              <td> {{$iValue}}  </td>    
           </tr>
                                                                    
        @endforeach  
 @endforeach  
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.