In this episode, we take a look at two handy helper methods in Laravel for working with strings: contains
and the newer doesntContain
. We start off by giving a quick refresher on the contains
method—basically showing you how you can check if a string (like 'I love cats') contains a certain word or part (like 'cat').
There are a couple of ways to use these helpers: either directly through the Laravel Str
facade or using the global string helper function, which keeps things nice and simple. We actually demonstrate both, so you can pick your favorite approach.
Next, we show how you'd previously handle checking if a string didn't contain something (by negating contains
), and then introduce the much more readable and convenient doesntContain
method. A quick example illustrates how to use it in real code, such as making sure a string contains a certain word before proceeding.
Overall, you'll see how these helpers make your code cleaner and easier to read whenever you're working with string searches in Laravel.