Laravel Postcodes

  • 26-03-2022
  • 1341
  • Laravel 6
  • Haresh Chauhan

Now we can do this example how it works Laravel 6 postcodes validation rule. The package uses the postcodes.io service a free and open-source API for looking up geocoded UK postcodes. Laravel 6 provides a validation rule and macro for validating postcodes. There are three ways to communicate with postcodes using this package. In this example, I will explain to you step by step 3 ways to communicate with Laravel 6 postcodes validation. This package validation is used to you can check postal code validation on for UK geocoded. Now we will start this example.

Laravel Postcodes

Step 1 : Install Package

open your command line and follow this command.

composer require juststeveking/laravel-postcodes

After the installed this package, merge configuration for services using.

php artisan vendor:publish --provider="JustSteveKing\LaravelPostcodes\PostcodesServiceProvider"

Step 2 : Add the following code

config/services.php

If, for some reason postcodes not working following below steps.

<?php
'postcodes' => [
    'url' => env('POSTCODES_URL', 'https://api.postcodes.io/')
],

Add POSTCODES_URL to your project .env file and add this line https://api.postcodes.io/ as the value.

.env

POSTCODES_URL=https://api.postcodes.io/

Step 3 : Basic Use

Here, we can use three methods right now we do that.

Method 1

Now we can use validation rule.

<?php

$this->validate($request, [
    'postcode' => [
        'required',
        'string',
        new Postcode()
    ]
]);

Here we can use second method.

Method 2

$this->validate($request, [
    'postcode' => [
        'required',
        'string',
        Rule::postcode()
    ]
]);

If you want to communicate with the service itself.

Method 3

<?php 
class PostController extends Controller
{
    protected $postcodes;

    public function __construct(PostcodeService $service)
    {
        $this->postcodes = $service;
    }

    public function store(Request $request)
    {
        // validation using example above
        $location = $this->postcodes->getPostcode($request->postcode);
    }
}

If you want to get more information visit this github postcodes package.

I hope it can helpful for you.

You want to get post update news do subscribe our blog.

Thank You.


We always thanks to you for reading our blogs.


dharmesh-image

Dharmesh Chauhan

(Swapinfoway Founder)

Hello Sir, We are brothers origin from Gujarat India, Fullstack developers working together since 2016. We have lots of skills in web development in different technologies here I mention PHP, Laravel, Javascript, Vuejs, Ajax, API, Payment Gateway Integration, Database, HTML5, CSS3, and Server Administration. So you need our service Please Contact Us

haresh-image

Haresh Chauhan

(Co-Founder)


We Are Also Recommending You :