I am doing a test, but not sure how to do it with laravel migration file. In short, there is the auto incrementing id
field (which will always be a positive integer). But I also want a negative_id
field, which would be the value of id * -1
, and would be automatically inserted in sync when an id
is inserted to the database. So when the first user is added, id
is automatically added as 1
so negative_id
should be added as -1
for that row, since 1 * -1 = -1
. The table should look like this:
| id | negative_id
------------------
| 1 | -1
| 2 | -2
| 3 | -3
Below is my current migration file:
<?php
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreateUsersTable extends Migration {
public function up(){
Schema::create('users', function (Blueprint $table) {
$table->increments('id');
$table->integer('negative_id'); // how to make this id*-1 value
});
}
public function down(){
Schema::dropIfExists('users');
}
Any idea how to accomplish this from the migration file?