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

francisceril's avatar

'status' or 'isActive' ?

Well, just looking for a naming convention advice. How do you usually name your table column for a boolean status?

Also, how should I add a last_login field for admin to check user activity?

Thanks

0 likes
9 replies
Nakov's avatar

I would go with is_active column name if it is a boolean type, a status will be expected to have more than two states.

As for the last_login I will add a timestamp field in the User table and touch it each time a user logs in by subscribing to that event. More on this here:

https://laravel.com/docs/events#event-subscribers

2 likes
bobbybouwmann's avatar

Normally when setting a boolean value I try to be explicit as possible. So my column name would probably be is_active or is_cancelled.

Another approach you can take is using a datetime. It's the same approach as SoftDeletes and the deleted_at column. This way you also know the activation date. If that is useful information a date might be a better approach. You can, for example, call this column activated_at.

About the last login functionality. This just depends on what you want to do with this. Do you want to show the last 5 records? In that case, you can use a one-to-many relationship. If you just use the one column in your users table I would probably take the same datetime approach and call the column last_logged_in_at as an example.

Let me know if that is useful for you ;)

2 likes
francisceril's avatar

@nakov Thanks

@bobbybouwmann Thanks. I think I'm okay with the is_active for my requirement. The same thing with the last_login since all I really need is the last activity.

I did some searching and came up with this. I haven't tried it yet coz' I'm not on my dev machine. Please do tell me if it's correct.

create_users_table.php

$table->datetime('last_logged_in_at')->nullable();

LoginController

function authenticated(Request $request, $user)
    {
        $user->update([
            'last_logged_in_at' => Carbon::now()->toDateTimeString()
        ]);
    }
Nakov's avatar

@francisceril yes, that's good.

For the column it can either be dateTime or a timestamp field.

And then you just need Carbon::now() you will not be able to store String value within a date field.

To get a String value you can create accessor in your User model:

public function getLastLoggedInAtAttribute()
{
    $time = $this->attributes['last_logged_in_at'];

    if($time) // this check is because it can be `null`
    {
        return $time->toDateTimeString();
    }

    return null;
}

Also don't forget to add it to your $dates array so it can be automatically cast to Carbon.

In your User model as well add this:

protected $dates = [ 'last_logged_in_at' ];
1 like
francisceril's avatar

@nakov

I think I'm missing something. My code above doesn't do anything. I also tried your suggestion of removing the toDateTimeString like so

$user->update([
    'last_logged_in_at' => Carbon::now()
]);

Haven't tried the accessor though but the first option should work.

So far this is what I have added.

LoginController.php

use Illuminate\Http\Requests;

function authenticated(Request $request, $user)
{
    $user->update([
        'last_logged_in_at' => Carbon::now()
    ]);
}

User.php Model

protected $dates = ['last_logged_in_at'];

What am I missing here?

bobbybouwmann's avatar

@francisceril Code looks good! You need to make sure the last_logged_in_at column is marked as fillable as well

class User extends Model
{
    protected $fillable = ['name', 'email', 'last_logged_in_at'];

    // This gives a Carbon instance back, useful for formatting and validation
    protected $dates = ['last_logged_in_at'];
}

Documentation: https://laravel.com/docs/6.x/eloquent#mass-assignment

1 like
Snapey's avatar

or

$user->last_logged_in_at = Carbon::now();
$user->save();
1 like
francisceril's avatar

@bobbybouwmann

Yes I do have this line on my User model.

protected $fillable = ['name', 'email', 'last_logged_in_at'];

I was able to make it work by using @snapey's

$user->last_logged_in_at = Carbon::now();
$user->save();

and changing my typo. use Illuminate\Http\Requests; to use Illuminate\Http\Request; 😀

I have noticed the following though.

  1. My User model uses class User extends Authenticatable instead of class User extends Model. I'm using the default User.php file.

  2. The time being recorded in the User table is wrong. I think it's using another timezone. When I run select Now(); from MySQL terminal, it outputs the correct time.

  3. The updated_at column is also being changed with the last_logged_in_at column. That column should not change on login. Tried adding this public $timestamps = false; but I think it completely disables both the created_at and updated_at columns.

Snapey's avatar

Yes, User model should extend Authenticatable.

The time will be based on whatever is specified in config/app.php (usually UTC)

You can stash the timestamps, update and then restore

eg

            $timestamps = $user->timestamps;
            $user->timestamps=false;   // avoid view updating the timestamp

            $user->last_logged_in_at = Carbon::now();
            $user->save();

            $user->timestamps=$timestamps;   // restore timestamps
1 like

Please or to participate in this conversation.