Skip to content

Instantly share code, notes, and snippets.

@riipandi
Created June 26, 2018 21:42
Show Gist options
  • Save riipandi/646ad066a73b31db87d10640a3335164 to your computer and use it in GitHub Desktop.
Save riipandi/646ad066a73b31db87d10640a3335164 to your computer and use it in GitHub Desktop.
Laravel Snippets
<?php
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreateUsersTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('users', function (Blueprint $table) {
$table->uuid('uuid')->primary();
$table->unsignedSmallInteger('group_id')->default(6);
$table->string('first_name');
$table->string('last_name');
$table->string('username')->unique();
$table->string('email')->unique()->index();
$table->string('password', 64);
$table->rememberToken();
$table->timestamps();
$table->softDeletes();
$table->foreign('group_id')->references('id')->on('user_groups')->onDelete('cascade');
});
DB::unprepared("
CREATE TRIGGER `users_fullname_insert_trigger` BEFORE INSERT ON `users` FOR EACH ROW
BEGIN
SET NEW.full_name = CONCAT(NEW.first_name, ' ', NEW.last_name);
END
");
DB::unprepared("
CREATE TRIGGER `users_fullname_update_trigger` BEFORE UPDATE ON `users` FOR EACH ROW
BEGIN
SET NEW.full_name = CONCAT(NEW.first_name, ' ', NEW.last_name);
END
");
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
DB::unprepared("DROP TRIGGER IF EXISTS `users_fullname_update_trigger`");
DB::unprepared("DROP TRIGGER IF EXISTS `users_fullname_insert_trigger`");
Schema::dropIfExists('users');
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment