Laravel From Scratch [Part 7] - Forms & Saving Data

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey guys welcome back to laravel from scratch in this video I want to start to create the the form to actually add add a post through our application so if we go to slash posts slash create that should load the create function inside the post controller so let's go there and you can see there's nothing here so it's just loading a blank page what we want to do is load up a view so we'll say return and it's gonna be in the post folder and then it's gonna be in a template called create alright so let's go ahead and save that and we'll go to our views and then post folder oops and let's say new file create dot blade dot PHP alright and then I'm just gonna copy what we have in the index and see for the h1 we're just gonna say create post and then we're gonna get rid of everything else here okay so let's just save that make sure that loads up what do they what do they do oh I didn't put view all right now for forms you can use regular HTML laravel I think it was laravel for included a really nice helper for forms so that has been removed from I believe laravel 5.0 and on so it's it's available from laravel collective so that's what we're gonna use let's go yeah laravel collective and then we're gonna go to HTML in forms and then this is going to show us how to install it with composer and if we go down here we can use syntax like this we can say form open and then the URL that it's gonna submit to and this will just create an opening form tag this will be the closing form tag and then we can include certain components so if we look at right here we can include labels like this and then for the input so we can do form text and just pass in whatever we want to call that passwords and checkboxes select groups all that stuff anything that you can do with a form and then it also includes us cross site protection okay so it's going to include an S CSRF input that's going to protect us from people trying to submit from other places it's gonna make sure that the forms submitted from our application so let's go look up here now we're gonna use composer so all we have to do is say composer require and then the package name and then the version ok so let's just copy this and we'll go over here and open up our terminal let's clear this out we'll pay set in and that'll install it now we do need to add some things to the config usually when you install a pack a larval package you'll have to add it as a provider and and if it has any aliases you'll have to add those so let's just copy this one line right here and then go to our config folder and app dot PHP all right I'm gonna just close this terminal and this is where the providers are listed you'll see right here there's an array of providers we're just gonna go right down to the bottom here and paste that in alright and then we're gonna grab the two aliases which is form an HTML so the aliases are right here we're just gonna add it to the bottom here just like that and now we should be all set to use it so let's close up that config file and let's go back to the documentation and we're just gonna grab this right here and then let's go to our view which is our create dot blade and paste that in there now instead of using URL here I mean you could do that but we're going to use actions so we're gonna put an array and we're going to say action and set that to the controller function which is going to be posts controller and then we're gonna do at store okay store is the function we're submitting to and then we just need to we need to describe the method and this bracket should go over here okay so we'll put a comma here and then the method which is gonna be post alright and we're using bootstrap so I'm gonna put a div in here with the class class of form group and in here we're gonna put our labels so we can do our double double curly braces and do form double Col and label and in here this is going to be a label for title and then the actual text which will be title with an uppercase T and then for the input or the text input we will do form text and we're gonna pass in the name for this which will be title okay and then the second parameter is the value this is going to be a create form so we don't want a value so we'll have it just a blank string and then we can add attributes inside of this array so let's say we want to add a class on to this text field so we can say class and we want it to be form control which is a bootstrap class and then let's also add a placeholder so placeholder will just say title and that should give us the the input so let's save it and take a look there we go let's see why is that oh I forgot the T and form control there we go alright so there's our title and then we want a body so I'm going to copy the whole form group and let's change this and this and then instead of a text we want this to be text area okay I will change this to body and the placeholder will say body or body text okay let's check that out there we go and we're gonna make this into an editor later on but not not just yet now we're also gonna add file uploading but I'm not gonna get into that just yet we're gonna add that later on as well I want I just want to get a basic crud application up and running and then we'll move on to authentication and file uploading and all that stuff alright so let's put a submit button we're gonna go right here and we're gonna say form submit okay that's gonna be the value and then we have some attributes we can add I'm gonna add class and let's make that BTN and then BTN primary okay and that should do it so there's our form now when we submit it's gonna make a post request to store so let's go over here and go to our store now validation and laravel is very very simple what we all we have to do is say this validate and then we can pass in the request that's being passed into the store function and then an array of rules so we want the title to be required okay and that's all we have to do also the body okay so now if we go down here and let's just say return one two three so let's go back and we're gonna go back to our form and let's submit and you'll see that it's not letting us if I enter the stuff in here and submit now it does alright now we want some messaging here and what I'll do is go to our views folder and in the includes we're gonna create a file called messages dot blade dot PHP alright and then in here we basically have three things we want to check we want to check the errors array that's created when we when we fail validation we also want to check for session values so session success and then session error and those those are going to be flash messages that we can create at any point so let's say a pair if count and we want to look for errors if that's greater than zero if that's greater than zero then we want to loop through say for each and then we want to do errors this is actually an object so we want to do errors all as error all right and then inside here we're gonna put a div with the class of alert and alert danger and we just want to put error all right and then below it we're gonna check for session errors so let's say if session actually will do messaging for success messages first so this will be session success and then down here we're going to end if and then let's put in a div the class of alert and this will be a single message so we don't have to loop through so success all right and then here we can just say session and then the type which will be success all right we'll get I'll show you how to sit actually create these messages later and then this one's just gonna check for a session error okay we'll change this to danger and then this to error alright and you can use this messages file in all your projects so we just need to include it in our main layout so let's go in the container right above the right above the content here and we're just gonna do include so include and that's gonna be in the ink folder and then messages alright so let's go ahead and save that and go back and reload and submit and now we get our error messages if I put the title in it's just gonna tell us the body if I put that in it's gonna submit okay and it's as easy as that it's very very simple to add validation you can see validation pagination this stuff is really easy with laravel it's very elegant so let's go back to our post controller and finish our store function now remember we use tinker and we inserted our stuff into the database we can do the same exact thing here all right so let's say create post and this is gonna be post equals new post okay and we've the reason that we can use this is because we brought it in up here okay just like we used it here and the index and everywhere else we used it okay so new post and then we want to add our field so we'll say post title set that to we're gonna set it to request and then input and then title alright this will get whatever is submitted into the form we want to do the same thing for the body okay so this will be post body set this right here to body as well okay and then we want to save it so we'll just say pose save and then what we'll just redirect so to redirect we can just say return redirect very simple and we want to go to slash posts and then let's set a message with this a success message so all we have to do is say with and success and then post created okay I remember we created that messages file this is where we can actually set the message so let's save it and we should now be able to submit a post so let's go to post slash create and let's do post three we'll just say this is post three submit and there we go we get a post created message and it gets added now why is it in the middle let's go up to our index here well we ordered it by title so I actually don't want that what I want to do is order it want to order it by created at all right good so let's put a link to add post in our navbar so we'll go over to includes navbar and I want to put this on the other side on the right so under this ul let's put a ul and we'll give it a class of nav nav bar - nav and then nav bar - right okay and then we'll put in an Li okay and this is gonna say add or let's say create post and it's gonna go to slash post slash create okay so now we have a link to create our post so I guess we'll go ahead and implement the editor I wasn't gonna yet but we will so let's say laravel I think it's shoot was laravel - CK editor right here Eunice sharp laravel CK editor so we need to install this with composer so let's grab this right here and we'll go over and open up our terminal let's clear this out paste that and run it okay we'll also have to add to the app PHP file we need to add a provider just like we did with the other package the laravel collective so let's go to app dot PHP which is in the config folder just let's close this up I'll just make it smaller so we want to add it to the providers which is right here providers array and we're gonna just paste that in save it and we need to also publish our resources so we do that with this command here which is an artisan command ok so let's see that this finish okay so now it's clear that and let's paste that in that'll publish the assets and then the usage what we want to do is in our layout I'm gonna take this right here we're gonna do it this way oh no we're not we're gonna do it this way you can use jQuery if you want so let's copy this and we're gonna put this into into the layout file I'm just going to close these up to avoid confusion all right so our layout file which is in views layouts app blade and we're going to put this let's put it right above the ending body tag all right and what basically all we have to do now is give our text areas that we want to use the editor we want to add this article CK editor ID so let's save that and then go back to our in create where we have a text area and to add an ID we can just add in to this array right here so let's do ID and we're gonna set that to article - CK editor save that and let's go back and now we have an editor it's as easy as that so if I create another post let's say post 4 and we'll say this is post 4 and let's make 4 bold ok now this is going to get saved as HTML if we look at the source code you'll see there's HTML tags so let's submit alright and then if we look at post 4 you'll see it's not parsing the HTML so the reason for that is when you just use if we go to our show template which is what we're looking at if we're just using these double curly braces it's not going to parse the HTML so with the body what we want to do is use 1 curly brace and then 2 exclamation marks and that'll actually parse the HTML so let's save that and we'll go back and reload and now you can see that it's actually parsing it there's no paragraph tags showing and we have our bold text all right so it's as easy as that to implement an editor so in the next video what I want to do is make it so that we can edit and delete our posts
Info
Channel: Traversy Media
Views: 321,230
Rating: undefined out of 5
Keywords: laravel, laravel collective, laravel forms, laravel database, laravel 5.4
Id: -QapNzUE4V0
Channel Id: undefined
Length: 18min 53sec (1133 seconds)
Published: Sat Jun 03 2017
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.