Have you seen our video tutorials? Check 'em out!

[Answered] Add a field without destroying data

emergingdzns - 1 month ago

I've got a module mostly complete and the client has asked for another column of data (so a new field in the form) to be added, but we already have a lot of data in the tables for the module. This particular model is ok to wipe out but so far all I've been able to do is a reinstall of the module, causing all of the data in the tables to get wiped out. I have to keep exporting, reinstalling, and then reimporting.

Is there a way to add a single field to a model as a migration and not destroy all the data in the whole module?

Answer

ryanthompson - 1 month ago

You can add a migration to the model with laravel command:

php artisan make:migration --addon=you_addon

Then use the fields(), assignments(), and streams() methods to create stuff like you normally would:

$stream = $this->streams()->findBySlugAndNamespace('foo', 'bar');

$field = $this->fields()->create($attributes);

$this->assignments()->create(compact('stream', 'field');

ryanthompson - 1 month ago

You can add a migration to the model with laravel command:

php artisan make:migration --addon=you_addon

Then use the fields(), assignments(), and streams() methods to create stuff like you normally would:

$stream = $this->streams()->findBySlugAndNamespace('foo', 'bar');

$field = $this->fields()->create($attributes);

$this->assignments()->create(compact('stream', 'field');

emergingdzns - 1 month ago

Awesome! Thanks!