Skip to content

Instantly share code, notes, and snippets.

@lcjury
Created April 22, 2019 03:17
Show Gist options
  • Save lcjury/b086a116824067127a320ca76145a738 to your computer and use it in GitHub Desktop.
Save lcjury/b086a116824067127a320ca76145a738 to your computer and use it in GitHub Desktop.
[Laravel] Macro to create a column and foreign key in migration with just one command
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Str;
Blueprint::macro('createForeign', function($model) {
$column = "{$model}_id";
$table = Str::snake(Str::pluralStudly($model));
$this->unsignedBigInteger($column);
return $this->foreign($column)->references('id')->on($table);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment