Laravel 6 Tutorial for Beginners #21 - Arrays & JSON

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
all rather my friends so at the minute if we take a look at our data in the pizzas table we are capturing 3 extra properties the type the base and the name but I want to add a fourth one and that is going to be toppings so any extra toppings the user wants to add on now when I start talking about toppings I'm thinking there's going to be several values a list of values and to me that screams ok well an array of data now in my sequel we can't store an array as a data type but what we can store is a JSON string which can represent an array so what I'm going to do is go to our migration file for the pizzas table first of all and I'm going to create a new column and this is gonna be of type jason and it's gonna be called toppings so let's add that on it's a table first of all and then it's going to be jason and then in here toppings now by the way if you want to be able to store jason in your database table and you're using zamp and MariaDB you need a modern version of MariaDB for this to work in laravel so make sure that you do install the latest version from the samp website otherwise you will get errors when you do this so anyway now that we've done that what I'm gonna do is roll back all those migrations and yes we will lose our data but I'm not bothered because we're not in production and then I'm going to migrate again so in fact let's just do this all in one swoop so we'll say PHP Arty's on migrate oh make sure we save this file so migrate and then I'm gonna say refresh so remember refresh takes it back to the beginning it puts everything in a down state and then runs all the migrations again so press ENTER and hopefully it's going to run all that so if I now come over here and refresh then we don't see any data anymore but we do see this extra column right here so we have that column now we need to capture the data on the form right here so what I'm going to do is use check boxes to do that so let me come to the create view over here this is where it is and I'm going to do for this a field set and inside this field set I'm going to do in a label first of all and that is going to be doesn't need a fall this one that's gonna be extra toppings all right so we need to now add a series of input boxes so I'm gonna say input the type is going to be check box and the name is gonna be equal to toppings right because remember we use the name attribute right here when we get the data inside the controller so we'll need that alright so back over here now let's say the value of this one is going to be mushrooms so if a user checks this then toppings is going to contain mushrooms and I'm also going to put some text next to this so I'll say mushrooms so they can see what they're actually putting a tick in so BR and let's do another one so what I'm going to do in fact is just copy this a couple of times and this one right here is still gonna be check box it's still toppings each one of these is toppings because at the end of the day they are all toppings we don't need separate names for them they're all going to go ultimately inside the toppings column but the value for each one is going to change so instead of mushrooms will do peppers instead of mushrooms down here we will do garlic and then let's do olives and we also need to update these things so peppers and garlic and then finally olives okay so before we do anything let me now come over here to the pizza controller I'm going to comment out this save thing but what I am going to do is try to capture the toppings so I'm going to output those to the terminal so to do that I'm going to say error underscore log and then remember we use a request to get the data from the forum and the name of the name attribute we want which is toppings now what do you think is going to happen here let's save that and refresh over here and now we can see all of these different checkboxes if I do mushrooms peppers and garlic and then order the pizza it still works but if I go now to the other terminal we can see the only thing it outputs is garlic and that's because it's only grabbing the last one I do which was garlic right here if I check olives and uncheck mushrooms order pizza then it's going to output olives so it's not outputting an array of values it's not capturing all of them it's just capturing the last one so how do we tell laravel that look we want to capture all of the ones inside an array that the user selects well all we do is add square brackets at the end of the name over here and that tells laravel that look we want to store whichever values that our user selects inside a toppings array so if I save this now and try it out again let's order a pizza and let's do peppers garlic and mushrooms order the pizza and we get an error and that's fine because it's proving that this is working it expects parameter one to be a string and we've given it an array so we can't put an array inside this function over here I'll tell you what instead or let's return that value so we can see in the browser so let's get rid of that and return here I'm just going to comment out this dude save it and let's go back to the farm and our refresh and let's do this again order the pizza and now we can see we get an array so we're capturing now an array of data cool so we've done two steps so far we've now added this field this column into our tail and we've migrated that it's expecting Jason remember not an array and we've also added the fields to the form and we're capturing those right here as well in fact what we could do now you say okay well let's add the toppings property to this pizza as we have done with the other properties so pizza toppings is equal to request toppings so now we're assigning the array to this property but if we save this now to the database it's gonna try to save an array into adjacent field and that's not going to work so we need to somehow transform this thing right here into Jason now we could do that manually inside this controller action but there's a dead simple way to do this if we go to our pizza model so let me open up up and let's go to pizza inside here we can add in a property and that property is called casts and what that does is take an array as a value and any column that we pass inside there it will cast into a certain data type back and forth so let me do this and then I'll explain it I'm going to say protected and then it's called casts so dollar sign casts I set it equal to an array and inside this array we want to take the toppings column and we want to make sure that it's cast into an array when it comes from the database so when we're working with it in our application it's an array so if I save this now what it's going to do is when we try to save something to the database it's going to take the array and it's gonna automatically turn it into a JSON string now when we get that JSON string back from this toppings then it's going to turn it back into an array again so that we can cycle through that easily in our code so this automatically does that casting for us okay hope that makes sense so let me save this now I think that's all the moving parts done we might get an error because I might have forgotten about something so I'm gonna refresh over here and let's add a new pizza so my name is Shawn kind of spell it no not even my own name and I like the veg supreme with a garlic crust I'm gonna go with garlic and olives as the extra toppings order the pizza this works right here let's go to the database and I really flesh this view and we can see now we get toppings garlic and olives and the rest have been captured as well now if we go to forward slash pizzas what it's going to do is grab all of the pizzas right and output them now there's only one at the minutes so hopefully it will still grab that pizza yeah we can see it we're not out putting the toppings here but that's absolutely fine if we go to the one with that ID which is one then we should see the details of that pizza as well and we do the veg supreme and the base is garlic crust so why not on this page right here why don't we cycle through the toppings as well and output those so let me go now to the view which is show that's for the individual one and what I'm gonna do now is another paragraph tag right here and this is going to have a class as well just in case we style this later on so toppings or you can style it if you wish and I'll say extra toppings all right and then below that we'll do it ul because it is going to be a list and inside here we want to output an li tag for each individual topping so what we're gonna do now is cycle through the toppings now remember because we did this in the pizza model when we get that jason string back off the toppings it's going to cast it into an array so already it's going to be in the right format foot was to cycle through so we don't need to do anything else we just need to now use a for each loop and inside that loop I want to grab the toppings stored on the pizza that we take into this view remember if we go to the pizza controller inside the show method right here we are passing in a single pizza with that ID so I'm going to go back to show I'm going to grab that pizza then I want the toppings property because we're going to cycle through that in the for each now I'm going to say each one is going to be referred to as a topping so as not to pink topping and then inside will say we want an li tag and each li is going to output the actual topping so just do curly braces and topping then we need to close our for each so end for each who so I think that should work let me save this and refresh over here and now we see those toppings awesome so what I'm gonna do is try going to the home page and then go into the form again I'm gonna try one more so Luigi and the type is gonna be Hawaiian and we'll go with thick crust and Luigi wants absolutely everything on his pizza so now if we go to forward slash pizzas we should see both of those first of all yep we do and if we go to forward slash 2 which should be the ID of the new pizza we just added follow eg we can see all of those toppings on that page as well awesome so this all works so that my friends is how we work with checkboxes a rays of data and storing Jason in the database in the next video I'm going to talk about how we can remove records from the table
Info
Channel: The Net Ninja
Views: 42,702
Rating: undefined out of 5
Keywords: laravel, laravel 6, tutorial, laravel tutorial, laravel 6 tutorial, laravel tutorial for beginners, laravel 6 tutorial for beginners, laravel for beginners, laravel tips, beginners, laravel basics, laravel crash course, crash course
Id: C5g1G6AVdco
Channel Id: undefined
Length: 12min 11sec (731 seconds)
Published: Thu Feb 27 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.