skip to Main Content

Working on a task where we need to create Model and Migration files programmatically. Also in the Model there will be some predefined functions. These functions will be same for all models. The functions are just relations with other Models. I have searched for a few options and found that Laravel has Stubs and we can create custom stubs which are great but don’t think there is any option to pass params to the stub file when generating it.

In this case, we want to create a model and migration with dynamic columns. May be there is no easy way to do it but in case anyone has done it already, can you please provide me a hint of how you did it.

Trying this laravel package
https://github.com/laravel-shift/blueprint
.It can generate models, migrations, controllers from Yaml file. May be we can create a yaml file dynamically and then publish it.

Thanks

2

Answers


  1. you can write your own command.
    for example i wrote a command for generate repository pattern in my projects

    php artisan make:repository repoName
    
    Login or Signup to reply.
  2. You can publish stubs

    php artisan stub:publish
    

    Create your custom stub

    <?php
    // stubs/controller.custom.stub
    namespace {{ namespace }};
     
    use {{ rootNamespace }}HttpControllersController;
    use IlluminateHttpRequest;
     
    /**
     * Hello from the custom controller stub
     */
    class {{ class }}
    {
        //
    }
    

    And call it

    php artisan make:controller --type=custom MyController
    

    Watch –type option

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search