简体   繁体   中英

How to generate unique student id (e.g 2014-444-168) in laravel

I am creating an enrollment system that should generate an unique student id upon submitting a form. The unique id is not included in the form field. Rather it will be generated using the store method in the StudentController class. Please refer to my code below.

class StudentController extends Controller
{
    public function __construct()
    {
        $this->middleware('auth');
    }
    public function store(StudentRequest $request)
    {
        $student = Student::create($request->all());

        $student->id_no = "12345669";

        $student->save();

        return redirect('index');
    }
}

As shown above I want to create the student id. Can someone please suggest best practices of doing the above task

This will be unique:

$student->id_no = date("Y").'-'.$student->id;

And even this will be unique:

$student->id

Because it's auto incremented primary key and it can't be duplicated.

You can append a string to the $student->id and generate similar but unique values. Like you find in college ids.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM