Laravel News
Lesson 2 of 2 · Laravel 5.8 Preview Part 2 | What’s New · Education
Laravel 5.8 Preview Part 2 | What’s New
welcome to level five point eight preview part two in this episode we're gonna explore even more new features coming to level five point eight stay
tuned so of course as development continues of level five point eight we have even more features to talk about and today we're gonna be talking about carbon two and a couple of changes to
the collection classes before we keep going go ahead and subscribe to the channel and click the little Bell button so you get notified with any new videos that we publish so with that being said
let's dive right in so first and foremost we're going to talk about carbon of course carbon is the PHP library that we use for daytime PHP is built in date time is a little
confusing and carbon makes our life extremely easy so for level 5.8 we're upgrading to carbon 2.0 now carbon 2.0 brings new features with it but the most important
new concept is the immutable date class and this is something that I personally ran into when I first started using carbon and it threw me for a loop and that is the following let me show you an
example so here we are in our level five point eight I'm just gonna pop into our web dot PHP and we'll continue from the previous lesson so route let's add a new get route and
we'll call it carbon function and so here it is let's say we have a date and we just call carbon now let's go ahead and dump date at this point okay and then later down the road let's say we
add days and let's add three days to that and then let's dump that date variable again let's check out the results in the browser switch over to carbon oops made a little typo here and
there we are so we see our first date is two seven and then our second date is two ten even though this is the same variable so what is happening is that we are actually changing our object now
this may be the desirable thing you want but most of the time it's actually not most of the time dates are protected you're gonna create a new date you should have to create a new date you
should not be able to modify a date because for example let's say that one of your columns is a date of birth that is not something that's going to change however you may create a copy of it and
do some modifications for it so for example we can find out when we are one week away from your birthday but that does not change your birthday your birthday is always the same it doesn't
change so in dates typically you don't want them to change like this and that's where the carbon in mutabal comes in now before we could talk about that we got to talk about the new facade so there's
a whole new facade based around here and the new facade in level 5.8 is gonna be date and we'll find it here illuminate support facades date so now all of the functions that you used carbon for
before now you're going to use date the date facade and so this is an opt-in feature if you don't want to use this you can continue to use carbon your regular way however starting in 5.8 if
you want to switch over to carbon and mutable in your app service provider in the register method you can add date factory and now we're gonna say use carbon immutable class and just by
adding that let's head back to the browser and I'm gonna refresh this and see what happens notice this first date is two seven and then the second one is to ten so now we refresh and we're at to
seven and to seven and that is because carbon is not actually changing the date and that's kind of the behavior that you expect from it so you see here we have a carbon immutable object as opposed to
just a carbon object and now the dates are exactly the same now to look back at the code you see here that we tried to add three days but that didn't work what we actually would have to do is create a
copy and on that copy we can add three days and so we have a new date equal to that and then we can say go ahead and dump new dates and now we're back to exactly the same thing again to seven
and to ten so that is kind of the expected behavior of dates in life in general and now the code reflects that so moving on let's talk about cash cash
and laravel is the easiest thing in the world and it facilitates cashing parts of your website makes it extremely useful so a small change in layer well five point eight is that up until this
point you could only get down to a minute cash right so if you wanted to cash something for say 30 seconds you couldn't do that because the minimum was one minute so now let me show you an
example we'll say route get we'll just call it cash again let's return a function and now let's cash something so say cash I'm gonna use the facade cash and then we'll
say remember and the key we're gonna give it it's just test demo and then as a second argument of course you're gonna pass in how long this is going to be we'll just say three for three seconds
remember now this number is in seconds this is the change right here so now we can say function and we're just going to return time so I just want to prove to you that it is being cashed every three
seconds so in the browser let's move on to cash our next example and there we are so these are seconds in you next time so notice here we have 73 80 83 and if
you keep refreshing 86 and it is 89 so you see it is resetting every three seconds notice even though I'm refreshing if you watch up here I'm continually refreshing but we are not
getting new values we're getting values every three seconds and that is because now cash is down to the seconds so look around your projects make sure that you take any values that you currently have
and you're gonna have two times sixty that same value so if you had three before for three minutes now of course you'd have to have a hundred and eighty seconds so that's a
quick change look through your code anyplace that you're using cash go ahead and convert it from minutes to seconds laravel five point seven the minimum password requirements with six
characters that is now moving to eight characters which is obviously cryptographically more secure so to show you that let's pop up into our terminal and we'll
say PHP artisan make off and so now let's go ahead and try to make a user let's register and so say John Doe John and example.com and then we'll say 1 2 3 4 5 6 I know you can't see it but that's
what I'm typing 1 2 3 4 5 6 register and we get the password must be at least 8 characters so this is a nice little addition just to stay a little bit more secure by default so this is a nice
welcome change nothing crazy here no breaking changes with that alright so up next let's talk about Amazon DynamoDB so if you are doing any sort of caching as a service
you might be using Redis you might be using your database however a new service is Amazon's DynamoDB now of course it promises like every AWS product to be absolutely scalable it is
a non relational database just like Redis is and they promise performance in the single milliseconds so extremely fast in level 5 point 8 you can now use this database so if you visit your cache
that PHP file your config file you see here now of course you can use array database file memcache and Redis but now you do have this new AWS service available to you so if you're already in
the AWS cloud and you want to expand on your services with your laravel project this is an easy change for you alright the last two things we're going to be talking about is to label collection
changes that are coming so the first one we've already covered in our channel however just for representation in this video we're going to show it one more time in a very quick overview so let's
say route get first where so first where is a method that is going to allow you to return the first instance where something happens out of a collection so if we had a collection and let's just
say return collect if you guys are not up to par with all the collections we do have a whole series over 60 videos at this point in every single method in the collection so definitely check those out
this is just going to be a quick preview of collections so let's say product let's say this first one is no and then the second one we'll just say 20 so we are looking for this product right here
and all we want to say is give me the first where product is true okay let's go back to our browser change over to first where and there we are so we're getting product equals 20
but in layer about 5.7 it requires you to have a second parameter so you could not do something clean like this you would actually have to have a second parameter in which most the time you'd
probably just pass true but now you don't have to do that so that's a nice welcome addition that follows the flow of all the other collections in Lenovo and the last thing we're going to talk
about is the reject method so the reject method let's show that one here in a new line let's say reject function so the reject method is going to allow you to completely erase your collection and
this is useful sometimes if a random event was to happen say in your app we're gonna simulate that right now we'll say that a random event happened right so we'll set that equal to true
and then let's have our collection collect data and we'll just grab this collection right here since we already have a collection built up so we'll just say that's our data object and then
let's just say if this random event happened I want to go ahead and reject this whole collection meaning I want to empty it out and you can call reject now with no parameters that is the change
reject has always been around but this is the new change now you can just call it without any parameters at all where before you would have had to pass true which didn't make a ton of sense because
reject will actually allow you to reject specific keys but if you just want to reject the whole thing you could just pass in no arguments at all all right so let's check that out
in the browser reject and of course we got an empty collection now if that random event we were talking about did not happen in that case then of course we get our collection back so with that
that is the level 5.8 preview – there's a second video on this a subscribe to the channel stay up to date we are still actively working on our collections course and we have a ton of
video and content coming every single week so stay tuned subscribe and if you have any questions hit us up on twitter at coders tape or down below in the comments for coders tape this is Victor
thanks for watching
Laravel 5.8 is still under development but today, we are doing part 2 of our Laravel 5.8 What’s New Preview on what’s to come when it gets released.
For the best experience, follow along in our interactive school at https://www.coderstape.com
Hit us up on Twitter with any questions or comments @codertape (https://twitter.com/CodersTape)