PHP Laravel Tutorial for Beginners 11 - Forms and Saving Data Part 1

 

hey what's going on guys welcome back to our blog and welcome to this particular blog this is the part number 11 of our laravel tutorial for beginners series and in this blog we're going to discuss forms and how to save data and if you try to go to the correct or PK project that LOC create and then you can see that we have a 404 not found because it is loading the create function of our post controller and as you can see we don't have any logic from here yet so all we have to do now is modify this function and to actually return a view so this should return a view coming from our post directory and create template and let's go ahead and and create that inside our post directory so new file and of course the name of our template is create blade great blade that PHP and then we're just going to copy the mark-up from this index that laid that PHP and paste it here and it should say create post instead of just saying post and then we're going to get rid of all this markup here because we're going to use a form for us to save information coming from a form into our database and for now let us save this and try to preview in our browser so refresh so as you can see we still have the peak a project that LOC create and we're getting a 404 not found that's because I think we need to specify first the post here before they create so post and then create and there you see the create post h1 that we have added in or create that plate that page we find so let's try to add a form into this page and we're going to use laravel collective for that so they have a some sort of a template for forms and we just need to install and add some dependencies into our project so we can see here HTML so laravel collective and we can also open up their website laravel collective and here we can see the different versions so for example 6.0 so installation for the lateral collective is here so composer require laravel collective / HTML so we're going to do that in our code editor Adam and we're going to open up the integrate the terminal from more code editor Adam and then paste just the composer required laravel collective so there you go and then enter we're just going to wait for a few seconds and now the installation is complete let's go back to the website and in here we have a guide on how to open up a form inside inside our template so we have here opening our form and then we can just copy and paste this one and paste it in our create that blade PHP so below here let's just paste this code and try to change this in because this is how the default on how to open up a form by specifying the URL and where to submit the data coming from this form but we can actually change this so in here we're going to specify the action instead of the URL so the action for this one is post controller and then add symbol and of course the store function inside our post controller class and we can also specify the method from here so it should be method and of course method with single code and the equal and greater than sign and here we specify the method which is post also in here we can specify a division with a class form group so this is actually coming from a bootstrap so we can also add a form label here so this will be form and then label and specifying that this is a label for our title and the actual label of our title field and of course we can also specify here the actual input so form and then tax we can specify the name of our input which is title and then the value so this is a create form so we should not specify a default value and of course we can also specify classes here or additional attributes so for classes or for classes for this particular input and this should have a class form control and of course a placeholder for this particular input so equal sign and then greater than and the actual placeholder which is title so let's try to save this for now and let's go back to our browser to preview and try to see here we have the actual label an input field which has the placeholder title but I think we don't have yet the correct classes for our input so it looks ugly for now so let's go back to our code editor and try to see so we have here instead of class we have specified classes so save that and try to refresh and there you go we have a much much better input for our title and then we can also the same or we can also do the same for our body so we're just going to copy this and instead of title we're going to say body and of course this is for body also and by d2 and so with this placeholder and of course instead of tax or using tax it should be taxed area there you go and save that can refresh our page so we have a title and a body now and the last part is to add a submit button for our form so before the closing tag of our form let's add the button submit which is form and then submit and of course we need to specify some values here also so submit and then of course the class for our button which has class and then we can specify the BTN class with the N primary to signify that it is a button so save and refresh so now we have a complete form with all the fields and of course the submit button and if you try to submit now so it redirect to another page and basically we're not doing anything for now because we should modify also the store function of our post controller so let's go back first or code editor and edit our post controller so inside here I'm just going to close this real quick and in here we need to add some logic to actually save our data coming from the forum we can also add the validation from this function also so we can specify this and then the function validate and of course we need to have the request here and also the area of the rules which as for our title it should be required okay so it should be required and also the body tag or the body or the body of our post should be also required so and then we're just going to return a number for now so 143 something like that save this and go back to our browser refresh and then try to submit so it is not allowing us to submit or any data because air is empty for now so but we don't have a message that it it is submitting an empty information but if we try to have an information here and then submit so as you can see we now have the return information 1 for 3 so I think this blog is rather long so we're going to continue working on with this forms and how to save data in the next blog and I hope you learned something from this blog and see you in the next blog thanks 

Post a Comment

Previous Post Next Post

Recent in Technology News