Laravel: Create Model with Migration, Controller and generate DB Table
Create new Model
Consider you decided to build a Blog Application in Laravel, and as every blog application has Posted. You decided your first model to be Post
. To generate a Post model in your application, Open your Terminal / Command Prompt and navigate to the project root.
Run the following command
Once this command is executed, open your editor.
Notice that a new Post.php
model is created in your project under App
directory
Also notice a new migration file is created under the folder database > migrations
And, also a new resourceful Controller is generated under the directory App > Http > Controllers
Generate Database Table
Since we are ready with our first Model, Let’s go ahead and modify the migration file to add columns in our posts table.
To add some fields, Posts are supposed to have a title and a body. Let’s add these fields to our migrations file.
Great, we are now ready to generate the Posts table from our migration file.
Go to your terminal and generate the following command.
There you go! You now know how to create a model in Laravel and also how to create a corresponding database table corresponding to the Model.
0 Comments
CAN FEEDBACK
Emoji