Creating a survey in Laravel using the laravel-survey package

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
thank you i hope i got your name right um here's a quick demo of laravel survey uh so here in front of me i have the documentation open i have also created a brand new laravel project and um yeah i'll just go through the docs with you and set it up on this new project first step i want to do a composer require to install the package um again this is a fresh leveling circulation so i assume that you already have that i'm gonna run this composer require fine and that is going to install the package for us then i'm going to do what we have here i'm going to publish the migrations of the package that's done and then i'm just going to run php artisan migrate and if i do that you'll notice that these new tables are migrated answers entries questions sections and surveys and so now we would have these uh tables in our database and so what i have done is i also have php my admin here so i can kind of show you that you would notice that there are these new tables like surveys table and entries etc for now everything is empty so i'm just gonna go and continue with the setup so usage the first step that you need to take is you need to create a survey you can do this anywhere um you could even do it in your own controller but i'm just going to create a seed for this so i will go and create a php artisan make seed i'm going to call it survey cedar you can call it anything you want then i'm going to head over to my seeders and i'm gonna paste the code that i have from the demo survey needs to be imported so i'm gonna import that and one more thing i'm gonna do one small change that i want to make is i want to make it such that guests can also submit um entries to do that i'm gonna steal a bit of code from the documentation again you would notice that there is this section that talks about allowing guests to submit entries as well so i'm just going to copy this and put it here and to say settings let's do uh this okay so this is just optional you can add it or not as you wish um database either i just want to make a call to my so i'm gonna do a call survey reader and that is it now i would need to run php artisan um dvc that we think and that should create my survey yeah now if i head over to my phpmyadmin again and reload this page you will notice that we have one survey inside and it's called cad population survey okay so um next thing i'm gonna do is now that i have this survey i'm gonna copy this name um still nothing will show up here so what i'm gonna do is i'm gonna head over to my app and i'm just gonna create a new controller i'm gonna call it entries controller again you can call this anything you wish and in my entries controller it's gonna have a couple of functions again this is optional but i'm just going to have a create function we show the form or the survey and then i'm going to have a store function where we will store the submission in our database so for create basically what i need to do is i want to get my survey the one that i have in the database and to do that it's just a regular laravel model so i can just do a ver name is this cat population survey and i'm just gonna get a first one okay and then i'm gonna return a view um for this demo i'm just gonna use laravel's default welcome view and i'm just gonna pass the survey along um now i'm gonna head over to the welcome view and make some changes i'm just gonna make it empty for simplicity and let's just have a head let's just have a body and in my body i'm just gonna have a container and inside the container i'm gonna use the documentation again you would notice that we have this thing here it's basically a headlight for the survey that you can use and this is using bootstrap 4 but you can customize you can publish those views and customize them so that it works in whatever uh css framework that you choose so yeah i'm gonna add this here and one last thing i need to do i need to update my routes and that is going to be here i'm going to have a my controller is called entries controller and i will be using the create method here let's just shorten this and so if i go back to my create method again i'll just fetch this survey from my database and then i return the welcome view with the survey that i finished so if i reload the page you'd see that this already appears here looks pretty ugly so what i'm gonna do is i'm gonna head over to the bootstrap docs and i'm just gonna copy the cdn link for this demo and i'm just gonna paste it in the head limits um let's see yeah this should do the trick so if i reload here okay now the survey looks reasonable but what you would notice is that if you submit the press submit button it doesn't do anything because this is for this um inputs the survey actually needs to be wrapped in a form so to do that i am going to change this div to a form i'm gonna set the method to post this is optional you can use get method as well but i think your post method would be more appropriate for this use case and then i'm just going to have action for action i'm going to use a route called entries store we haven't created this so we're gonna do that soon because this is a post post uh forum i'm just gonna have a uh c surf uh token there as well and let's just create the routes now um let's create another one called store i'm going to change the verb to post and i'm going to give both of these routes a name name i'm going to say entries.create for the first one and store for the second one now what i will also do is i'm gonna head over to my entries controller and i'm gonna uh just dd jump and die my request uh to see what's inside okay so if i come back here one more time reload the page and press the submit button you'll notice that this is what we have in our submission q1 q2 and q3 doesn't appear the radio button wouldn't appear unless it has a value so um that's why you wouldn't see anything there so if i for example fill this up with let's say jfr submit you'll notice the values get filled up here okay um next step is we want to keep this entry in our database so what i'll do is again i'll head over to the documentation and i still does a snippet so there's this section validating submissions this will handle both validation and also storing the uh entry so first step is um let me import this entry s and we want to also get this service so we want to access the same survey and we use the survey to access the validation rule so to do that i could just copy and paste it here or i could just create a another function inside here let's just call it survey return and i'm just going to return the survey and so here i'm just gonna reuse this survey in fact i can just inline this and here i'm gonna keep a variable called survey and i'm gonna have this survey here so i'll get this array from the database i'll use it to validate my inputs the input that comes from the form and then finally i store it as an entry in my database now one mistake i've made here is that in the documentation is that here instead of passing the survey we actually need to pass the rules of the survey for validation so i'm gonna fix that here and now if i head back here and if i just go back if i press the submit button you'll notice that it will validate the input now the validation rules you may have noticed when we were creating the survey we actually passed the rules to the question so the first question is the only question right now that we are validating you want to make sure it's numeric and the minimum is zero um the other two questions are not validated so um you could add those validations but if you're using a seeder you definitely want to re-seed your database um yeah that's that and now if i head over here and let's say we submit something invalid uh before i submit i'll just head over to my database again you would notice that i have no entries just yet entries are empty press the submit button and we will have an entry and entry comes with some other uh relationship so an entry will have answers so that particular entry has these answers now um that's what we have here um now instead of returning an empty page i guess i'll just head over here and do a simple um return let's say revert return redirect back with a success message and we're just gonna say thank you for your submission and now in my view to display that i was i would just write a session success if sexually access is available you want to do a let's just do an alert alert success and here we just use the session success message i think this should do the job now if i just go back one more time let's say two java e and then you'll notice that this access message appears there as well and if i go back to my entries you will have you'll see that i have a second entry um yeah so this is the basic usage of the package um i think you can go through the docs briefly and see what are the options that you have there's quite a few different options that you have and you can customize how you use and what you use from this package you could for example make sure that people uh that only users um who are logged in can can make a submission or you can limit the number of submissions a user can have and things like that um yeah that's about it thanks again for checking out this package and um hope this was helpful cheers
Info
Channel: Matt Daneshvar
Views: 220
Rating: undefined out of 5
Keywords:
Id: BA7tc-2rcWg
Channel Id: undefined
Length: 12min 34sec (754 seconds)
Published: Tue Oct 12 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.