skip to Main Content

The following are the function that I am trying to make work. I think I have the information required correct and the corresponding data also exist in the table but it is showing the error as above. Below are the function in my UserProfileController.

I’ve tried to change the column name from created_at to date_checkIn but it is showing the same error, both the columns exist in the database table as shown in my screenshots above. Please help


public function userClockOut(Request $r)
    {
        $result = [];
        $result['status'] = false;
        $result['message'] = "something error";

        $users = User::where('staff_id', $r->staff_id)->select(['staff_id', 'date_checkIn', 'time_checkOut', 'location_checkOut'])->first();

        $tt = AttendanceRecord::find($r->tt);
        $tt2 = AttendanceRecord::find($r->tt2);

        $tz = $tt->created_at;
        $tz2 = $tt2->updated_at;

        $date = Carbon::createFromFormat('Y-m-d', $tz, 'UTC');
        $time = Carbon::createFromFormat('H:i:s', $tz2, 'UTC');
        $date->setTimezone('Asia/Singapore');
        $time->setTimezone('Asia/Singapore');

        $users->date_checkIn = $date;
        $users->time_checkOut = $time;
        $users->location_checkOut = $r->location_checkOut;

        // Save the updated data to the database
        AttendanceRecord::updateOrCreate(
            ['staff_id' => $users->staff_id, 'date_checkIn' => $date],
            $users->toArray()
        );

        $result['data'] = $users;
        $result['status'] = true;
        $result['message'] = "suksess add data";

        return response()->json($result);
    }


Screenshots to show that the column exist in the database table :

https://paste.pics/f6103e8822c01f26bd23d500e0b0e9ad https://paste.pics/008df0256fd5ec20caff3772194cc0cf https://paste.pics/66cca657a49944e8087451c3b75967d3

2

Answers


  1. $tt or $tt2 doesnot found.

    $tt = AttendanceRecord::find($r->tt);
    $tt2 = AttendanceRecord::find($r->tt2);
    
    Login or Signup to reply.
  2. It looks like your $tt = AttendanceRecord::find($r->tt); doesn’t return any value so its throwing an error. To solve this first you need to make sure that the $r->tt has a value and that value exists in the database. Next is to avoid that error instead of using the find() method you should use the findOrFail() which will return a 404 status code if the given id do not exist in the database.

    $tt = AttendanceRecord::findOrFail($r->tt);
    $tt2 = AttendanceRecord::findOrFail($r->tt2);
    

    Also make sure that the $r->tt is the primary key of the table and also make that there’s a tt attribute in your form

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search