mercredi 26 août 2015

Laravel - Transform data during migration

Is it possible to transform a table's data during a migration? As an example, I would like to add a new column, but the default value isn't sufficient for existing data (the default value is fine for new records, but existing data already has a structure that needs to be put into the new field).

I would like to be able to do something like this:

Schema::table('comments', function(Blueprint $table) {
    $table->unsignedInteger('level')->default(0);
    $table->text('lineage');
});

foreach (Comments::whereNotNull('parent_id')->get() as $c) {
    $c->level = $c->calculateLevel();
    $c->save();
}

so that 'level' doesn't have to be calculated on demand (it can be intensive if it's a long comment tree)

I could write a command which calculates and saves these level values, but it feels like something that should happen with the migration.



from Newest questions tagged laravel-5 - Stack Overflow http://ift.tt/1KP8K05
via IFTTT

Aucun commentaire:

Enregistrer un commentaire