May Sale! All accounts are 40% off this week.

peanut's avatar

Make migration from code

So I'm trying to create a migration from one of my model's with it's model name (building a CMS so every time they build it, it will create the database fields (if they've changed from the last migration).

This is what I have so far:

$name = $this->getClassName();
        
$command = $this->artisan('make:migration create_' . strtolower($name) . '_table');
dd($command);


/****** METHOD IN CLASS ********/
/**
 * Call artisan method
 */
protected function artisan($command, array $args = [])
{
    $process = new Process('cd ' . base_path() . ' && php artisan ' . $command);
    return $process->run();
}

My question is after the migration is created, is there a way to edit it within Laravel? Or will I have to do some weird opening of the file, editing the up and down methods?

Thanks for any help!

0 likes
3 replies
martinbean's avatar

@peanut Not sure what you’re trying to do. Why are you creating migrations in this way?

peanut's avatar

Similar to Silverstripe. You create a new model for each page type you want and then go to dev/build and it grabs the models for each page type and creates the appropriate database tables and foreign key constraints to each site tree object. So the homepage will be unique compared to other pages. It may have more fields like a banner image, services section, etc. I'm making it user friendly so they would just create a Homepage model in App/Models/Page or whatever. I'm grabbing each new page type/model from that directory and am creating migrations on it.

You can see what I mean by going here: https://docs.silverstripe.org/en/3.2/tutorials/extending_a_basic_site/

Please or to participate in this conversation.