Laravel 6 Beginner - e18 - Final Project Part 2 (Questionnaire/Survey App)

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
[Music] let's pick right back up where we left off so now we can create a questionnaire however we need to have this concept of questions inside a questionnaire and each question is going to have any given number of answers so we can represent that as a questions table and we're going to need a model for that with the migration as well as an answer table again with a model and a migration let's create both of those at the same time back in my terminal I'll go ahead and run PHP artisan make model for question and I need a migration let's run the same thing for an answer all right so let's get that set up let's open up the create questions table let's get started with this very simple a question needs to belong to a questionnaire so we could say okay this table needs to have an unsigned big integer or questionnaire ID as well as we need to have the actual question let's just call it question so a string for a question okay so then at the same token we need to go ahead and open up the answers and we need to have the same sort of relationship an answer belongs to a question so we need to have a table unsigned big integer for the question ID and then we'll need a string which will represent the actual answer we'll just call that answer okay so let's go ahead and open up the model so we've got the question model right here as always let's go ahead and turn guarded to an empty array and what kind of relationships does a question have we know that a question belongs to a questionnaire so let's do that now so a public function questionnaire return this belongs to questionnaire perfect we also know that a question has many answers let's go ahead and fill that up right now so we'll say answers return this has many answer plus all right so now let's jump into the answer and let's do the exact same thing guard it equal to an empty array and then we'll have a relationship here because an answer belongs to a question josè public function question returned this belongs to a question class and there we go okay the final thing of the puzzle is that we need to make sure that a questionnaire knows about this collection of questions so let's jump into the questionnaires model let's add a new relationship here for questions so a questionnaire has many questions so returned this has many questions class and there we go alright let's review through all of these relationships because I know I went kind of fast but I think they all should make sense so a questionnaire is simply a collection of questions right so we have this relationship here of questions so a questionnaire has many questions ok in the inverse a question belongs to a questionnaire right we know that and in turn a question itself will have many answers so it has many answers and this is the relationship now if we jump into the answer itself an answer simply belongs to a question so that's the flow that we have between one to the other now in order for us to make this work we're going to need a form we know that we're gonna have to create some sort of create deal and we're also gonna need a controller so let's call it the question controller we've got the questionnaire controller let's go ahead and create a question controller PHP artisan make controller question controller all right so looking at our UI once we create a questionnaire let's go ahead and jump into one of my questionnaires here I should have at least one and there it is so cool title I should be able to have a button here that says add a question so let's do that now in my questionnaire that show right down here let's add a link and what do we have here in terms of URL what we have here is a nested relationship a questionnaires has questions so that's the nested relationship so we will start with questionnaires / then I need the ID of the questionnaire that will add that in questionnaire ID and then slash questions and then slash creates right because we're trying to create one of those all right we obviously don't have this just yet so let's go ahead and create it let's go to the web that PHP file and down here let's add a new get route for slash questionnaires slash then the questionnaire so we can actually use route model binding slash questions slash create this will hit the question controller at the create method alright so we have that working now let's go ahead and wrap this up with some classes will say BTN and BTN dark there we go and then in here let's go ahead and say add new question all right let's give this a go and there it is so when I add new question of course now it says well you don't have a create method inside your question controller okay close this up let's go to the question controller and let's add that create method I will need a view so let's go ahead and set that up real quick we'll go to the question dot create view but now I do need my questionnaire and I can use route model binding so right up here I can say app questionnaire and then I can just save that as questionnaire and I do need to pass that through it will say compact questionnaire questionnaire now if I want to simplify this up here we can input it up there at the top so let's go ahead and do that like so so now we have an import statement over on the top alright let's go ahead and clean this up then we are good to go let's go ahead and create this create method I will actually use the same exact create method because we're gonna basically rewrite this a little bit so let's save this as let's change the directory into the question directory like so so now we've got a create method inside the question now this is going to say create new question and so for the action on this form we're gonna go to slash questionnaires slash the ID of this questionnaire again we can grab that with questionnaire ID slash questions okay that is going to be a post route now before I forget let me go ahead and create that route so again that'll be a post request and we can get rid of this and that is going to hit the store method okay that is good to go obviously my form is going to change quite a bit and this next part here is probably one of the most asked questions that I always get and it is how do you create through relationships basically this form needs to create a question and a couple of different answers all at once so how do I make that happen well to give you the short answer is just by using a race but there's a little bit more to it than that it's all about the naming convention and how I handle everything and you'll see how clean it ends up being in my controller so let's take a look at how to do that right now first of all I need my question itself so we'll put the question right up here so instead of title let's go ahead and replace all of them with question so my label will say question okay now here's the tricky bit for the name of my input you need to name it something inside an array that you can reuse later in my case remember that I am going to save it to my questions table let me actually open that up the questions table that I'm creating it has a field of question so I need a double nested of question because I need to be able to have it as an array of question equals whatever value I have now bear with me I know that this is a little bit difficult to understand just with words but by the end of this video this will be very clear so stick with me so for the name will say question and then in array syntax let's say question again and I'll show you why I'm doing that in just a couple of minutes and then let's go ahead and change our placeholder matter of fact let's bump this down into a new line just it's a little easier and then we'll say enter question and then question help let's go ahead and change this text to something like ask simple and to the point questions for best results okay then the air that we are going to be looking for I know it's a little bit weird but it needs to be question dot question now I know that's a little bit strange but it's because of the nesting that we're doing up here we're gonna end up with nested like so okay now in terms of this part here I will actually cut this out I need to change this altogether this will still be a form group so let's add that back in so say a div of form group but this is going to be a fieldset and so inside of my field set that's where I'm going to have a legend and this one will be called choices so these are the different choices and then inside of here let's divide each one with a dip this is just to create some separation inside of that and to that we'll paste what will be the first of the different choices we're going to have let's go ahead and have a small here let me bring it from here one of these let's copy that let's paste it in here just to keep it consistent so this is gonna be an ID of choices help let me change that up this is choices help we're gonna reference choices help through this right here this aria described by that's what we're gonna reference this up here let's go ahead and change this over to give choices that give you the most insight into your question so again this right here it's gonna be the first of what I'm gonna do for choices in the ideal world of course with JavaScript or something like that we would make this dynamic but because we are focused on whatever I'm not gonna interject with any JavaScript for this we're gonna do it a hundred percent with PHP so let's go ahead create foreign group for let's change all these purposes for answer one and this won't be purpose and this is going to be choice one and so again my naming convention here is very important and it's not going to be answer one it's going to be answers in plural then we'll do the array and then we'll do answer singular and again this answer here has to do with the fact that it might create answers I am expecting it to be under answer and I'll show you again why I'm doing this this way in just a couple of seconds so in terms of described by is actually going to be choice help like so and let's also go ahead and break this up into a different line to it easier to see so we'll say enter choice one and we can actually get rid of this all together and now this one is going to be answers dot zero because this is the very first error so in that error bag of answers I want you to grab the first one which just has an ID of zero and then give me the error for the actual answer so that's how that one would work okay so let's go through this one more time I'm creating a field set this is just simply so that I can group together a couple of different inputs that basically have the same functionality we're creating a legend which is the equivalent of a label for an input field and then we'll have a small and then again the reason why I'm adding this is so that I can describe it by only once and I only have to have this text once inside of here I just have a simple label and input field and the naming convention of this is just answers so that way they get collected into a key of answers this will be an array as notated by this and inside of that I will have answer singular and then inside of that it's whatever the user actually types into this input field okay in terms of grabbing my errors because we are nested in this particular way so the first one is going to be found under answers dot zero because it's a zero based index so that's what this is and then the second one will be found under answers that one so we'll go ahead and change this over to one and two and three and so on and so forth okay so now all I need to do is go ahead and repeat this another time and let's change all of my once for answer for two and then I need to change this to one okay and then I can copy it again and let's change all of my twos to 3s and then I can change this one to two and then finally we'll do it one more time and I'll change all of my threes to four and we'll go ahead and change this to dot three and then at the end here let's go ahead and change this to add question alright let's see what this looks like in the browser go ahead and hit refresh and there we go alright so we've got a question it's got the question here so here would be something like what should we do right and then here we'll have stay the same change do something and do else right something like that basically they're gonna be given four choices then they're gonna add question and then at that point we should have one entry inside my questions table and then for entries that belong to this question inside my answers table alright so what does the controller look like for that let's go back here let's open up the question controller and let's start to work on that store method you know the very first thing about the store method is that we can actually continue to use route model binding I'm going to need my questionnaire because that's what everything is sort of based off of but first and foremost let me go ahead and die and dump my request I want to show you this whole structure that I did with the form I think it's gonna become clear once I show you what we're going to receive in the backend let's go ahead and dine dump the requests and let's just grab all of its values all of its keys so I'll go ahead and add question and there we go so this is what I'm getting back I'm getting an array under question and inside that array I have the exact thing that I need I need that question what should we do and that's great because I can grab that and send it straight through through my create method using a relationship and then I've got this key of answers and inside of there I've got four answers and each one is a key value pair which is exactly what I need I need the key to be answered that way my database knows what column we're talking about and then I need the value pair it up to it so this is exactly what I need and this is the most important part of the setup of trying to create through relationships and trying to keep your code as clean as possible if you create your forints properly you basically don't need to do anything in the backend to repurpose these arrays you can create any array shape that you need for the back end right in the front end it's very simple to do you just need to pay attention to how we are doing it okay so let's go had and use what we know from this to create what we need so first of all let's talk about how to validate in a nested relationship okay so let's start off by that let's go ahead and save it to data and let's go ahead and do request validate at first glance validating a nested relationship might not be super clear but once you understand that we can use dot syntax for array notation you may think well that's easy because that should just be question dot question and that needs to be required where does question that question come from let's go back to that create that blade right here let's go back up here to where we actually grabbed the question look at the name is just simply question that question that's it that's all it is okay what about the next one though the next one might be a little bit trickier because the next one is answers but then it's an array how do I get anything inside that array well we can use an asterisk so we can say answers dot asterisks dot answer an answer comes from answer right here so answers array answer becomes answers dot asterisks to represent the entire wild-card array dot answer and that's it that's good enough for validation and I'll prove it to you right now I'll go ahead I will keep this open here in this tab just simply because I want to show you this from time to time and I don't want to have to keep dumping this out so let me go ahead and create a new one here a new tab and let me go back here to this one there we go and let's add a new question and again I'm just keeping this here for reference that's all it is and that all I will do here is just hit this button and look at validation everything is working for us every single field is good to go so we can type something in and now if I ask question you see that the validation error does go away however I don't have my old data but that's actually a pretty easy fix where we fix that right now right inside my input let's add a value in the value is gonna be old we already kind of talked about old earlier in this series so I don't really need to explain what it does but we simply need to go in there and grab the question dot question all right does that work let's find out we'll put SDF and there we go now I do have my value let's go ahead and make the same change to my four choices I'll copy this one let's bring it down here and let's add it right here now this one is gonna be this right here so answers dot zero which is zero base index so basically number one dot answer all right I'll copy that I'll bring it in here let's change that to one let's do it one more time and then we'll do a one final time right here like so and so now I'll do STF STF STF I'll leave this from blank so that it fails validation but notice that we still have the old values for each of these and then of course at that point if I actually typed in choice for then that goes through into my controller which doesn't do anything right now so we get a blank page awesome let's move on up next in the controller the first thing we need to create is the question right we need to create this question in the questions table now we already took care of all the relationships so this is actually quite simple we can save it to a question variable and we can use the relationship that we already have through the questionnaire remember that the questionnaire is being passed through route Mar binding so right here we can say okay questions create and now this is where the magic really happens let me down dump this just right I can show you I am gonna die and dump the validated data not the request data the validated data at that point okay let me show you what that would look like that question so now we've got this question and that's all I need and that has what I need okay so all I need to pass in here is data question and then presumably at this point I will have a question model so with that question model I can continue to chain and do the same thing with the answers so I can say question I know you have a relationship of answers so on that relationship I want you to create but I can't just do create I need to do creates many because we are creating more than one at the same time and to this one I would pass in something very similar data answers right and that's this array right here which contains an array and that's why we're going to use they create many and at the end of this I just need to redirect back somewhere let's figure out what we should redirect I think that we should just go back to the questionnaire will say questionnaire slash and then I need the ID questionnaire ID and let's clean up our controller let's go over our controller one more time so we've got our data it's being validated and then we're taking care of our nested values by using this dot notation and I already showed you where I was so I'm not going back to the create method I'm doing it for both in this particular case this is an array so I'm just using a wild card I need to make sure that they're all required every single value in there that has an answer is required that's all it is then using my questionnaire which I am grabbing through the top I am using the questions relationship that I created at the beginning of this episode and then we're gonna create on that a new question that is gonna return my question model which I'm saving to this variable and using that variable I am tapping into yet another relationship which is the answers relationship and then I'm going to create many and then I'm using the data of answers again just the way that I took care of my front end with the form then I'm simply going to redirect back to the questionnaire where I'm presumably going to display this properly to give this a go we do need to migrate our database so let's do that now PHP artisan migrate that migrates our table sequel pro now we should have answers which is empty and we should have a question let's this ago let's see if it just works I'm gonna refresh this I'll hit continue and it says not found and that's okay there we go so we've got a question and it's got the questionnaire ID and then we should have four answers and there are my four answers with my question ID Association already done I hope you see how powerful this whole thing is and how simple it is once you actually understand how creating the form in the correct location with the right names makes all of the difference in the world now for this lesson we're running a little bit long and I know there's a lot of content in here so let's break it up into another part where we actually take care of the front end because right now my view is not even showing but we'll take care of that in part 3 thanks for watching and as always don't forget to subscribe and I'll see you in part 3
Info
Channel: Coder's Tape
Views: 19,974
Rating: undefined out of 5
Keywords: laravel, laravel 6 auth, laravel authorization, laravel 6 auth tutorial, laravel front end, laravel frontend template, laravel frontend scaffolding, twitter bootstrap, bootstrap laravel 6, laravel mix, laravel mix tutorial, laravel login, laravel beginner project, laravel survey, laravel project from scratch, laravel has many, laravel hasmany, laravel belongsto, laravel belongs to, laravel saving to multiple tables, laravel 6, laravel 6 full project
Id: _SyG3HMv48k
Channel Id: undefined
Length: 22min 57sec (1377 seconds)
Published: Mon Dec 30 2019
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.