Creating Initial Laravel Structures for The Models and Its Migrations

JuanD MeGon
A free video tutorial from JuanD MeGon
Online instructor, web developer, master & systems engineer
4.6 instructor rating • 18 courses • 22,627 students

Lecture description

During this class, you will see how to generate the initial structure of the models of the system, along with their migrations through the use of PHP artisan commands. Remember that the order of the migrations is crucial, so you must pay attention to the order in which each model is created with its respective migrations.

Learn more from the full course

RESTful API with Laravel: Build a Real API with Laravel

Build a RESTful API for a market system using Laravel and dominate the most challenging features of APIs with Laravel

14:42:27 of on-demand video • Updated June 2020

  • Create your own projects with Laravel (not only RESTful APIs)
  • Make the most of your work tools (Sublime Text 3, Git, Github, Composer, PHP artisan and Laravel)
  • Generate and maintain the structure of the database in an automated way with Laravel and the PHP Artisan console
  • Use PHP Artisan effectively and be much more efficient
  • Create and configure your own local development environment
  • Properly configure your development environment for all your projects
  • Hone the main HTTP methods/verbs for HTTP and RESTful APIs
  • Use free professional tools to improve your productivity
English Now that we are ready to understand the structure of our RESTful API, the models and the relationship between those models. We are able to implement the classes that represents those specific models. In the context of the RESTful API, the models are called basically resources. In fact in a RESTful API, you can have several resources and maybe some of them are not related with a specific model but it is very probable that every model that you have is a resource in your RESTful API. So we are going to create now our the representation of the resources using models from Laravel. Laravel provide us a PHP artisan command to create easily the basic structure of a specific model and its migration. The migration is going to allow us to programmatically create the structure that represents that specific resource inside the database. So by default, Laravel comes with one model that is the user model here. And we are going to use this model. Of course, we are going to perform some modifications over this. And additionally we have the migration for this specific model. You can see here the specific migration and the specific attributes. Now we are going to do the same with our resources, but of course remember that "Buyer" and "Seller" extends directly from user not from model and the other resources are regular models that don't have any kind of inheritance between our own resources or models. So let's to open a connection with our virtual machine. Be sure that the virtual machine is running. Go to the RESTful API folder inside the virtual machine. And there, you can see the usage of the "make" model command. You can see that we need to specify the name, and we have any other options like the migrations and the controller. It means that we can create the migration and additionally we can create a controller for that specific resource. Additionally we can specify if that controller corresponds or a controller for a resource. For now, we just want to create the model and the migration without the controller. We are going to create the controllers later in a future class with a specific folder and other options. So we just need to create the model with the migration at this point. So we are going to start with the "Buyer" and the "Seller" models without the migration because remember those models extend from user directly. It means that doesn't require a specific migration because those models have the same attributes. We have those models created so, we can see here "Buyer"and "Seller". So let's say the "Buyer"extends from user and the same from "Seller" as we are no using the definition of model anymore. We just don't need this line. Now let's to create the other three models with its specific migrations. We have "category", "product" and "Transaction". So we already have the six models that we are going to use in our RESTful API and we can proceed in the next class with any future structures. See you then.