در حال بارگذاری ...

Introducing Laravel Phone0:01

Laravel is amazing when it comes to validation, and it provides a ton of rules out of the box. But there is one kind of data where it's a little bit more complicated. So we're talking about phone numbers here. Let's say hi to Laravel Phone. This is the repository for a Laravel form by Propaganistas. I didn't find which person is behind this package, so yeah, we have to go with Propaganistas for now. So it's a pretty small package. You can also see that it hasn't been updated that much in the latest month.

Installing and Testing Setup0:30

So it's a pretty small package. You can also see that it hasn't been updated that much in the latest month. But yeah, it has a lot of GitHub stars because it's quite unique and super, super helpful. And again, it's one of those packages, super small, but big impact for application. There is a demo which you can look at, but we don't want to do this. We are diving right in, and I'll show you in the code why we need this package. Let's install it. All right, and we are ready here inside a CreateUserTest. So you want to make sure that it stores a user's phoneNumber, which we have provided here.

So you want to make sure that it stores a user's phone number, which we have provided here. Let's check this, if it works. So it seems to be passing. And we see that a user has this phone number. So this must be working. It looks like that. And let's take a look at the controller. Here we have our validation right in the controller, and here we're making sure that our phone number is numeric.

Replacing Numeric with Phone1:28

Here we have our validation right in the controller, and here we're making sure that our phoneNumber is numeric. So this is mostly what you're going to use because this is what Laravel and php offers you, and everything more than this is quite complicated because there are so many ways phone numbers could look like, especially for different countries. What we can do now, for example, with this new package is something very cool. So first we're going to switch out numeric with phone. And if we run our test, it will still pass. Nothing changes here.

Restricting Allowed Countries2:00

And if we run our test, it will still pass. Nothing changes here. But one of the new cool things that we can do is, let's say we only want to accept phone numbers that are from my country, from Austria. Let's take a look at the test again. This is a valid one, so this should pass. But if we take, for example, I think this is from Germany, our test should fail. And it does. It does not. Why? Because I've changed the wrong thing here.

And it does. It does not. Why? Because I've changed the wrong thing here. So this is what I need to change. Let's try it again. And now this is failing. The following errors occurred during the late request validation phone. So this is now not working anymore because this is a German phone number, and we said that we only wanted to accept the ones from Austria. Okay, what about now? Let's add just Germany here. And now German number is working.

Okay, what about now? Let's add just Germany here. And now German number is working. The Austrian number is working. Now it's not because we, it's failing because of what we expect in the database, which is a different error. So this should still pass. And then let's say, I don't know which country this is. I don't know if it's even a country, but it should fail now. And it does because the validation of the phone did not work. So how cool is this?

And it does because the validation of the phone did not work. So how cool is this? We can now not only define that we want to have a phone number, which already works way better than just a numeric field, but we can also define which countries we want to allow. And if you're not a fan of those string syntax, there is also a new phone rule, which you can new up like this and then call country, I believe. And then I think we need to provide an array. So again, let's take Austria and Germany.

And then I think we need to provide an array. So again, let's take Austria and Germany. And I think our test should pass. It does not. I think we kept the wrong number here. So let's go back to the Austrian number and it does. So this is another way how you can use this package if you prefer this syntax. And the nice thing for a long time, I wasn't aware of this package. And now I've seen that quite some big applications use this package.

And the nice thing for a long time, I wasn't aware of this package. And now I've seen that quite some big applications use this package because it's just so useful, especially if you have to deal a lot with user phone numbers in your application. And this is something that Laravel cannot provide by default because it's quite complex. So I'm really happy that there's this package for this. And if we go back to the documentation, we see there are some new things that we can do.

Validating via Country Field4:29

And if we go back to the documentation, we see there are some new things that we can do. So what we can also do is we can define that there is a specific country field. So what we're seeing here is that we have a phone field. And here we are checking that this is a phone, but we want to make sure that this number is from a specific country. So we don't have to provide the country like we did here before. We can say the country is stored in a different field in your database. So this way you don't have to provide it yourself like we did here.

Casting and Formatting Numbers4:59

We can say the country is stored in a different field in your database. So this way you don't have to provide it yourself like we did here. You can use it from one of your fields because your User can define from which country he or she is from and then you're taking this column to check again. So this is also super nice and important. And there are some more things that you can do, and then something more interesting also is attribute casting. So here are two specific formats for phone numbers, which are used quite a lot,

So here are two specific formats for phone numbers, which are used quite a lot, and you can cast directly then from your database columns. And what this means now, you don't get just a phone number back, but an object, and on these objects, you have a lot of different methods to get specific data. Let's see if we have some examples here. Yeah, formatting is also a thing. If you're going to cast your phone numbers, you can format this in specific ways.

If you're going to cast your phone numbers, you can format this in specific ways. So this is also quite helpful if you use casting. Yeah, or number information, getType, getCountry. So a lot of things that you can do if you cast your phone numbers with this package. Let's break down what we just learned. Validating phone numbers is hard. I mean, really hard. So I'm really glad that here's this package that can help you out.

I mean, really hard. So I'm really glad that here's this package that can help you out. So I will use this package every time that I need to deal with phone numbers. Thank you, propagandists.

دوست دارید گاهی خبرهای Laracasts را ایمیل کنیم؟