/.php
Created
December 9, 2019 11:13
Laravelで足りないカラムを保存時に自動的に作ってくれるやつ
This file contains hidden or 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
<?php | |
protected static function boot() | |
{ | |
parent::boot(); | |
self::saving(function($me){ | |
$columns = array_flip(\Schema::getColumnListing($me->getTable())); | |
foreach($me->attributes as $key => $val) { | |
if (!isset($columns[$key])) { | |
Schema::table($me->getTable(), function (Blueprint $table) use($key) { | |
$table->string($key)->nullable(); | |
}); | |
} | |
} | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment