For my appointment table, I'm letting MySQL increment my id (primary key) for each new row. My application uses this appointment id in my routes, which is fine. But in order to be able to reference a specific appointment id without "showing" the id itself (in a link sent by email for example), I would like for my appointment table to have a column which contains a string that is unique to this specific appointment (but not part of the primary key).
public function up()
{
Schema::create('appointments', function(Blueprint $table)
{
$table->increments('id');
$table->integer('client_id')->unsigned();
$table->foreign('client_id')->references('id')->on('clients')->onDelete('cascade');
$table->integer('user_id')->unsigned();
$table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');
$table->dateTime('appointment_at');
$table->string('my_unique_key')->unique(); --> Want unique generated string
$table->rememberToken();
$table->timestamps();
});
}
I've read about UUID, Laravel's str_random() and md5. Of course none would generate a unique string that I can use when inserting a new row to my appointment table.
So I guess would need to have retry logic when inserting to verify that my string is unique, unless MySQL can do that? I'm afraid retry logic would create overhead and lower the performance of my app if have a lot of users adding appointments at the same time.
Would an MD5 of my created_at timestamp be close to unique? What if I concatenate the values of two variables (ex: created_at and appointment_at) and then do a MD5, would that do the trick?
What would be the syntax for retry logic in here?
public function store(AppointmentsRequest $request)
{
$appointment = Appointment::create(Request::all());
return redirect('appointments/'.$appointment->id );
}
via Chebli Mohamed
Aucun commentaire:
Enregistrer un commentaire