Laravel Postcodes: A Service Wrapper Around Postcodes.io

News

October 21st, 2019

laravel-postal-code-validation.png

Laravel Postcodes is a package by Steve McDougall that provides a validation rule and macro for validating post codes. The package uses the postcodes.io service—a free and open-source API—for looking up geocoded UK postcodes.

There are three ways to interact with postcodes using this package. First, you can use the provided validation rule:

1use JustSteveKing\LaravelPostcodes\Rules\Postcode;
2 
3$this->validate($request, [
4 ‘postcode’ => [
5 ‘required’,
6 ‘string’,
7 new Postcode()
8 ]
9]);

Second, you can use the provided macro:

1$this->validate($request, [
2 ‘postcode’ => [
3 ‘required’,
4 ‘string’,
5 Rule::postcode()
6 ]
7]);

Third, this package defines a service for interacting with the API directly:

1use JustSteveKing\LaravelPostcodes\Service\PostcodeService;
2 
3app(PostcodeService::class)->getPostcode($postcode);

You can learn more about this package, get full installation instructions, and view the source code on GitHub at JustSteveKing/LaravelPostcodes.

This package was submitted to our Laravel News Links section. This section is a place the community can post packages and tutorials around the Laravel ecosystem. Follow along on Twitter @LaravelLinks

Filed in:

Paul Redmond

Full stack web developer. Author of Lumen Programming Guide and Docker for PHP Developers.