Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

babo's avatar
Level 1

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

0 likes
12 replies
Sinnbeck's avatar

Can you use dd() to show us what your variables hold

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

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

                  }else{
                        $comp_hol = null; 
                  }
         @endphp
   @endforeach
Sinnbeck's avatar

@babo Great so you figured it out and it works now? If not we cannot really help unless we know your data.

babo's avatar
Level 1

@Sinnbeck for the result of the data loop the data is there but when array_filter still error

Sinnbeck's avatar

@babo Ok good luck. I can only say that there return $var['company_id'] == $company_id; is is telling you that $var is a string, not an array..

babo's avatar
Level 1

@Sinnbeck result of dd($comp_hol);

  #table: "company_holiday"
  #fillable: array:10 [▶]
  #guarded: []
  #keyType: "string"
  +timestamps: false
  #connection: "mysql"
  #primaryKey: "id"
  +incrementing: true
  #with: []
  #withCount: []
  +preventsLazyLoading: false
  #perPage: 15
  +exists: true
  +wasRecentlyCreated: false
  #escapeWhenCastingToString: false
  #attributes: array:10 [▼
    "id" => "0a270323-8fe5-48a1-9c66-f46570fbd19f"
    "company_id" => "7e990fd0-6d34-11ec-b042-ddd475e7661a"
    "date" => "2022-04-04"
    "description" => "holiday"
    "national_holiday" => 1
    "cuti_bersama" => 0
    "created" => "2022-04-05 11:35:11"
    "created_by" => "Admin ETS"
    "modified" => null
    "modified_by" => null
  ]
Sinnbeck's avatar

@babo And why are you trying to run json_decode on a model ? Its a model, not a json string..

What is this code supposed to do? How is $holiday used?

Sinnbeck's avatar

Did you mean to do something like this?

$holiday = $company_holiday->firstWhere('company_id', Auth::user()->company_id);

If so that still makes little sense as you already filtered it in the database

company_holiday = CompanyHoliday::where('company_holiday.company_id', $company_id) //filtering on company id right here
            ->whereDateBetween('company_holiday.date', $date_start, $date_end)
            ->get();
babo's avatar
Level 1

@Sinnbeck Yes, that's right, in the company_holiday table there are 2, when I return it to the controller I get 2 data, but why is there only one data in the view? even though it's already in the loop

Illuminate\Database\Eloquent\Collection {#1493 ▼
  #items: array:2 [▼
    0 => App\Models\CompanyHoliday {#1532 ▼
      #table: "company_holiday"
      #fillable: array:10 [▶]
      #guarded: []
      #keyType: "string"
      +timestamps: false
      #connection: "mysql"
      #primaryKey: "id"
      +incrementing: true
      #with: []
      #withCount: []
      +preventsLazyLoading: false
      #perPage: 15
      +exists: true
      +wasRecentlyCreated: false
      #escapeWhenCastingToString: false
      #attributes: array:10 [▼
        "id" => "0a270323-8fe5-48a1-9c66-f46570fbd19f"
        "company_id" => "7e990fd0-6d34-11ec-b042-ddd475e7661a"
        "date" => "2022-04-04"
        "description" => "holiday"
        "national_holiday" => 1
        "cuti_bersama" => 0
        "created" => "2022-04-05 11:35:11"
        "created_by" => "Admin ETS"
        "modified" => null
        "modified_by" => null
      ]
      #original: array:10 [▶]
      #changes: []
      #casts: []
      #classCastCache: []
      #attributeCastCache: []
      #dates: []
      #dateFormat: null
      #appends: []
      #dispatchesEvents: []
      #observables: []
      #relations: []
      #touches: []
      #hidden: []
      #visible: []
    }
    1 => App\Models\CompanyHoliday {#1531 ▼
      #table: "company_holiday"
      #fillable: array:10 [▶]
      #guarded: []
      #keyType: "string"
      +timestamps: false
      #connection: "mysql"
      #primaryKey: "id"
      +incrementing: true
      #with: []
      #withCount: []
      +preventsLazyLoading: false
      #perPage: 15
      +exists: true
      +wasRecentlyCreated: false
      #escapeWhenCastingToString: false
      #attributes: array:10 [▼
        "id" => "f4679e82-181f-4cf8-b510-638e008d29df"
        "company_id" => "7e990fd0-6d34-11ec-b042-ddd475e7661a"
        "date" => "2022-04-14"
        "description" => "libur"
        "national_holiday" => 0
        "cuti_bersama" => 1
        "created" => "2022-04-04 10:01:25"
        "created_by" => "Admin ETS"
        "modified" => null
        "modified_by" => null
      ]
      #original: array:10 [▶]
      #changes: []
      #casts: []
      #classCastCache: []
      #attributeCastCache: []
      #dates: []
      #dateFormat: null
      #appends: []
      #dispatchesEvents: []
      #observables: []
      #relations: []
      #touches: []
      #hidden: []
      #visible: []
    }
  ]
  #escapeWhenCastingToString: false
}


babo's avatar
Level 1

in view i get only one data

App\Models\CompanyHoliday {#1558 ▼
  #table: "company_holiday"
  #fillable: array:10 [▶]
  #guarded: []
  #keyType: "string"
  +timestamps: false
  #connection: "mysql"
  #primaryKey: "id"
  +incrementing: true
  #with: []
  #withCount: []
  +preventsLazyLoading: false
  #perPage: 15
  +exists: true
  +wasRecentlyCreated: false
  #escapeWhenCastingToString: false
  #attributes: array:10 [▼
    "id" => "0a270323-8fe5-48a1-9c66-f46570fbd19f"
    "company_id" => "7e990fd0-6d34-11ec-b042-ddd475e7661a"
    "date" => "2022-04-04"
    "description" => "holiday"
    "national_holiday" => 1
    "cuti_bersama" => 0
    "created" => "2022-04-05 11:35:11"
    "created_by" => "Admin ETS"
    "modified" => null
    "modified_by" => null
  ]
  #original: array:10 [▼
    "id" => "0a270323-8fe5-48a1-9c66-f46570fbd19f"
    "company_id" => "7e990fd0-6d34-11ec-b042-ddd475e7661a"
    "date" => "2022-04-04"
    "description" => "holiday"
    "national_holiday" => 1
    "cuti_bersama" => 0
    "created" => "2022-04-05 11:35:11"
    "created_by" => "Admin ETS"
    "modified" => null
    "modified_by" => null
  ]```
Snapey's avatar

@babo you have a collection of models, inside the foreach you dump a model and then die. You only see one model.

if you want to dump them all put the dd outside the foreach loop

Sinnbeck's avatar

@babo Maybe it would be easier if you could show the how you are using it in the view. I mean, what comes after that raw php

Please or to participate in this conversation.