'TypeError Cannot access offset of type string on string (View: C:\xampp\htdocs\xtendanceweb\resources\views\laporan\index.blade.php)

Can some one help me with this problem? my error : TypeError Cannot access offset of type string on string (View: C:\xampp\htdocs\xtendanceweb\resources\views\laporan\index.blade.php)

my code in controller

 if(isset($request->filter_date)) {
            $date_start = Carbon::parse($request->filter_date)->format('Y-m-d');
            $date_end = Carbon::parse($request->end_date)->format('Y-m-d');

 $company_holiday = CompanyHoliday::where('company_holiday.company_id', $company_id)
            ->whereDateBetween('company_holiday.date', $date_start, $date_end)
            ->get();
 }

my code in index.blade.php

   @foreach($company_holiday as $comp_hol)
        @php
             $company_id = Auth::user()->company_id;

                  if($comp_hol != null){
                       $holiday = array_filter(json_decode($comp_hol, true), function ($var) use ($company_id) {
                              return $var['company_id'] == $company_id;
                       });

                  }else{
                        $comp_hol = null; 
                  }
         @endphp
   @endforeach


Solution 1:[1]

At one point $var is a string. You can solve that problem by checking it is an array and it contains the 'company_id' key:

return is_array($var) && isset($var['company_id']) && $var['company_id'] == $company_id;

I don't know the contents of the json, so it could be that this will never return true.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Gert B.