Mercurial > packages > magicforger
comparison src/BlueprintExtensions/CustomBlueprint.php @ 0:329123c41eaf
initial commit
| author | luka |
|---|---|
| date | Thu, 23 Mar 2023 19:24:43 -0400 |
| parents | |
| children |
comparison
equal
deleted
inserted
replaced
| -1:000000000000 | 0:329123c41eaf |
|---|---|
| 1 <?php | |
| 2 | |
| 3 namespace Wizzard\Magicforger\BlueprintExtensions; | |
| 4 | |
| 5 use Illuminate\Database\Schema\Blueprint; | |
| 6 | |
| 7 class CustomBlueprint extends Blueprint | |
| 8 { | |
| 9 /** | |
| 10 * Create columns for the table that should be in every table. | |
| 11 * | |
| 12 * @return void | |
| 13 */ | |
| 14 public function defaultColumns() | |
| 15 { | |
| 16 $this->timestamps(); | |
| 17 $this->reference("users", "created_by"); | |
| 18 $this->reference("users", "updated_by"); | |
| 19 $this->softDeletes(); | |
| 20 } | |
| 21 | |
| 22 | |
| 23 public function reference($table_name, $column_name) | |
| 24 { | |
| 25 // check if the table exists before creating the reference | |
| 26 if (!Schema::hasTable($table_name)) { | |
| 27 throw new Exception("Table $table_name does not exist."); | |
| 28 } | |
| 29 | |
| 30 $reference_column_name = $column_name ?? ($table_name . '_id'); | |
| 31 | |
| 32 $reference_column = $table->unsignedBigInteger($reference_column_name); | |
| 33 $table->foreign($reference_column_name)->references('id')->on($table_name); | |
| 34 | |
| 35 return $reference_column; | |
| 36 } | |
| 37 | |
| 38 } |
