hey what's up guys welcome back to our blog and welcome to this blog this is the part 9 of our laravel for beginners tutorial series in the previous blog we have worked with the controller or post controllers we have added their outs so you've tried to open the web dot PHP in our routes folder so as you can see we have defined here route and then the resource and then the name is post and our controller which is post controller and then we also added or generated the post controller class so ii see here we have here all the functions that we need in order for us to work with our code or create read update and delete functionality of our project and in this blog we're going to continue working with our post controller and try to fetch data coming from our database and display it on our browser by the way guys if you try to open our post model so as you can see we haven't done much here and if you want to change the table name you can change it here and also you can change the primary key so it's a for example you want to change the table name here so for table name and then protected and of course the variable table equals the new name or the name of your table that you want in your database and of course you can also change the primary key because by default we have we are using the ID auto increment ID so you can also change that in here so protected say for example protected and then the primary key primary key it was ID there you go or you can change it into public so public primary key and then say for example you if you want to name it as item underscore so for example you want to name it as item underscore ID as the primary key so you can do that also by by default we're using the ID right and also if you want to change or if you don't want to include the timestamps that we have or that has been added automatically for us so time stamps remember that created at and updated at fields so we can set it to false to not include that in our database so you can do that here but I'm going to show you now how to display the data coming from our database into our page or post page because let's open our project in our breath so this is the home page right and if you try to go to the post section and the page is currently now blank but we can change it because currently it is loading the index function coming from our post controller which is now returning or it doesn't return anything for now so we will change this and we will return in the view for our index so we're just going to edit this so instead of returning empty it should return a view so it should be a file under post directory and then the name should be indexed at blade that PHP so there you go so we need to create that in our view so resources and then views and then we need to create a new folder here so it should be post and there should be a file here that says index that blade that PHP so there you go and in here let's just stay the layout coming from our layouts folder so layouts and app and of course we need to add the suction content for our content to be here especially the post so and section need to add end of the section here and of course we are going to place an h1 to have our post there and now let's try to preview our post page so as you can see we have the navigation bar and the h1 post and now let's try to get all the posts coming from our database so in our post controller instead of returning just the post index so we can return the actual post so we can use the post model because we're using the eloquent so it is basically just a object relational mapper and in here we can just say post double colon and then all so it will return all the records or all the posts coming from our database so I just comment this one out and also this and also this one so save and then review our code so refresh this page and we have some sort of error here so class HTTP controllers post not found so that's because we then click or use the app post here so we're just going to add that so use app backslash and then post and then semicolon and then save that and let's go back to our browser and refresh so there you go you can see that all the posts coming from our post table in our database has been loaded and it is an array so let's go back to our post page and try to iterate this array to actually display the title so instead of returning the just the post so we're just going to return the actual variable post so this will be returned view with and of course the name is post and our actual variable is post so it should be S or with s because this variable should contain all the posts coming from our database so save this and in our index that laid that PHP we're going to iterate all the posts coming from our database so let's just do that and in here we'll be adding a condition so at if and then count so first we're going to check if the post array has an element so pose is greater than 1 and then we're just going to have an else here and of course the end of our if statement if there's an element inside the post array so we're going to iterate that by using the for each and then post and individual posts there and we're just going to use the bootstrap element for a division so and if for now or end of our for each and of course this one is coming from our bootstrap implementation so class well and it should contain the h3 it will hold the title of our posts so post and then the title of course and let's try to preview this in our browser so save this one and refresh so there you go we have the post one in post too so we can add also decorated add variable or created add attribute in this so we can see that when it is created and also we can make this title as link so we can actually click on this and then redirect it to another page specifying the ID and then we can see all the details of that particular post so let's go back to our code and and in here let's add an element so small so this will hold the date that the post has been created so small and then it should be written on the double braces the post variable and of course decorated add attribute so save this and preview our page in a browser and refresh so as you can see we have the post one post over and of course the day that when it is created and let's make this post one or post two as a link so let's go back to our code editor and instead of just returning the h3 here we're going to add a link element so H or h3 and inside that is the link element so href and then double code so we should close this one here so a for our link and then of course it should be redirecting into the post page specifying the post ID so and then ID let's try that save this and let's go back to our browser and refresh so as you can see the post 1 and post o title has been converted into link and then we can actually click on this and we can see that is blank for now because we haven't configured yet this particular page so in the next blog we're going to discuss and continue working on with this how to view your information coming from the database so I hope you learned something from this blog and see you in the next blog of Thanks
hey what's up guys welcome back to our blog and welcome to this blog this is the part 9 of our laravel for beginners tutorial series in the previous blog we have worked with the controller or post controllers we have added their outs so you've tried to open the web dot PHP in our routes folder so as you can see we have defined here route and then the resource and then the name is post and our controller which is post controller and then we also added or generated the post controller class so ii see here we have here all the functions that we need in order for us to work with our code or create read update and delete functionality of our project and in this blog we're going to continue working with our post controller and try to fetch data coming from our database and display it on our browser by the way guys if you try to open our post model so as you can see we haven't done much here and if you want to change the table name you can change it here and also you can change the primary key so it's a for example you want to change the table name here so for table name and then protected and of course the variable table equals the new name or the name of your table that you want in your database and of course you can also change the primary key because by default we have we are using the ID auto increment ID so you can also change that in here so protected say for example protected and then the primary key primary key it was ID there you go or you can change it into public so public primary key and then say for example you if you want to name it as item underscore so for example you want to name it as item underscore ID as the primary key so you can do that also by by default we're using the ID right and also if you want to change or if you don't want to include the timestamps that we have or that has been added automatically for us so time stamps remember that created at and updated at fields so we can set it to false to not include that in our database so you can do that here but I'm going to show you now how to display the data coming from our database into our page or post page because let's open our project in our breath so this is the home page right and if you try to go to the post section and the page is currently now blank but we can change it because currently it is loading the index function coming from our post controller which is now returning or it doesn't return anything for now so we will change this and we will return in the view for our index so we're just going to edit this so instead of returning empty it should return a view so it should be a file under post directory and then the name should be indexed at blade that PHP so there you go so we need to create that in our view so resources and then views and then we need to create a new folder here so it should be post and there should be a file here that says index that blade that PHP so there you go and in here let's just stay the layout coming from our layouts folder so layouts and app and of course we need to add the suction content for our content to be here especially the post so and section need to add end of the section here and of course we are going to place an h1 to have our post there and now let's try to preview our post page so as you can see we have the navigation bar and the h1 post and now let's try to get all the posts coming from our database so in our post controller instead of returning just the post index so we can return the actual post so we can use the post model because we're using the eloquent so it is basically just a object relational mapper and in here we can just say post double colon and then all so it will return all the records or all the posts coming from our database so I just comment this one out and also this and also this one so save and then review our code so refresh this page and we have some sort of error here so class HTTP controllers post not found so that's because we then click or use the app post here so we're just going to add that so use app backslash and then post and then semicolon and then save that and let's go back to our browser and refresh so there you go you can see that all the posts coming from our post table in our database has been loaded and it is an array so let's go back to our post page and try to iterate this array to actually display the title so instead of returning the just the post so we're just going to return the actual variable post so this will be returned view with and of course the name is post and our actual variable is post so it should be S or with s because this variable should contain all the posts coming from our database so save this and in our index that laid that PHP we're going to iterate all the posts coming from our database so let's just do that and in here we'll be adding a condition so at if and then count so first we're going to check if the post array has an element so pose is greater than 1 and then we're just going to have an else here and of course the end of our if statement if there's an element inside the post array so we're going to iterate that by using the for each and then post and individual posts there and we're just going to use the bootstrap element for a division so and if for now or end of our for each and of course this one is coming from our bootstrap implementation so class well and it should contain the h3 it will hold the title of our posts so post and then the title of course and let's try to preview this in our browser so save this one and refresh so there you go we have the post one in post too so we can add also decorated add variable or created add attribute in this so we can see that when it is created and also we can make this title as link so we can actually click on this and then redirect it to another page specifying the ID and then we can see all the details of that particular post so let's go back to our code and and in here let's add an element so small so this will hold the date that the post has been created so small and then it should be written on the double braces the post variable and of course decorated add attribute so save this and preview our page in a browser and refresh so as you can see we have the post one post over and of course the day that when it is created and let's make this post one or post two as a link so let's go back to our code editor and instead of just returning the h3 here we're going to add a link element so H or h3 and inside that is the link element so href and then double code so we should close this one here so a for our link and then of course it should be redirecting into the post page specifying the post ID so and then ID let's try that save this and let's go back to our browser and refresh so as you can see the post 1 and post o title has been converted into link and then we can actually click on this and we can see that is blank for now because we haven't configured yet this particular page so in the next blog we're going to discuss and continue working on with this how to view your information coming from the database so I hope you learned something from this blog and see you in the next blog of Thanks
Post a Comment