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

jabrij93's avatar

Php laravel don't work the same as in local when deployed to shared hosting, cPanel

So, currently, my web-app is working correctly in local. The problem in shared hosting is that when I clock out, it automatically changes all the time clock-out and location clock-out of all the other users as well. Here is the screenshot in local, https://paste.pics/48b6310c2e1bdf728ccedc9ed6fd42ce and here is the screenshot when deployed to shared hosting(cPanel), it is behaving like this, https://paste.pics/f0a9c1aafc62e5edbb0157bf8c1de1e6 .

I've tried reuploading everything for third times, double checking on the controller file in shared hosting, api.php in routes, and the blade.php file in views, resetting all the data and restarting again and it's still the same in shared hosting. I wonder what's wrong ? Please help, I've stucked at this bug since last week friday.

Below are the code taken from hosting using WinSCP

Api.php in routes

<?php

use Illuminate\Http\Request;
use Illuminate\Support\Facades\Route;
use App\Http\Controllers\AuthController;
use App\Http\Controllers\UserProfileController;


Route::post('login', [AuthController::class, 'login']);

Route::get('getdata', [UserProfileController::class, 'getdata']);
Route::post('getdata/{id}', [UserProfileController::class, 'showdata']);
Route::post('adduser', [UserProfileController::class, 'adddata']);
Route::delete('deleteuser', [UserProfileController::class, 'deleteuser']);
Route::PUT('updateuser', [UserProfileController::class, 'updateuser']);
Route::post('updateuserClockIn',   [UserProfileController::class, 'userClockIn']);
Route::post('updateuserClockOut', [UserProfileController::class, 'userClockOut']);

Route::middleware('auth:sanctum')->get('user', function (Request $request) {
    return $request->user();
});

function in UserProfileController,

public function userClockIn(Request $r)

    {

        $result = [];
        $result['status'] = false;
        $result['message'] = "something error";


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


        $mytime = Carbon::now('Asia/Kuala_Lumpur');
        $date = $mytime->format('Y-m-d');
        $time = $mytime->format('H:i:s');

        $users->date_checkIn = $date;
        $users->time_checkIn = $time;
        $users->location_checkIn = $r->location_checkIn;

        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);
    }
public function userClockOut(Request $r)

    {

        $result = [];
        $result['status'] = false;
        $result['message'] = "something error";

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

        $mytime = Carbon::now('Asia/Kuala_Lumpur');
        $date = $mytime->format('Y-m-d');
        $time = $mytime->format('H:i:s');

        $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);

}

blade.php files in views displaying the History page,

<tbody class="bg-white divide-y divide-gray-200">
    @php
    $no = 1
    @endphp
    @foreach ($history as $row)
    <tr>
      <td class="px-6 py-4 whitespace-nowrap">
        <div class="text-sm text-gray-900"> {{ $no++ }} </div> 
      </td>

      <td class="px-6 py-4 whitespace-nowrap">
        <div class="text-sm text-gray-900"> {{ $row->staff_id }} </div>
      </td>

      <td class="px-6 py-4 whitespace-nowrap"> 
        <div class="text-sm text-gray-900"> {{ $row->date_checkIn }} </div> 
      </td>

      <td class="px-6 py-4 whitespace-nowrap"> 
        <div class="text-sm text-gray-900"> {{ $row->time_checkIn }} </div>
      </td>
      
      <td class="px-6 py-4 whitespace-nowrap text-sm text-gray-500">
        {{ $row->location_checkIn }}
      </td>
                        
      <td class="px-6 py-4 whitespace-nowrap text-sm text-gray-500">
        {{ $row->time_checkOut }}
      </td>
                        
      <td class="px-6 py-4 whitespace-nowrap text-sm text-gray-500"> 
        {{ $row->location_checkOut }} 
      </td>
      </tr>
      @endforeach
</tbody>
0 likes
20 replies
jlrdw's avatar

Probably a difference in timezones for local versus the server.

jabrij93's avatar

@jlrdw i've gotten like 3 same answers. does that means that I would have to change the timezone configuration in cPanel ? at the same time, if that's the case, then why does the time check-in and location check in still works though and only time check out and location check out that has problem?

jlrdw's avatar

@jabrij93 Check the timezone of both servers local and production by doing:

date_default_timezone_get()

But that is peculiar that check-in works and check-out doesn't. If all users are in the same timezone you could set it to the needed timezone. Also I wouldn't break it down, instead use a timestamp or a datetime field.

jabrij93's avatar

@jlrdw where should I put this ?

date_default_timezone_get() ?

and just for info, right now i'm using Carbon::now('Asia/Singapore') in the controller ..i'm not familiar with the method above but is it the same as applying the code below in mysql ?

SELECT @@global.time_zone;

and then

SET GLOBAL time_zone = 'Asia/Singapore'; 

is it the same thing?

jabrij93's avatar

@jlrdw in my UserProfileController, there are 2 functions that are related to check in and check out. should I put in either one of them ? and as the code above, should I add @php like the code below ?

@php
echo date_default_timezone_get();
die;
@endphp
jlrdw's avatar

@jabrij93 see edited answer above. No that function don't stay, it just gives you the current timezone set so you will know. Don't leave it there.

1 like
jabrij93's avatar

@jlrdw i read the comments on the link that you posted above, just to make sure that I am doing it right, in config, app.php file in my laravel project, I change the

'timezone' => 'UTC', 

to

'timezone' => 'Asia/Singapore',
1 like
jabrij93's avatar

