Skip to content

Instantly share code, notes, and snippets.

@mikkame
Created December 9, 2019 11:13
Laravelで足りないカラムを保存時に自動的に作ってくれるやつ
<?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