Laravel 8 Array helper | How to use array helper in Laravel 8 | Learning Points| Updated 2021

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hi guys in this video we're going to see some normal helper functions for handling the complex arrays so let's get started [Music] i'm going to add a few basic arrays here a simple array now here we have just the values and at the last value has a further array of single value now also i have the array key the string and the boolean as the value so these two so to work with the array functions in the laravel the first one i want to show you is the dynamically adding the css classes so what we do basically and after that you call the class of the function so here i say c so this is the function and it takes the array so here i'm going to pause this array variable and let's output this now if i refresh here we get here class one class three the thing is that class one has true set as a value in class three set as a true as a value so if i switch these in that case we get this a class one cluster so you get the idea when you're dealing with data and you're having data coming from database you can use your data something like this you can pass boolean values and in your html for example you might have a class and there you can pause the variable like this all you need to do is put your output variable so i'm just gonna copy and paste it here so this way so this class might be you might be having it in dave and you might have something like this so this is going to output that those two classes this way and it is going to add there and you might have some css that is working on these glasses so it's going to be useful now we can set values in our a and get bubbles in array we know that that quite simple way in laravel we can do it in multi-label so let me show you how we do it after that we are going to get the value so here we say here data underscore set all right and when we say set we can uh set the value so for setting the value i want to use a type of array where we can see the more appropriately so i'm going to copy the array here this copy here this is the way where we're going to set up a value and here the second argument is going to be the way of parameters you want to set the value so here i see here products all right and then i put dot and this dot will go inside a further array so i see here desk and then for the array price and i say set this price to 200 all right so this is a value we're setting and i'm going to pause this products array here all right so here now let's even get so i refresh all right we need to remove this for this particular case and save this go back and refresh now here we have two arrays if you see the products array we have products and inside we have a product so this is by default previously there and the products here so if i get products we get products desk and then decided price and we have only 200 so this is something something we just added now if you want to update this particular array we can remove this s now this time it will go there then me so let's do that so i save this refresh this time we go product and it is having the price there it's yeah these values are in array that's why we have 0 1 2 here and this added this value right after this last array so we have here price 200 i might want to update this particular 230 so to do that i need to say that go inside a particular product and for that we have here monitor so which is 0.2 so i will say here 2 dot and go there refresh open and we get price 200 so here it is 230 and here now it's 200 it is just updating that value because it's just got it from that index so this way you can update the values in um deeper array now let's say if we want to get the value here i say get the value for obviously forgetting we don't need a pause anyway we're just going to pause the array so here we set the value i'm going to put this here [Music] single dollar and i say again the price all right so here we are setting it to let's set it to let's say 440 let's say so we setting it to 40 we don't have 40 there so we will set 40 and then we're getting uh to get we say here get all right let's see refresh we get 40. so it is getting the value from deeper array so you get the idea how you pass products it goes in product and it goes in price you know where the index is two now you might have a loop where you might want to use this and get the value of each because if i pass here one it is going to get me to 70 so let's let's see that so we get 70 so yeah this is something useful you can use now there is another interesting function from larval where we can convert our array into a query so let's let's see that so hit call query simply now for our a we have these three arrays i'm going to pause simple already okay now we have all come out pooped and did in here so let's refresh so you can see here we can name and model and price and feature so it's just converted query and you in your code you might want to use it somewhere you want to bigger projects bring bigger data and you will be sorting that data as well sorting is good and you can do it by using a simple functions in php but in darwil we get a lot of benefits so let's see how we can do that so here i have the array query i'm going to convert this query so to see this works we have here this product so i'm just going to change this because that has some values with different current now it's not going to show up let's see we have product and we have the cem and that's why because we have the this array inside product so what i do i simply pause uh the prop because well this is inside there so you can see that it's not order currently and uh if i save this and go there refresh and you can see the keys already changed and we have the values here it's working perfectly now this is the basic sorting that we just did in here with products but i want to show you some complex stuff so let's do that so we currently have products array in this simple and simple right here so i'm going to add a complex array which is going to be deep level so here i'm going to copy and paste to save time and here i'm going to pass that array now let me zoom out a bit so yeah we can see so we have a complex array where we have people data where we have one person's data so john the media's houses and this house first has the array inside it and the others doesn't then you have sides then the uh directions we have right here and inside there is another array and the products and a lot of things going on this one so what we do we are going to copy this complex array and we are going to use a function it's going to be recursive all right and we pass our variable so semicolon is required so this is going to solve everything recursively so let's see go here refresh we get this data side by side let's see how it looks so here uh i have this people uh data so i open this you can see that it's sorted already if we go inside sites and you can see we have here east-west north south here east north southwest so it's changed already video houses you can see there too we have this holding working we have products let's see products so you can see it's a deep label uh sorting going on here so we have chair desk monitor so it's something that is it's sorted inside that array as well now we have this uh this array inside it so let's see that one so i think it's here yeah it's here so you can see this is sorted inside this array we have q t so this is also sorted so yeah you can see that how good is this function we just added the array and it all recursively uh sorted all the values now another type of uh function that i really like is a flattened array now i want to pass the complex around this one because we want to have let's refresh now you can see that it went into all these values so named john h it is just taking all the values simply and just making a single array so we have 33 values in that particularly in a deeper level but it just got all those release here this can be useful when you're dealing with complex data and you might want in some case values from few arrays you can pass uh bowser is in a single array and then you can pass that in this flattened so it's gonna give you all the data as you like let's see example how we can check what type overlap we are dealing with so if we don't know what data we are getting from database and we want to check so and it is going to tell us if this array is associated right so if i go there we get true now let's say do we have a simple array no what here dollar numbers and now we can pause this so here now we see this becomes false this is not a shitty right so i'm just going to say here add and i'm going to add an array here and to see the different type of data because i have this complex array that's it's too too big to work with so i'm just going to add here another array and this is mixed array so we have this much data here already so okay now i'm going to use this array for our this example here in us name and then pause uh well let's see so pause this name and we want to check it so let's go refresh now you see here we have people we are getting the values in index and here you can see that and at last it added the uh my new name so it works just fine now if i want to add it's in the seventh place uh the name only or the data of array so what we can do in that case we simply say here seven we put comma and we're just telling them put it in seven place so this is an array we're passing say refresh we get sound we get a and we get name but that can be useful in some scenarios now let's see an example if you want some filter data out of an array so for example i have this mixture a and i want values from inner arrays and i don't know the value from this very array so for example these four values roger mark one two three these are the values we don't want we only want the arrays data from these arrays like this right here in this area here all the data from these arrays i want to forget so there is a function provided in larval that can be used to save our time so we just gonna use that it is collapse and here i'm just going to remove name and other arguments now this is going to get us the values so refresh here now let's go to the array back we have people array we have john 32 been getting those at least here we are not getting roger mark one two three now we are not getting that we are getting one two here we got a b and one two you can see that so if this can be useful if you're getting data from database on some level you want only the nested arrays so you can use the collab collapse helper from right marvel now let's see uh how we can remove values from ray so here we have the clubs i'm just going to comment this one out and i'm gonna put this here now to remove a value we need to use a function poll and for this i'm going to use a simple array and i'm going to call this numbers let's do that so i'm just going to put numbers here and i'm going to toss the key which key we want to remove so here i say let's say uh key2 all right so here we go here refresh you will see we got the key and the value of n2 so we can three now if i print these numbers so if you're printing output which is here if i print this would come here and numbers you will see the number three is not there so we see here one two let's compare it with the array so we have a right here so you see the index two is gone and that value is gone so if i change index two to index three so this 565 should be gone so let's see here refresh now it's showing up there but it's not in array anymore so this can be useful now the only thing you need to make sure whatever key you're passing is present there so let's see in if you want to remove one of the products so what i do so i'm just going to copy and paste this and inside product i would say pass this key product so we want to go inside it and in product we have three products so we have 0 1 2 and these are arrays so if i say here let's remove one [Music] we need to pass this here to see the outcome all right save that refresh now you'll see here we have share 70 showing up there but here we have zero and two one index is gone and there is no chair anymore now let's further do some data operation on our race so here is we to save it go there we get the first value john h32 we go here the first value is people a key and the value is this it simply got the value now let's let's do this so i'm going to pause the numbers and i'm going to pause numbers like this because this first method can do further operation than just getting the first value so here we can pass a function and this function gets a if dollar value is more than something so let's let's say what we should get so we have here numbers one two maximum 565 which is the only number here um over 100 so we can pass that if the number value is good and it should give us the output let's see so we get 565 because it is more than 100 so if i say if it is more than let's say the second bias number is 78 so we can say here okay if it is less than 100 and the value is more than 70. do we have any value which is 78 so we get the 78. so you get the idea we uh get the volume based on operation now we can also do one thing here that if we say if a number is less than let's let's do this i'm just going to copy this i comment this one and just put here like this and i say if the value is more than 700 which is not present there so we get no so you can put some kind of validation but here if i put comma and then put here 12 for example and go there we get 12. the thing is in this function narlow helper we can pass a default value so if we are not getting any output after the operation we will get that value which is the default and given as there so we can do that and we have 12 so if i pass here anything that is going to be default and how much this one out the same way here we can say last so it operates similarly so refresh you will get the default volume here i can change 23. get 23. now if i simply use lost and remove everything so just like this and refresh and we get 45 because that is the last value so by default it gives us the last value but if you want to put the operation into it you can do the operation all right let's see a few more functions here now i'm going to command this one out and here i'm going to paste these versions so like this and let's see quickly how these work so first one is the accept so we're just passing the keys here from these keys we don't want values except these keys get all values so if i go here and refresh we get that a key four five six values if we go to the array it's here so we're getting those values but we are not getting uh roger mark one two three so if i change here if i remove two keys from there you will see that we will be getting the key and data for these two keys too so that's how it works now the next one is exists you just pass the array and the key and we check if it exists so if i go here refresh it says true so we have zero key there so if i say here let's check if there is key 20 present in this array it gives us false so you might want to check before inserting data or giving the data you can do that previously we saw the data get now this time we get to check the array get so this function what exactly does we pass the array we tell this is the value we need to get if you don't get the value give us the default value so we have zero here so save this and refresh here we get zero now if i go to products.price inside products what we get so we go here we have products already inside there we have product then we have name and price the thing is these are coming as a value and there is index 0 1 2 because this is inside a big array you can see that this already starting here so we can't straightaway say product.price we need to give key so here that's why it is going to zero so if i say here dot and pass here keep one this time it will go product key one is zero one which is share and then price and we should get seventy so let's say i refresh we get 70. so this goes there so if you have uh you want to get the value you might want to pass here really dynamically that can be done here it can be very useful if you don't get the value you can pass some default value and that can be useful too so this has it's quite simple it's going to check the value for us so if i refresh here it says false and the same way we haven't passed the key here so if i pause here dots are zero and refresh then it says true because the present so i'm just gonna remove it because it's not present so that says yeah it works so i'm just gonna comment this one now now the another one is random so i'm just going to remove this come in this one and remove this comment so the same random products now products has only one milli in this case so that is not the right array because if you say you're only one key so it's not gonna work i'm just gonna pass numbers and here let's pause numbers and refresh we get 67 and if i go to array we get 67 here from our a then we get this then we get 3 then we get 67 then we get you know randomly each time it refreshes it to get to know the same way we can work with the uh shuffle shuffle will get us the complete array not a single number but shuffled so i'm just gonna pause right here as numbers i say refresh sorry about that the spelling is at my right so it's going to be shuffling like this refresh we get the shuttle number refresh you will see each time it's changing the position so now the another one we need to see is the prepend in some cases you might want to add some value before and in the first place in the array so you can do that we have zero mark then product and that's not the right array to put mark there so we can do let's put it simple array that makes more sense as we are inserting the name so refresh you get the zero mark the name desk and all that so you can see simply that the first value it isn't something there however this array doesn't make any sense uh the type of data we inserted because we are passing the value with the zero index so if the index is in string you might not want to use this one before ending this video i want to show you one last data working function and run uh you can use it on any array and you can pass a function so let's say here function and then we are going to do some operation on it so here i say dollar value and we have access to dollar key and here then we can work with it now if you are not going to use key don't define it here so i'm just going to remove it in that case and here i'm just going to say var dump all right refresh we get all the values so if you want to do some operations and we simply say return and it is going to return the value so we have array of that particular value in the id as well so we have the key as well as volume so you can use that one if you say here more than 40 [Music] you will get more value so this can be useful in way more better way when you're dealing with a lot of data and can save a lot of time so you might want to use it go for this video is in the link in the description also thank you for watching see you in the next one [Music]
Info
Channel: Zarx Biz
Views: 2,319
Rating: undefined out of 5
Keywords: How, to, Program, laravel, laravel v8, laravel 8, laravel project, laravel setting up, laravel v8.0, v8.0, laravel 8.0, laravel 8 playlist, laravel 8 tutorial, laravel 8 lesson, laravel 8 introduction, laravel tutorial, laravel 8.0 tutorial for beginners, laravel for beginners, laravel tutorial for beginners, helpers in laravel 8, helpers in laravel8, laravel8 helpers, laravel 8 helpers, helpers, helpers in laravel, laravel8, web hosting, helpers classes, custom helpers, developer
Id: Nw6eCiUtU8U
Channel Id: undefined
Length: 23min 12sec (1392 seconds)
Published: Sat Sep 04 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.