Generate id auto in Laravel with date

Generate id auto in Laravel with date

Auto-Generating IDs with Date in Laravel

If you'd like to generate an auto ID in Laravel that includes the current date, you can easily achieve this by customizing your model logic. One effective approach is to use the model’s boot method or a dedicated service class. Here's how you can do it using the boot method in your model:

  

Prerequisites

Before working with Laravel, make sure the following tools and dependencies are installed on your system:

Git

Git is a version control system used for tracking changes in your codebase.
Download: https://git-scm.com/
To verify installation:

git --version

PHP

Laravel requires PHP version 7.3 or higher.
Check your version:

php -v

Composer

Composer is the dependency manager for PHP. Laravel uses it to install packages.
Download: https://getcomposer.org/
Verify installation:

composer --version

Web Server

While Laravel includes a built-in development server, it's recommended to use Apache or Nginx for production.
For development, you can start Laravel’s internal server using:

php artisan serve

Database

If your Laravel project uses a database, make sure the required database engine (e.g., MySQL, PostgreSQL, SQLite) is installed and configured properly.

User Model with Auto-Generated User ID

The following User model automatically generates a custom model user_id based on the current year and month. The format looks like:
KH-YYYY-MM-00001

File: app/Models/User.php

<?php namespace App\Models; use Illuminate\Database\Eloquent\Factories\HasFactory; use Illuminate\Foundation\Auth\User as Authenticatable; use Illuminate\Notifications\Notifiable; class User extends Authenticatable { use HasFactory, Notifiable; /** * Mass assignable attributes. * * @var array<int, string> */ protected $fillable = [ 'user_id', 'name', ]; /** * Hidden attributes for arrays. * * @var array<int, string> */ protected $hidden = [ 'password', 'remember_token', ]; /** * Casts for specific fields. * * @return array<string, string> */ protected function casts(): array { return [ 'email_verified_at' => 'datetime', 'password' => 'hashed', ]; } /** * Automatically generate user_id when creating a new user. */ protected static function boot() { parent::boot(); self::creating(function ($model) { $latestUser = self::latest('created_at')->first(); $currentYear = date('Y'); $currentMonth = date('m'); $prefix = "KH-{$currentYear}-{$currentMonth}-"; if ($latestUser && strpos($latestUser->user_id, $prefix) === 0) { $lastNumber = intval(substr($latestUser->user_id, -5)); $nextNumber = $lastNumber + 1; } else { $nextNumber = 1; } $model->user_id = $prefix . str_pad($nextNumber, 5, '0', STR_PAD_LEFT); }); } }

Example Output

DateNew User ID
2025-04-20KH-2025-04-00001
2025-04-20KH-2025-04-00002
Souy Soeng

Souy Soeng

Our website teaches and reads PHP, Framework Laravel, and how to download Admin template sample source code free. Thank you for being so supportive!

Github

Post a Comment

CAN FEEDBACK
close