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

tarekreza's avatar

TypeError Cannot access offset of type string on string http://127.0.0.1:8000/employees/monthly/salary/payslip/10/1970-01-09

I get this error: TypeError Cannot access offset of type string on string http://127.0.0.1:8000/employees/monthly/salary/payslip/10/1970-01-09

In controller

public function MonthlySalaryPayslip(Request $request,$id,$date)
    {
        $date['y'] = date('Y', strtotime($date));
        dd($date['y']);
        $date['m'] = date('m', strtotime($date));
        $data['details'] = EmployeeAttendance::whereYear('date', $date['y'])->whereMonth('date', $date['m'])->where('employee_id', $id)->get();
}

I get date and id from URL.

0 likes
3 replies
asims's avatar
asims
Best Answer
Level 4

Is $date a string? If so you won’t be able to access it like you are. You’ll need to parse the string to a date using Carbon for instance or DateTime and then get the parts from that. Alternatively if your string is always in the format Y-m-d then you could explode the string at the - and then use the parts from that so $date[0] would be your year, 1 month and 2 day. See here the php manual:

https://www.php.net/manual/en/function.explode.php

1 like
tarekreza's avatar

@sinnbeck

//public function MonthlySalaryPayslip(Request $request,$id,$date)
    //{
        $date['y'] = date('Y', strtotime($date));
        //dd($date['y']);
      //  $date['m'] = date('m', strtotime($date));
      //  $data['details'] = EmployeeAttendance::whereYear('date', $date['y'])->whereMonth('date', $date['m'])->where('employee_id', $id)->get();
//}

Please or to participate in this conversation.