@jlrdw storing the timestamp, u mean by having $table->timestamps in my migration database table ? if yes, i have already done it rn. Besides is there any other way to convert Carbon to local time ? the one that i am using right now is Carbon::now('Asia/Singapore'). I think it is already doing the job of converting the time to local time?

jlrdw's avatar

@jabrij93 if check in works but check out doesn't, have you gone through and checked things like:

  • Do you have an accessor and mutator changing it
  • Some other function like a helper changing it

Otherwise seems both would work normally and the same.

2 likes
jabrij93's avatar

@jlrdw How do I check an accessor/mutator that are changing it ? Yeah, I actually wanted to point this out because I suspect that the following code might have duplicated or in some way affects how the code functions since both of them are accessing the AttendanceRecord model and when data gets updated or changed in the index.blade.php file, it will affect the data in history.blade.php . Previously, I put the code below in users folder, index.blade.php file. It is using the AttendanceRecord model. And then in the same folder there are one other file, history.blade.php where it is also using the AttendanceRecord model as in the following code below the index.blade.php. I want to emphasized that I've removed the code in index.blade.php file both in local and hosting. But maybe there are some parts that it has caused to mutate or change permanently ? And if so, where should I look it ?

index.blade.php

@foreach(App\Models\AttendanceRecord::get() as $data)
    @php
    $latestRecord = App\Models\AttendanceRecord::orderBy('created_at', 'desc')->first();
    @endphp
    <td rowspan="2" class="px-6 py-4 text-sm font-medium">
        <p class=" text-sm font-medium text-gray-500"> Check in : {{$latestRecord -> date_checkIn}} <br>
        {{$latestRecord -> time_checkIn}} <br>
        {{$latestRecord -> location_checkIn}}
        </p>
        <p class=" text-sm font-medium text-gray-500"> Check Out : {{$latestRecord -> time_checkOut}} <br>
        {{$latestRecord -> location_checkOut}}
        </p>
    </td>
@endforeach

history.blade.php

<tbody class="bg-white divide-y divide-gray-200">
    @php
    $no = 1
    @endphp
    @foreach ($history as $row)
     <tr>
         <td class="px-6 py-4 whitespace-nowrap">
             <div class="text-sm text-gray-900"> {{ $no++ }} </div>
         </td>
         <td class="px-6 py-4 whitespace-nowrap">
             <div class="text-sm text-gray-900"> {{ $row->staff_id }} </div>
         </td>
         <td class="px-6 py-4 whitespace-nowrap">
             <div class="text-sm text-gray-900"> {{ $row->date_checkIn }} </div>
         </td>
         <td class="px-6 py-4 whitespace-nowrap">
             <div class="text-sm text-gray-900"> {{ $row->time_checkIn }} </div>
         </td>
         <td class="px-6 py-4 whitespace-nowrap text-sm text-gray-500">
             <div class="text-sm text-gray-900"> {{ $row->location_checkIn }} </div>
         </td>
         <td class="px-6 py-4 whitespace-nowrap text-sm text-gray-500">
             <div class="text-sm text-gray-900"> {{ $row->time_checkOut }} </div>
         </td>
         <td class="px-6 py-4 whitespace-nowrap text-sm text-gray-500">
             <div class="text-sm text-gray-900"> {{ $row->location_checkOut }} </div>
         </td>
     </tr>
     @endforeach
</tbody>

the variable $history above is defined in my UserController with the following function :

  public function history()
   {
       $history = AttendanceRecord::get();
       return view('users.history', compact('history'));
   }

My userClockOut() function and userClockIn() function that I posted in my previous post both are in another controller UserProfileController.php. Let me know if you need routes information

jlrdw's avatar

@jabrij93 I suggest write three new methods only to test this problem.

One method insert a check in and another method check out.

Then retrieve datetime check in and check out you inserted.

If the test shows correct, you have to have other code somewhere that is changing the check out datetime.

Is this all your code, or is there another programmer involved also. Because if check in works then check out should as well.

2 likes
jabrij93's avatar

@jlrdw I never write a test before. How do I write it ? This is by far my first most longest project I've ever done. My duration in programming is yet to be 7-9 months. There's still a lot of things that I don't know. I write all this code by myselves, but there's some part that I ask for help from other people and I write it by myselves. However, there's another guy that are in charge of deploying this to shared hosting.

jlrdw's avatar

@jabrij93 I don't mean like a unit test, I meant some real methods to see what is going on.

jabrij93's avatar

@jlrdw I kindly ask for your guidance to gain clarity and so I can take some practical steps

jlrdw's avatar

@jabrij93

  • write another temporary function where you enter check in
  • write another temporary function where you enter check out

These temporary functions are only to troubleshoot what is going on.

Then see if these work as expected with correct date times. If these new temporary functions work correctly then you know there is somewhere in your code that is making check out wrong.

Because earlier you stated that check in was correct but check out was not correct.

1 like
jabrij93's avatar

@jlrdw Yes check in was correct but check out wasn't. i'll try to do as you said above

jabrij93's avatar

@jlrdw finally works. I tried to refer to a senior developer, he was the person in charge of deploying it to shared hosting, after checking here and there, tweaking here and there. he found the solution to actually change the $table->id() in migration database table to be $table->primary('id') . somehow, why in the local, $table->id() works but need to change to $table->primary('id') in shared hosting, still unclear. perhaps there might be a differences in how mysql works in windows and how mysql works in linux. thanks !

Please or to participate in this conversation.