PHP Laravel Tutorial for Beginners 7 - Models and Migrations (Models & Database Migrations)


 hey what's going on guys welcome back to our blog and welcome to this blog this is the part 7 of our laravel tutorial series and in this blog we're going to discuss how to work with models and migrations but before going into models and database migrations let's try to add first the about and services links here in our navigation bar so let's go ahead and try to do that in our code editor and in here we can try to open up our app that blade dot PHP so let's go ahead under views and then layout app that blade that PHP and as you can see here we have here the links of the login and register so all we have to do now is just copy this and paste it here so we'll just add the links suffer example for our home let's just put slash there for our home and then copy that for our services so the link for our services is slash services and the same thing with our about page that there and change this link in to slash about and then let's just go ahead save this and let's refresh our page and there you go we can see that we have the Home Services and About section but I think we need to modify the layout because we want this home services and about to align with login and register so in order for us to do that let's go back to our code editor and in here instead of having one nav item so let's make it free for our home and then the same thing for our services close the list item tag and paste another one so save this and here refresh so as you can see now we have the home services about login and register links online together and if you try to click any of these links it should work for all of them so the about services home and also the login and register links one last thing that we will modify here is that we need to add some class to our content to properly position our text services and also the about Us section let's go back to our code editor and try to modify this and in here let's go to the main element and it has a class of B Y - 4 for now so let's add a container class for this and then say go back to our browser and refresh there you go you can see that the about or this elements are slightly indented from the left so about us and also with the services now that we have that covered let's try to go to the topic models and database migrations so the first thing that we're gonna need is the page B my admin if you are using one or Sam or any versions of the wampserver so the PHP myadmin is embedded into that and included on the installation process and I hope that you included that during the installation process of the one server or lamp or any version or any variation of the one server now we need page we may admin because we're going we're going to add a database here so all we have to do is click the new and then we're just gonna name our database ah speak a project there you go and then create now we don't want to create tables here because we want our laravel project to automatically create tables for us so let's go back to our code editor and make some changes and add controllers add borders for our project and in here let's try to open our terminal so I'll shift e so this will open up our integrated terminal for our atom code editor and from here we can issue commands or run commands artisan commands page B commands or any other commands that we need so the first thing that we're gonna need is to create a controller for our post so the key word for that is PHP artisan make and then Colin and then controller and of course the name of our controller which is post controller and then enter and we're just going to wait for a few seconds to allow the installations or to allow the generation of files so there you go controller created successfully and the next thing is we need to we need to add a controller or I mean a modal but for now let's try to check first if the controller has been generated for us here in our project so as you can see we have here the post controller the deck extends controllers and really use the illuminate coming from the laravel and added the namespace controllers so the next thing that we're going to generate is a modal so in order for us to do that just like the controller is to issue a command PHP artisan and then make and then instead of controller we're going to use a modal and try to generate a post model so as you can see we need to or I think the best way here is to use a singular form of post so here we are using post and then enter and before that we need to add /m because we're going to use the migrations so this will enable us to generate the table from our laravel project so - m and then enter and there you go model created successfully and created migration post table so as you can see if we try to go to our database view database and then migrations we have here the create post table a great post table migrations so as you can see it extends migration class and then it has the two function up and down and then we need to modify this method function up because currently this particular function create a post table with this attribute ID which is Auto incremented and also it includes the time stamps for created and updated add which will be automatically filled in when we try to run the migration command and since we're dealing with post we probably need to more algebra which are title and body so in order for us to do that is we need to add it here in our app function so table and then string and then of course we need to name our attributes such as title there you go and another one for our body so table and then we want to make sure that we use the medium text here because usually the body of a post tends to be longer than the title right so medium tags and then this will be the body of our post so save this and as you can see here if you try to go to migrations we also have the migration or create user stable migration for our users so with including all the attributes ID name email password and all other attributes included here now we will make few changes here before we run the migration and create our table so let's try to go to our ENB file to input our database details so we have here the database the username and the password so I think the username and password are already set but we have to update the database name which is in our case we have used the PK project there we go and then save this so before we run the migration command and generate the tables I'm going to show you a quick change that I made in providers app service provider dot PHP so under provider folder or providers directory and then app service provider dot PHP make sure to import the illuminate support passage and facade and then schema and inside this function book make sure to add this schema default string land which is 191 and if you have added that already we're good to go and we're now going to issue the PHP artisan migrate command so PHP artisan and then migrate and if we try to check our page with my admin and refresh our project core PK project so as you can see we have here all the tables generated coming from our project or PK project so we have here the post inside this post we have the ID which is the auto-generated ID and the title which is the land is 191 so we have set this in our code and the body and of course the time stamps decorated that and updated that so in the next tutorial I'm going to show you how to populate this table by using the tinker command and also we're going to work with fetching data in our database so I think that's all there is to it for this tutorial and I hope you have learned something and see you in the next tutorial thanks 

Post a Comment

Previous Post Next Post

Recent in Technology News