what's going on guys welcome back to our blog and welcome to this blog so this is part number 10 of our laravel for beginners tutorials and in this blog we're going to cover how to display the information coming from our database and using the Luke went over em so we're going to display our post so basically from the previous tutorial we have worked with our controller post controller and our post model and of course we have laid out the basic layout of our index that laid that PHP where in which we already iterated the post coming from our database and also making our title as link so that we could click on that link and then be redirected to another page where in which you can see the full details of that particular post and also we have added the created at or written on date and then I have realized that we are we use the class well which is not included in in bootstrap anymore so we're going to use card for this class and in our app that blade that PHP file we're going to add the post page so we have here all the items are all the links from our navigation so home services and about so we're going to add another link or another item in our navigation bar so this should be all about the blog which contain all the posts and of course this should be all about posts so let's go back to our browser and try to preview and let's try to refresh the page so as you can see we now have the home services and of course the blog which is added recently so click on that and you can see we have here a deposed and I think we can take this further so instead as you can see this is somewhat ugly for now so let's try to modify this in our code editor so let's go back here in our index that played that PHP and yet as you can see we have here with the card right but instead of using a ch3 I think we're gonna use the UL for now so this is coming from our bootstrap integration on so so you L so inside this and it should have a class list group and then list group flush flush there you go and then of course inside here is the list item which has the title of our post so it should have a class list group item and in here we're going to place instead of using the h3 so we're going to or I think we could keep this h3 in this area so h3 so there you go instead of using HC only so we're facing the title and acclaim created add date inside a list item we should reiterate the list item so let's go back and try to add or remove this and add it here and so with the closing dev and closing l unordered list tag so there you go and then save that and refresh so as you can see we have now a slightly different version of our post so if you try to click on this post so we still have a blank page so we'll try to work on that for now and let's go back to our code editor so let's go to our post controller and as you can see we have here a function that says show that takes in a parameter ID so we can just edit this function and try to return a single post so return and then post and of course you can use a function here called fine and then specifying the ID and let's just save this and try to preview in our page so refresh and here you can see that this is the post one and this is the body of the post one so let's try to check and click on the post to so there you go the idea is to and the title is post own and now let's take this further and try to beautify this instead of just seeing the array of data so let's go back to our code editor and instead of returning basically just the post so we're going to return a view just like what we have in our index function so post and then we're going to return a certain view so this view will be a post or under the folder post or directory post with the show that blade that PHP file name and this should go with our post variable so and then the actual variable post and of course we can try to get this on our post as show file so let's try to create that on our post directory so let's try to create a new file here so this should be show that laid that PHP ok and then we're just going to copy the mark-up from this index that laid that PHP and paste it here and then we're going to extend the layouts as well and of course you can get rid of this because we're not dealing with we're not dealing with an area so we can just have or we can just use the post variable and then you can get the title from there and of course we can also get the other attributes so the post body and of course the created on attribute so we're going to add it here by placing it inside a small tag so this should be written on and of course double curly braces and then in our post variable with the created on attribute and I think we can add an HR here so HR horizontal rule and then of course we're going to add the body or the post body of the particular post so post and then the body of the post so let's try to save this and preview on our page so this is the current version all we see is an array of values coming from a particular post but if we try to refresh this now we can see the post title and the post body and of course the I think we misspelled instead of using created on I think it is created underscore AET or at and then save that and refresh so we have here all the information the post title post body and of course d created at date so post one and then post two and I think we could also add a button or back button here so let's do that so above the post title we'll just try to add a back button here so it should be a link that goes to the post and of course it should go it should say go back and it should have a class of a button so BTN and then BTN default so save and refresh so we have here a link that tells us to go back to the post or to the page where all the posts are displayed so I there you go post two and then post one and then let's go back so what if you want to sort by maybe title or ID ascending or descending so let's try that on our index function of our post controller so as you can see we are doing the post and then post modal and then double double colon and then all so we can actually sort by title or ID so whatever you want to sort by so say for example let's just comment this one out and try to copy that suppose we can say here order by and then the attribute that you want your records to be sorted by so title say for example in this case title and we can say descending or the des e and of course we need to say get and refresh here so as you can see now the post o is the first one to appear here instead of the post one and if you try to go as sort by ascending sort by title as ascending there you go refresh so the post one is the first one two we're here in our post page and actually you can also use the SQL commands or SQL statement and our controller to get all the post coming from our database so all you have to do is use the DB class so actually I'm going to show you that real quick because it it is very easy to get all the records coming from our database by using the DB class so we're just going to use DB and of course and here we're going to comment that one out for now and use this as our variable per a post so post and then equals the DB and of course the Select function and our SQL command which is select all from host and then let's just save this and refresh so we still have all the post coming from our database so post one and post all so I think that's all for this particular blog and in the next blog we're going to discuss forms and how to save data so I hope you learned something from this blog and see you in the next blog thanks
PHP Laravel Tutorial for Beginners 10 - Fetching Data Using Eloquent ORM Part 3
Tags:
Php Laravel Tutorial
Post a Comment