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:
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.
@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.
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.
@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'];
}
and changing my typo. use Illuminate\Http\Requests; to use Illuminate\Http\Request;😀
I have noticed the following though.
My User model uses
class User extends Authenticatable instead of class User extends Model. I'm using the default User.php file.
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.
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.