Created
April 22, 2019 03:17
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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