0

I'm trying to create a table, which contains an "email_address" field and an "activation hash" field. The default value of "activation hash" field should be

sha1(microtime().email_address))

Is it possible to set this up using laravel migrations and how can I do this?

Surely, i should say, that i'm using postgres as DB engine

Quimmo
  • 65
  • 1
  • 5

2 Answers2

0

Most database engines are limited as to what values can be set as a default to columns. Depending on your database engine, you might be able to do it via an event/trigger but it's easier and arguably better to solve this at an application level with Eloquent events.

Elias
  • 1,532
  • 8
  • 19
0

When I need to do something like this, I split the code in 2 pieces:

  1. Create the column (using Schema Builder)
  2. Set the value (over PHP, loading all models and updating one by one in a foreach)

Note: Both steps can be done at migration file.

You can't do it entirely at MySQL (if you are using MySQL) query since it doesn't have a microtime() function.

But I found this UDF implementation for microtime() on MySql:

https://github.com/JohannesMP/MySQL-udf-microtime

If you are not updating a huge dataset, I would stick with the first solution. But if you are updating a huge dataset, so the MySQL implementation will be faster.

Elias Soares
  • 9,884
  • 4
  • 29
  • 59