 Hello, this is Christian. In this video, we're going to create some databases for our program. This is the one that we've been working so far. This is the data set that we're going to add to our database. Right now, it's just coming from the RAM data, so I want to create it using my SQL and maybe SQLite. So traditionally, when you create databases for your program, you will go what's called the data-first approach. That means that you're going to go here. I'm going to use my SQL here, for example, and launch my database system. And then I will create my database here, all the structure of the tables first, and then I'll go to my program and do the connection and so forth. So that approach is still being used, of course. That's fine. But Laravel gives you the other way around. You can do what's called the code-first approach. And this is also very common in most full-fledged frameworks like Django and also in the .NET or ASP.NBC, that's that as well, using the entity framework. OK. So the first thing we need to do is just basically create a database first. And there are ways as well. We can create a database programmatically in Laravel. But for this one here, we're going to do it just this way. So the database name is called Laravel. And just create that. That's all we need to get started. The tables will be created, and all this data also will be created automatically or programmatically. OK. So now, back in the program here, now, you can see that inside the database folder here, there is a folder called Migrations. Inside the Migrations folder are four files provided by default when you create your project. Now, these are the dates that these migration files were created. You can see it back in 2014 and 2019 and so forth. Now, each of these files, as you can see, has a really structured naming convention. The year is the date that the file was created. And then the file name is, of course, underscore create users table, create password, resets table, and so forth. If you open that first one here, you will see that it's a class that extends the migration class. And then in here is two functions. One is called up. One is down. Down, as you can see, it just means that it's going to drop this database table if it exists. So if it creates everything new again, that's what you do. Up here, it just means you're going to create your table. And the table is called users, which is why the naming convention is also followed the same way. Create a users table. And then the table here is referred to the table object that allows you to create these data fields programmatically. And these will be converted to SQL command. And they'll be converted to actually creating the actual table. So here you can see that it's about two, four, six, seven files. Maybe it's going to add a few more. It depends where these are being called. So I'm expecting when I run the migration to run all these files, one table for users, one table for password resets, one for jobs, fail jobs, and one for personal access tokens. So let's go ahead and check the environment first, the .env here. It tells you that by default, this is the default database connection. This is the type I want to use my SQL. The host is correct. The port number, default, the database is called Laravel, which I named that Laravel as well. If you want to change it to something else, make sure it matches this database name. The user is root and password is none. This is just a local setup. So if you are using actual username and password, make sure they actually match here. The credential must be matched, otherwise it will fail. So that's all I need here for this one here. Now let's go through the command terminal here. And we are going to run these migrations here. So I'm going to leave this running at when a new terminal here go to my project. And then here is what I'm going to type all those commands. OK, if you want to run each of these individual file manually yourself, that's fine. You just have to type the whole name here. But I'm going to run everything in the migration to see to show you what's going on over here. So do that by issuing the artisan command called migrate. And that's it. Migrate here will run everything in the migrations folder. In this case, full file. Let's hit Enter. And if all goes well, you can see that it's migrating and has been migrated. So you can also check the status of this by typing the same command here and then just put colon status. It will tell you if there's any failure or if there's any file has not been run, it will tell you right here. As you can see, all these four files have been migrated, been processed. And then now, if you go to the database file or page. Now, if you refresh the page, you're going to see that Laravel is now populated with five tables, the one that we saw earlier. This one we didn't see, but it's been created automatically also by the migration class. So this will always be here. But the users table you can see here has been created, the structure, as you can see, has been created for you as well. With the ID as the primary key. And this is a foreign key. And then in so forth. Perfect. Even all the data types here, whether these are has entity for values, whether they are null or non-nullable, all set up here again from the code. Same thing for these guys over here as well. Now, we don't see all the actual data, but you will do that in the next phase, where we'll be seeding this table with actual data. So pretty cool. And then the advantage of this is that, of course, you can export the statements and send that to other users. Or if they have your code, they just run that automatically in the command terminal. And the other developers also have a copy of your database as well. Perfect. OK, so now let's undo this. There is a function called reset, fieldroom, migrate, and then reset. This will undo what we have just did. Roll everything back, delete all the tables, and then in so forth. OK, so if you do that, go back. You can see that all the table should have been removed, except migrations. You need that one there just to track your migration process in here. OK, so now let's go ahead and create a table for our projects. And we're going to fill this data programmatically to the table in the MySQL database here. All right? OK, now we're going to do everything here just in the command terminal until we do the actual coding. Let's go here. And so to do that, we're going to create a migration file that will look kind of similar to this here. So PHP, artisan, migrate. Now let's say make migration, OK, and then the name of the migration file. Of course, you're not going to put the date here. The date will be stamped automatically by learnable. So I'm going to call it, if you just call something like, for example, if you put users like that, OK, or users like this, if hit Enter. Now you're going to get a migration file called users. You can see here. But inside here, you can see that it has just a skeleton for us, right, just the up and down. Nothing is filled in here yet, which is OK if you want to do this way. Then you will have to go in and do the schema. Or you can copy this and put it in there and then make the changes if you want. Or you can, again, let artisan do a job for you, OK? So I'm going to go ahead and delete this again. Don't want that. And I'm just going to go up arrow, make a shortcut. And I'm going to call here the same function with the same name. If you follow this convention, says create, that means go ahead and create a schema using a table name projects, OK, and then follow by table, OK? So whatever is between create and table, what will be the name of your table? If you have multiple names, maybe project, ABC, and then table, then you're going to see a table name projects, ABC, OK? But ours just go project. So we'll call it projects. And then you hit Enter, you're going to get something very similar to what we saw here. If you open the follow up, you're going to see that it's pre-populated for you with some two default data set here. Of course, it's not what you want, but at least it has this thing already set up. We can easily go back and then just make some changes. All right, so now let's close this for now and let's fill this up. And so I'm going to open this for now. Open the inside our app. If you remember inside the data, not the app, sorry, inside the routes, the data files, the data file here. So we have four fields, ID, name, discussion, and URL. Now this ID, as you can see, you can set it so that it's auto increment. For my IDs, I'm going to make it so that it's not auto increment. I'm going to randomize this data myself, OK? So name, description, and URL. So over here, I'm going to change this to say, if you just leave the ID like this, that means it's going to be an auto increment and primary key. I want this to be a string. So to do that, you put here the string. But no, I should not string an integer. Then the name of that field is called ID. OK, if you want this to be unique, then you can call the unique function like this. And now it's unique. OK, so that's that one there. I don't need a timestamp. So I'm going to change this to, say, a string that contains the field called name, the project name. OK, and if you don't just leave it as is, then that's pretty standard. And then the next one here is going to be table string as well. It's called the description. And then one more. I'm going to duplicate this with that. And then this is a URL. And that is the structure for my table. If I run this, it's going to create that for me, all right? So let's say this and go to the terminal. And now we're going to migrate and run a migration. Of course, I don't want to run a migration like I did earlier. If you just do that, it's going to run everything again. I don't want that. I want only that file. So you can either type the whole file yourself, which is a lot of typing to do, or the easier way out is move this into a directory. So let's say that I want to move this into a directory called projects. OK, so move this file into that projects directory. Now it's in there. OK, so when I do the migration down here in the terminal, I can do something like this. I can say migrate only the file inside its path inside the database folder, migrations, migrations folder, then the projects folder. OK, everything in that project is on one file. So that's how I can avoid the other ones. So hit Enter. Hopefully we're one. And here we go. Everything looks good. Now let's go to the PHP Miami over here. Refresh. And there is our project folder. I mean, table. Click on the structure. And here they are. OK, so actually automatically set as a primary key, which is OK, but it is not all auto increment. That's going to let me do some randomization here. So far so good. Right, so the next thing is we're going to go ahead and see this table with the data from our site. Of course, I'm not going to do that from the site. I'm going to do everything again in the terminal. So now how do we do this? Inside here, the database folder is a seeders folder. And again, here there's a function or class called database seeder. Inside this database seeder has a run function. And again, this is default setup only. You can put your functions or your code in here to actually see your data. So if you run a command seed, make seed or something, then it will run all the data in here and seed your table. But instead of doing this one here, I'm going to create something similar for my project only. So I'm going to run a command called phprdysummake, then seed. I think that's what it is. Yeah, make seed. And then, no, that's not right. I don't remember where it is. Let's see. Make seed or is it seeder? Let's look at the command trial. So let's see. I don't remember. I think it's seed. Is it either make seed or db seed or something? So make seeder or create a seeder class. Maybe that's the one. Then there's a db seed is to seed the database. So you want to make the class. So it's the seeder class. OK, perfect. All right. So here again, let's go in the imperial console. And we're going to make a seed. So phprdysum and then make seeder. And then the file name is going to be called project seeder. OK, let's hit Enter. And hopefully, you will create that for us, as you can see here, as a class project file called project seeder. And it looks similar, just like the yellow one. Now we go ahead and put that inside here. What does it do? Well, this is what we create our database, right? How do you properly database in your program? Is through that seeder class. So I'm going to do the following. I'm going to go over here and copy this data set, actually the whole thing here, the for loop as well, and put it inside the seeder class right here. OK, let me do that first. And I'll move this constant out of it, because I cannot put constant inside the function. I'll move outside. This is returned here. And the class space, OK? So my constant is up here, available in global space. And then inside the function run, it's going to be where I put the create my database table. OK, so in here, let me collapse this, give some space. I'm not going to need this for now. What happens in here? OK, so in here, we do something kind of similar to just adding to the database. So what we do to do is we need to run a command code DB for the DB class called table. This function here will actually do the insertion for us. But now the table is called projects, OK? And we're going to run the insert command. Inserts command is an array of our data sets. OK, the DB if it's not available, make sure that you do the import. So control space bar is going to be coming from, always coming from the facade. Just hit the first one here. It says eliminate support for SAP DB. That's the one. So click that. And just make sure it says DB. And it should be imported at the very top, OK? So you need that to be used. All right, so now we insert into this table project. And what do we need to insert? Well, I need the ID field. So this will be ID field that will map to the, I don't want a DB ID here. I call it project data, right? That's this project data here. And then get the ID put in there, which is OK in this case, right? So I'm going to assign it with the data of ID. And then comma, the next field is the name. It's coming from the data name, OK? Next is description, your description. I'll just copy both of these right here. OK, URL, and then bam. All right, so those are the fields that will be added to the table projects when we run the cedar. OK, so let's say this. And I'm going to remove this one, so it's not too messy. And say that now go back to the terminal and run that cedar command. PHP artisan instead of make, it's called db.seed, OK? And if you want to see just the file name, then you would then give it the path, I think the path, the name or the name of the file called object cedar. If you don't include this, you should run the seed. As you can see, nothing's going to happen, OK? If I do that, right? It's going to run the database seed, which is this file here. Nothing in here. That's what happens, OK? I want to run the project cedar. So to do that, you will have to choose the class, so the dash class and then the name of the class is called project cedar. It enter, if all goes well. As you can see, very quickly, very fast. If I go back to the Myron over here, refresh the page. And you should see that now my projects have been populated with the data from the file. Pretty cool, huh? So there we go. That's how you do it. And if you want to keep adding more data to this, you can do so in the command terminal. Just update this, add more data to this file, and so forth here. So that is pretty much how you do it using MySQL. Very easy. Now, the neat thing about this, as you can see, is that it's quite powerful because what if I want to change to a different database? For example, I want to use SQLite. All you have to do, because this is the same stuff here, similar to the PDU, you just have to change the database type. So I'm here environment. Instead of saying MySQL, what if I want to use SQLite? And SQLite is very, very fast, pretty easy to do. I could do that by, you know, I just need this one here. I need the database name. And I want to turn all these off. So OK, come all these out. This thing is the palm sign instead. OK, so I'm going to look for an SQLite. And the database name will be the same. So the connection I do is just that. And the rest will just work just fine. All I need to do then is go to the root class here, the root folder, and create a file called larval.sqlite. It is just a text file, as you can see. But once you've done one of the migration, it's going to convert this to a SQLite 3 database. And actually, I should call it larval.sqlite. It has to match the name. OK, so you don't need a point number of things like that because it will map directly to this simple file. Now, in here, I'll do exactly the same as before. Let me clear this control here. Now, if you do have SQLite installed, make sure you do so you can access it. But to run a migration, are you going to go and just go back to the previous command here, right here. Migrate, right? The same file inside a project folder, run that command. And as you can see, the SQLite has now been updated. It's now a binary file. You can read any more. And it's an SQLite format version 3, OK? And then now, I'm going to seed this table. This table, I mean, yeah, the table projects. I mean, just to make sure it's working, I can open another terminal over here to show you. I do have a SQLite, so if you type a SQLite 3, it's going to take you right into the terminal here. If you don't have it, just go install a SQLite first, OK? And then if you type in dot databases, usually it will tell you your database. If it's not there, for whatever reason, but it should be there. I'm going to open it, though. I'm going to clear this, so type in shell, dot shell, and CL, let's look at it. Dot open, and the file is called larval.sqlite. You're going to type it correctly, though. If you type it wrong, then it's going to create a new database for you. That's all. It's not going to crash or anything. So I open that file for use. Now, if I type in dot tables, enter, you can see that I have a table called projects and one for migrations, which is very similar to what you saw earlier over here on the MySQLite migrations and projects, OK? Now, you want to see what's inside, and then you can do the select all from the projects folder. And you have to terminate with the semicolon. And there's nothing there, because you haven't added data yet. So now let's go back and let's see this table. So again, go back to the command, or get the db seed. This is the command right here, OK? And hit Enter. Now the data has been seeded. Now switch over to the SQLite and run the command again. And there it is. All the data has been added to our database SQLite and ready to use and a good go, OK? You can insert data here, too. For example, insert into your projects folder. Other values will be 555. And then the type of the app one description is like my first app. And then the URL is HTTP. Have one dot at one. Insert that, run the command select again. And there it is. It's been added to our database. So here we go. And that's that. Quit, quit that. And that's how you migrate data to a database, folks. You can use very easily. I can switch between the two here without changing anything in my code. And that is the beauty of these type of frameworks. So any questions, let me know. Otherwise, I will see you in the next video.