Reading Laravel Session last_activity

Ask about general coding issues or problems here.

Moderators: egami, macek, gesf

Post Reply
sheenlim08
New php-forum User
New php-forum User
Posts: 7
Joined: Sun Sep 01, 2019 1:17 pm

Sat Apr 17, 2021 1:18 pm

Hi PHP Dev, I need some help in this laravel thing im trying to learn, although i know this site is not dedicated to Laravel i'd just like to be pointed the right way.

I have a table database based session in laravel, how do I read the last_activity field? When I read the value from javascript as in "new Date(item.last_activity)"

The year seems to be wrong. At first I thought it was the time on the server that is wrong but then when i run "date" on the terminal it shows correct timezone + time + date.

the last_activity field is populated automatically by laravel i believe on is a integer datatype.

Sample Data from API: { "0": { "row_id": 1, "user": "Default Admin", "user_agent": "Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:87.0) Gecko/20100101 Firefox/87.0", "ip_address": "192.168.56.1", "last_activity": 1618127344 } }

The VUE component that reads the last_activity is
<td>{{ new Date(item.last_activity) }}</td>

Output: Tue Jan 20 1970 01:28:48 GMT+0800 (Philippine Standard Time)

I think laravel is putting on the timestamp wrong or laravel because the field is automatically populated. Any one has any idea or that can enlighten me?
sheenlim08
New php-forum User
New php-forum User
Posts: 7
Joined: Sun Sep 01, 2019 1:17 pm

Sat Apr 17, 2021 1:51 pm

I found a solution using the Carbon class
(also make sure that the time on the server is correct, mine was still a different timezone)

on the Laravel PHP side I read the last_activity as

Code: Select all

public function sessions(Request $request)
   {
      $jsonReposeData = [];

      foreach ($request->user()->getSessions()->get() as $currentSessionObject) {

         // If the session age has not reached the SESSION_LIFETIME value
         if ($currentSessionObject->LastActivity() > Carbon::now()->subMinutes(env('SESSION_LIFETIME'))->getTimestamp()) {
            $currentSession = [
               "row_id" => $currentSessionObject->RowID(),
               "user" => $request->user()->getUser()->get()->first()->getFullName(),
               "user_agent" => $currentSessionObject->UserAgent(),
               "ip_address" => $currentSessionObject->IPAddress(),
               "last_activity" => Carbon::parse($currentSessionObject->LastActivity())
               //"last_activity" => $currentSessionObject->LastActivity()
               //
            ];


            array_push($jsonReposeData, $currentSession);
         }
      }

      return response()
         ->json([
            'success' => true,
            'sessions' => $jsonReposeData,
            'responseMessage' => 'Your sessions has been retreived'
         ]);
   }

On the javascript front end side, the data is read by (new Date)

Code: Select all

<template>
  <v-simple-table :loading="loading">
    <template v-slot:default>
      <thead>
        <tr>
          <th class="text-left">
            User
          </th>
          <th class="text-left">
            IP Address
          </th>
          <th class="text-left">
            User Agent
          </th>
          <th class="text-left">
            Last Activity
          </th>
        </tr>
      </thead>
      <tbody>
        <tr v-for="item in sessions" :key="item.row_id">
          <td>{{ item.user }}</td>
          <td>{{ item.ip_address }}</td>
          <td>{{ item.user_agent }}</td>
          <td>
            {{ new Date(item.last_activity) }}
          </td>
        </tr>
      </tbody>
    </template>
  </v-simple-table>
</template>
sheenlim08
New php-forum User
New php-forum User
Posts: 7
Joined: Sun Sep 01, 2019 1:17 pm

Sat Apr 17, 2021 1:52 pm

Post Reply