Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

yarkin's avatar

How to generate Laravel's Model, Model's Migration and Controller programmaticaly

So i want to generate Model then Model's migration programmatically. What I mean by that is; for instance I have a form that accepts model name and migration fields. I want to be able to submit form and let my Laravel code generate model and migration automatically. Also I want to be able to generate controller code based on some logic. Is it possible to achieve what I describe with Laravel and PHP?

0 likes
31 replies
yarkin's avatar

@webrobert But still, how can I add fields to migrations? How can is generate controller code?

yarkin's avatar

@newbie360 how can I send data via artisan commands to stubs then? How can I edit the existing Model, Migration, and Controller code? Stubs are good but how can I generate those properly using Laravel and PHP?

martinbean's avatar

@webrobert Why on earth do you want form submissions responsible for generating server-side code? Users should not be able to create files like this at all.

yarkin's avatar

@martinbean I want to make something that's why. Of course, not everybody can access this stuff and it will be protected and stuff, at least I hope so.

martinbean's avatar

@webrobert Sorry! Yeah, was meant to be directed to @yarkin. Typed reply on mobile and think your name just came up first. Sorry again!

I want to make something that's why. Of course, not everybody can access this stuff and it will be protected and stuff, at least I hope so.

@yarkin Artisan has commands for creating these things. You don’t want to be doing them through forms. If you’re creating files via HTTP requests then—ignoring the massive security risk—it also means these files aren’t going to be version-controlled if they’re just created on an ad hoc basis.

1 like
yarkin's avatar

@martinbean What if i come up with a secure way and automate the version-control process? Isn't it possible?

martinbean's avatar

@yarkin You’ve still not said why you’re wanting to do this.

It’s not done because it’s a bad idea.

Snapey's avatar

it seems a totally crazy idea

2 likes
yarkin's avatar

@Snapey Why? Is it not practical or insecure or not a good idea at all?

webrobert's avatar

@martinbean @yarkin @snapey

In about 1999 I wanted to save credit cards and change them every month. People thought this was a crazy idea. Today they call that a subscription.

I agree it’s not common. But are there not countless sites already doing this? What about push button Wordpress installations?

It really seems this is actually the evolution. Today we have drag and drop websites. What was once a backend function is now front facing.

@yarkin I think it probably depends what your building and why. I think it’s curious. Not crazy. Impractical, maybe.

1 like
yarkin's avatar

@webrobert, of course, I try to take their inputs but I just wanted a simple answer yet I get replies that aren't really related to my questions.

webrobert's avatar

@yarkin I’m not sure you will find a code example anywhere that directly does what you want. But I did give you the answer. That beeeze code does make controllers and migrations with options. Aside from make a form and wiring it up for you. What else do you want?

yarkin's avatar

@webrobert basically I want to edit the whole routes folder's content, register new migrations, models, controllers. Change application's configurations fully. But I will use third-party services to deploy the admin panel generator so that it will have its own file system. And also an ai analyzer that analyzes code for threats and security.

webrobert's avatar

@yarkin I get it. But the question and topic was

How to generate Laravel's Model, Model's Migration and Controller programmaticaly

You said we’re off topic. And you wanted s simple answer. I think you already got the best answer. Look at how breeze does it. Given the maker of the framework wrote it. He just happens to call it via a command.

Wire it up to a form and try it. Just don’t break the internet.

yarkin's avatar

@webrobert Thank you, you're right I should've stick to my questions, sorry about that.

Snapey's avatar

it's a crazy idea because none of the rest of the application is going to be able to use this model and controller because they don't know anything about them

next you will want a code syntax highlighting editor that can allow the user to write their own functions in these new controller, and then to add routes to the web.php etc etc

Do you really want to support something like this? What possible use case is there for something that has to be installed by the user as a package but then they have to go through a web front end to add their code?

1 like
yarkin's avatar

@Snapey yes I want to support this. Besides, if everything goes as planned, it will not be free. You have a point but could you at least answer my question? Cause these replies gone out of context. I just want simple answers like 'You can create models like that', 'Introduce them to the application like that'. I want to achieve my goal yet I get 0 support nor answers.

Snapey's avatar
Snapey
Best Answer
Level 122

@yarkin ok

models, look how artisan creates models. It does it by using stubs with placeholders. You can copy that approach or come up with your own. You are going to need the shell of a class with places where you can string replace things

alternately call artisan from within your code. This is covered in the docs

https://laravel.com/docs/8.x/artisan#programmatically-executing-commands

As for migrations, look at a migration file. It's just a php class. It uses Blueprint to create or edit tables. Easy enough to replicate in your own code

1 like

Please or to participate in this conversation.