Playing
10. String doesntContain() method

Transcript

00:00
A little while back Laravel added a contains method to its string helper set. Now we have a string doesn't contain. Let's go ahead and review both of them just in case you haven't seen either and let's see what they do. Okay so it's going to be pretty obvious but let's
00:15
go ahead and create our string here and let's just say something like I love cats. Let's go ahead and die dump and we'll test out the string contains first. Now we can either use this using the string helper directly from support which acts kind of like a facade where we can do something like contains and then we can pass in the string that we want to look for haystack and then needles
00:38
which could either be an array of strings or just a single string. So if we just go ahead and say cat for example and we open this up sure enough we get true. So this is one way of using it. Another way of using the string helper which is what I prefer just so I don't have to pull in an additional namespace here is just using the string function which just does exactly what you would expect it
01:00
goes ahead and returns whatever you are trying to call on that base string class. Okay so we kind of get how a contains method works now we have a doesn't contain method so obviously if we wanted to check before if a string didn't contain something we would have to negate it with doesn't and we would obviously get false here. Now we can go ahead and change this up to use doesn't
01:25
contain so let's go ahead and check it out doesn't contain and a string so obviously this is going to be false because it does contain cat. Let's build a tiny example out of this so let's go ahead and say if the string doesn't contain the word cat anywhere we're going to go ahead and just die so string must contain cat. Okay let's get rid of this we'll go over and just take a look here
01:50
and we don't get anything but if we change this over to I love dogs then of course we're going to see that issue so a really simple one but obviously when you are working with strings having both of these methods is super helpful and it obviously reads a lot better and makes your code look a little bit clearer.
26 episodes2 hrs 34 mins

Overview

Need to know what’s new in Laravel as it happens? Every episode of this course is dedicated to covering the most interesting and useful Laravel additions in detail, so you’re ready to start using them in your applications.

Check back often, and stay completely up-to-date with Laravel.

Alex Garrett-Smith
Alex Garrett-Smith
Hey, I'm the founder of Codecourse!

Episode discussion

No comments, yet. Be the first!