alright guys welcome back to our blog and welcome to this blog this is the part 8 of the Lara blog for beginners tutorial and in this blog we're going to discuss how to fetch data with Luke rent and also we're going to populate our database by use by using the tinker command and in order for us to do that let's open or integrate the terminal of our atom editor so in order for us to do that is we have to press shift alt and T and there you go we have opened our integrated terminal and in here we can type we can run the tinker commands from here and also we can populate our database by using the tinker so all we have to do now is type in PHP artisan and then tinker and as you can see we have here the brackets that let us know that we are inside the tinker program and from here we can directly work with our database and directly work with our models so for now let's bring in the post model so app and backslash and then post and then we can have a function in here so for example count and it will return zero which means we don't have yet a post in our post table or post model so now let's create a new instance of our post so in order for us to do that is define the variable dollar sign post and then equal sign and then our in model and then new app backslash and then post then semicolon and make sure you have the backslash instead of forward slash so this will be the backslash and enter so now we have a instance or an instance of our post so it is being saved in our memory and by that we can use that in order for us to move forward and add a post to our post table so now we can use the post instance and then set the title for our first post so let's just make it the title into post one and then semicolon there we go and then of course for our post body let's just do the same for our post body so this is this is the post body semicolon and then there you go this is the post body and then after that one we can save this save the information for our title and our body so post and then save and then enter and it returned it returns true which means that we have saved a certain post to our post table now if we try to check our database inside our PHP myadmin we can see here that we have a post and of course we have the first post which is which has the idea of one that I tell we have set this into post one and of course we have set the body content this is the post body and it also have the created at and updated at fields which is automatically populated for us so let's try to add another one and first let us create a new instance of our post model so new and then app backslash and then post this will create another instance and then we're going to set the title for this this is post two and the post body also we need to set a value for this this is the second post body there you go and we will say finally we will save this values so our database so it returns true again which means that we have now have to post in our post table in our database so refresh here and then you can see we have two posts or two records of our post table so post two and then this is the second post body with D created at and updated at fields now if you try to go back into our project and if you want to add a functionality here for our create read update and delete we're going to need to have different functions and different routes in our laravel project so let's go back to our code editor and try to edit our post controller let us open our post controller and as you can see here it is pretty much empty for now so in order for us to add the functionality for our create read update and delete I think we're gonna need functions or several functions from here so we need the first one is index so it is just a way of listing all our posts from our database another one is create and I'm just going to type it in here you don't need to follow this time so store to actually save our data into our database and then we have the function to add it and also we have a function that should be updating our data and of course we have also show and of course the last one too delete our record from our database so we could actually create this functions manually but we'll try to create this by using some commands so I think I haven't shown you this yet so I think I just have to delete this post controller for now and generate another one so I have deleted the post controller for now and I'm going to generate it again by using some commands so in order for us to do that let's run the command PHP artisan and then make and then controller and of course the name of our controller is post controller with s post controller and then we're just gonna add the resource keyword here and then we're just going to wait for a few seconds to allow the generation of our controller so there you go controller generate that are created successfully and if we try to open up the new post controller we can see that all the functions that we have listed earlier has been generated successfully so we have here the function index so display a listing of the resource and some of the functions here don't need or doesn't need the parameters but there are also functions that need the parameter so say for example this one we have D create so show the form for creating a new resource and then the store has this parameter request and we're going to define some of the logic later on in this function and also we have the show so it has the ID that will identify the record that we want to show here and also we have the edit the same thing it has the ID parameter and also we have the update that requires two parameters the request and of course the ID of the resource and of course the destroy function that will enable us to delete based on the ID of the resource so now that we have laid out all these functions in our controller we need to have a route for each functions right so we're going to add or we're going to go to the web page P of our routes and as you can see we have here the route that we have currently and if you try to go to our command or our terminal let's try to list our routes for now so PHP artisan and then route and then list so these are the currently generated routes for our project so as you can see we have here drag in the home the password the register and all other functions or all other routes that we have set and now let's try to add a route for all our functions because it is a kind of pain in the ass if you try to generate individual routes for our functions right so let's try to generate route for all our resource in our post controller so route and then resource and of course the name will be post and of course the name of our controller which is post controller and then we're going to save this for now save and then let's go back to our terminal and try to run another one PHP artisan and then route and then list so let's try to find our resource or generate that routes for our post controller so as you can see we have here all the the routes related to our post controller so we have here the post store post index post create post show and of course post update and all other functions or all other routes that we that we need in our post controller and I think that's it for this particular blog and in the next blog we're going to continue working on with our fetching data with Lucan so we're working with the create read update and delete so see you in the next blog
alright guys welcome back to our blog and welcome to this blog this is the part 8 of the Lara blog for beginners tutorial and in this blog we're going to discuss how to fetch data with Luke rent and also we're going to populate our database by use by using the tinker command and in order for us to do that let's open or integrate the terminal of our atom editor so in order for us to do that is we have to press shift alt and T and there you go we have opened our integrated terminal and in here we can type we can run the tinker commands from here and also we can populate our database by using the tinker so all we have to do now is type in PHP artisan and then tinker and as you can see we have here the brackets that let us know that we are inside the tinker program and from here we can directly work with our database and directly work with our models so for now let's bring in the post model so app and backslash and then post and then we can have a function in here so for example count and it will return zero which means we don't have yet a post in our post table or post model so now let's create a new instance of our post so in order for us to do that is define the variable dollar sign post and then equal sign and then our in model and then new app backslash and then post then semicolon and make sure you have the backslash instead of forward slash so this will be the backslash and enter so now we have a instance or an instance of our post so it is being saved in our memory and by that we can use that in order for us to move forward and add a post to our post table so now we can use the post instance and then set the title for our first post so let's just make it the title into post one and then semicolon there we go and then of course for our post body let's just do the same for our post body so this is this is the post body semicolon and then there you go this is the post body and then after that one we can save this save the information for our title and our body so post and then save and then enter and it returned it returns true which means that we have saved a certain post to our post table now if we try to check our database inside our PHP myadmin we can see here that we have a post and of course we have the first post which is which has the idea of one that I tell we have set this into post one and of course we have set the body content this is the post body and it also have the created at and updated at fields which is automatically populated for us so let's try to add another one and first let us create a new instance of our post model so new and then app backslash and then post this will create another instance and then we're going to set the title for this this is post two and the post body also we need to set a value for this this is the second post body there you go and we will say finally we will save this values so our database so it returns true again which means that we have now have to post in our post table in our database so refresh here and then you can see we have two posts or two records of our post table so post two and then this is the second post body with D created at and updated at fields now if you try to go back into our project and if you want to add a functionality here for our create read update and delete we're going to need to have different functions and different routes in our laravel project so let's go back to our code editor and try to edit our post controller let us open our post controller and as you can see here it is pretty much empty for now so in order for us to add the functionality for our create read update and delete I think we're gonna need functions or several functions from here so we need the first one is index so it is just a way of listing all our posts from our database another one is create and I'm just going to type it in here you don't need to follow this time so store to actually save our data into our database and then we have the function to add it and also we have a function that should be updating our data and of course we have also show and of course the last one too delete our record from our database so we could actually create this functions manually but we'll try to create this by using some commands so I think I haven't shown you this yet so I think I just have to delete this post controller for now and generate another one so I have deleted the post controller for now and I'm going to generate it again by using some commands so in order for us to do that let's run the command PHP artisan and then make and then controller and of course the name of our controller is post controller with s post controller and then we're just gonna add the resource keyword here and then we're just going to wait for a few seconds to allow the generation of our controller so there you go controller generate that are created successfully and if we try to open up the new post controller we can see that all the functions that we have listed earlier has been generated successfully so we have here the function index so display a listing of the resource and some of the functions here don't need or doesn't need the parameters but there are also functions that need the parameter so say for example this one we have D create so show the form for creating a new resource and then the store has this parameter request and we're going to define some of the logic later on in this function and also we have the show so it has the ID that will identify the record that we want to show here and also we have the edit the same thing it has the ID parameter and also we have the update that requires two parameters the request and of course the ID of the resource and of course the destroy function that will enable us to delete based on the ID of the resource so now that we have laid out all these functions in our controller we need to have a route for each functions right so we're going to add or we're going to go to the web page P of our routes and as you can see we have here the route that we have currently and if you try to go to our command or our terminal let's try to list our routes for now so PHP artisan and then route and then list so these are the currently generated routes for our project so as you can see we have here drag in the home the password the register and all other functions or all other routes that we have set and now let's try to add a route for all our functions because it is a kind of pain in the ass if you try to generate individual routes for our functions right so let's try to generate route for all our resource in our post controller so route and then resource and of course the name will be post and of course the name of our controller which is post controller and then we're going to save this for now save and then let's go back to our terminal and try to run another one PHP artisan and then route and then list so let's try to find our resource or generate that routes for our post controller so as you can see we have here all the the routes related to our post controller so we have here the post store post index post create post show and of course post update and all other functions or all other routes that we that we need in our post controller and I think that's it for this particular blog and in the next blog we're going to continue working on with our fetching data with Lucan so we're working with the create read update and delete so see you in the next blog
Post a Comment