Recap and Goals0:08
Let's start with a quick recap. In the previous episode, we created a new lateral project with the live wire start kit and changed some things. Mainly we changed this welcome view to just show our app heading and subtitle. And once you log in, we changed this dashboard view and we added a new bookmarks route and view. We used some blade components and layouts to make all of this happen.
We used some blade components and layouts to make all of this happen. So for now we have hardcoded this one single role and displaying the same. So in this episode we create database tables and try to fetch the roles from those tables. In Laravel, we use models to communicate with the database tables. Since we have these job roles, naturally we need a roles table,
Creating Role Model0:55
Since we have these job roles, naturally we need a roles table, which means we need a model which can communicate to that role table and that could be the role model. Let me open the code Explorer and models are located within app models. As we saw in the previous episode, there's a user model already created to communicate with the user table. Now here we can create another model role manually
to communicate with the user table. Now here we can create another model role manually or we can use the command line tool bilateral. Open the command line, I'll open a new tab. There's a command line tool, PHP Artisan with a huge list of commands. So if you just type list, you'll be able to see the entire list of commands available to you. The things we would be using the most would be make like we would use this to make middleware,
The things we would be using the most would be make like we would use this to make middleware, make migration, make model, make controller, and uh, we would also use the migrate command and a few more. So if you'd like to browse this list and get an idea what's possible, you can do that. So now let me type PHP artisan make model. So instead of doing this manually, like creating a file
artisan make model. So instead of doing this manually, like creating a file and adding the content, this will be much easier. Hit enter and now it prompts what should the model be named as You can see the example it's flight that is singular and you can see that the first letter is capital. So that's the convention followed by lateral. So if we have a rules table, the model name should not be rules. Instead it should be rule within our capital.
the model name should not be rules. Instead it should be rule within our capital. So I'll type rule, hit enter and now I'm asked if I would like any of the following a database, CDO so that we don't have to manually enter all the uh, table records or in case we are sharing this project with somebody in the team. We could create a CRA sample set of data in the table so that the team member doesn't have to recreate manually
We could create a CRA sample set of data in the table so that the team member doesn't have to recreate manually and there's a factory. If you'd like to generate huge amount of users or posts, something like that from requests migration, we would need that. So we would need a CDR. I will hit space to select that we would need a migration. Now what's a migration? Migration is how you use version control with databases.
Now what's a migration? Migration is how you use version control with databases. So you can use migration files to define how your tables are structured. We'll do that soon and scroll down further. Yeah, we don't need anything else. So I will select C, D, E and migration and net into. Okay, so we have our model created within app models. We have our migration within database migrations and then we have the CDE also created.
We have our migration within database migrations and then we have the CDE also created. Look at these names. We didn't have to specify any of these. It automatically uh, recognize that we are trying to create the roles table and that's available in the name and we need a role CO. So let me open up all of these next to each other. I'll close the command line and let's look at the role model. There's nothing here, just a skeleton, uh,
Defining Roles Migration4:11
and let's look at the role model. There's nothing here, just a skeleton, uh, which is extremely easy for us to get started with and we don't need anything for. Now let's look at the migration. Now this is where you can define your table structure so that this files can be shared in introduction. This files can be shared with the team members where they all can easily duplicate your entire database structure.
where they all can easily duplicate your entire database structure. Now we create this rules table using schema facade. Now facade in lateral is a way to call complex functions, uh, easily in a readable way. I mean you don't have to worry much about it. The, you will see these facades in a lot of places for now. Like we saw that initially as well in the routes file. This is the route facade. You will come across a lot of them.
This is the route facade. You will come across a lot of them. You can dive deeper later. Let me close this and come back. So this is how you basically create a new table. Now this is already entered for us and we also have two columns typed here. That is the id, which creates a new auto incrementing, big integer. So you don't have to do that manually. And also timestamps. This creates two columns in our table,
So you don't have to do that manually. And also timestamps. This creates two columns in our table, created at an updated at everything automatic. You don't have to do anything. So now in our roll table, we need some columns. First we need a string, which is the title of the roll. So we use the string method. This is the type of the column and the name of the column is title. Let's duplicate this line.
and the name of the column is title. Let's duplicate this line. And the other one is uh, the company name. That's also a string. Yes. And then we have another string, which is um, the location like remote or is it hybrid, is it in office and so on. And another string, which is the type if it's part-time or full-time. And then we have the last data. That is the details.
if it's part-time or full-time. And then we have the last data. That is the details. When you click on view details, you're able to see the details, right? That will not be a string because it's a little longer. So we will use the text column for this. And the name of the column will be details. Now you can see all the available types here, all the methods in the lateral documentation. Alright, so the schema is specified within the function up.
all the methods in the lateral documentation. Alright, so the schema is specified within the function up. That is once, once you run the migration, this is the function that will be called and it'll create the new table for some reason. If you want to remove the table or if you want to undo that action, you roll back. So when you say migrate, roll back, this down method is called and it drops the uh, entire table.
this down method is called and it drops the uh, entire table. It basically reverses what we did. And now let's use the command line and migrate, let me say PHP artisan migrate. That's the command to run all the migrations that haven't previously been run. So creating all the tables or updating the tables if required,
Viewing Database in TablePlus7:25
So creating all the tables or updating the tables if required, whichever files have been created after the previous migration, they'll all be uh, run. So let's hit enter. It says it's done, but how do we check? It would be great if there's a visual way to see your table, see your data, right? For that we use something called as table plus. That's a very popular application. So you can go to your browser
That's a very popular application. So you can go to your browser and look for table plus table plus.com. This is the exact application. Download the free version. That's more than sufficient for now. Once you install table plus, you can create a new con connection. Select SQL it here, click on create, give a name to your, uh, connection. I would just call it Taleo.
give a name to your, uh, connection. I would just call it Taleo. And here you need to specify the database path. Let me show you. Go back to Visual Studio Code. In the route of your project, you will see the database folder. Within this you'll be able to see this SQ lite file. That's the one you need to link to. So click right click on this. You can copy the path, go back to table plus paste it here
So click right click on this. You can copy the path, go back to table plus paste it here and test your connection. If all goes well, you'll be able to see the record here. And now click on connect. So here you can see the list of tables in your database. We did not create so many tables, but these are all created by later. Well, for the functioning of different features there we saw that the user's table was already created.
Well, for the functioning of different features there we saw that the user's table was already created. This is what it is with the single user that you might also be able to see. And here's the roles table that we just created with all the columns that we specify. Like I mentioned, we even have the ID and the created at and updated at columns. Now instead of manually entering some uh, rules here, let's do that using the CD, the rule cer that we created.
Seeding Roles Data9:17
Now instead of manually entering some uh, rules here, let's do that using the CD, the rule cer that we created. Going back to the code rule CDO, when you run the CDR in command line, this run function will get executed. So here let's define a new array with some rule that is, uh, let's say we have the title full stack developer and I will paste the rest of the things so you don't have to see me typing for five minutes.
and I will paste the rest of the things so you don't have to see me typing for five minutes. Okay? So this is the rule. Now the way we enter it into the database is using the rule model. So we say rule, and then there is a create method that we can use over the model to help us create this record. So all you have to do is pass in the array
to help us create this record. So all you have to do is pass in the array and you'll have to import this class. So you can right click and import class or you can also use this shortcut. So now the class has been imported. Okay, let's open the command line and say PHP artisan DB colon seed. That's the command with the option of the class.
DB colon seed. That's the command with the option of the class. So here, there's already a suggestion that is rolled CO let me hit enter. And it says sing database. Looks like it's done. Let's go back to table glass and refresh. Yes, we do have the record created. Similarly, you can add all the rules that are required to build your app. Of course, in the future you would want
to build your app. Of course, in the future you would want to create an admin user with all the functionality to delete rules, create new one edit. We're not gonna do that in this uh, course, but for now you know how to seed the initial data for you to create the app. So going back instead of uh, one roll, let me add maybe nine roll. Once again, I've pasted all the rules here.
add maybe nine roll. Once again, I've pasted all the rules here. And instead of creating this once, let me use for each loop to create all the rules. So I will use for each roll as roll. And there we go. So let me open the command line again and uh, I will roll back so that we don't have the role stable. We don't have the single record that we created PHP Artisan Migrate Rollback.
We don't have the single record that we created PHP Artisan Migrate Rollback. So now the roll stable doesn't exist. I will create it once again by saying migrate. And once again, D, B, C. Okay, if all goes well, we should be able to see nine roll here. And there we go. Now that we have them all here, instead of hard coding right here in our um, dashboard view, let's try and fetch them from the database.
Fetching Roles in Dashboard12:19
of hard coding right here in our um, dashboard view, let's try and fetch them from the database. So come back to the code. Let me close all of these and let me open the dashboard view first and scroll down. So this is where we have hardcoded all of these values. Now we need to get the rules data from the table and pass it to the view. And as discussed controllers is something
and pass it to the view. And as discussed controllers is something that helps us do that. That's where that's the right place to put this part of the code, this logic. So open the terminal once again and let's create a new controller using the same make command make colon controller, controller. Yeah, once again, what should the controller be named? And as you can see, the convention is to name it
Yeah, once again, what should the controller be named? And as you can see, the convention is to name it with the user as in the model followed by the controller. So here we say roll controller and which type of controller would you like? You might not really understand what this is. Um, but for now our controller, the only thing, the only function it has is to get the roles and pass it to the view. So that's all the role controller needs to do.
and pass it to the view. So that's all the role controller needs to do. I will select invo. Invo controller has a single method and that's called let's hit enter. It's created. Let me open this and close the terminal. And here's the role controller with the single invoke function. This is where we get all the roles. And how do we do that? Once again, using the role model
This is where we get all the roles. And how do we do that? Once again, using the role model and an eloquent method. The way create was an eloquent method. We have all that is I'm gonna just get all the rules from there. Once again, you'll have to import the class. And now you need to pass this to the view. So earlier, if you go to the routes file, we directly used view, but instead we are going to go through the controller
So earlier, if you go to the routes file, we directly used view, but instead we are going to go through the controller and return the view here. So here you just say return view, which view you would you like to return. That's dashboard. And let's also pass the data here that is roll. Okay? And instead of using the view, we will say get. So we are saying get dashboard. And this is where we have
So we are saying get dashboard. And this is where we have to mention the controller and its method. Since it's an invoke controller, we simply have to say roll controller plus, and that's it. So if all goes well when we hit the dashboard route, the rule controller class is called, which has a single invo function. So it'll get all the rules from the database
which has a single invo function. So it'll get all the rules from the database and it'll return the dashboard view with the rules data. But once again, that data has to be displayed here, right? So here is this single uh, roll div. So instead of hard coding, I will use the four each blade directive and say for each rolls as roll, we do this.
and say for each rolls as roll, we do this. Okay, so at the end of this diviv, I'm going to close the for each like so, so it's still hardcoded, but let's see if we are able to see the same div nine times, then it's working, go back refresh. And yes, we are able to see the same data nine times. Let's change that now. So scroll down to the title. Yeah, the company. And here you can use the curly braces
Let's change that now. So scroll down to the title. Yeah, the company. And here you can use the curly braces syntax to type in the variable, which is rule company. And right here will be the roll title. This is where you specify the role location. And here would be roll type. Finally, this is where we have the role details. That's it. Perfect.
Finally, this is where we have the role details. That's it. Perfect. We have fetched them from the database and we are able to display. Let's do more in the next episode.
