php-google-maps-distance-matrix icon indicating copy to clipboard operation
php-google-maps-distance-matrix copied to clipboard

Use in laravel controller

Open abdullah6066 opened this issue 5 years ago • 1 comments

Can you tell me how can I use it in laravel Controller

abdullah6066 avatar May 19 '20 18:05 abdullah6066

The readme does a decent job of explaining how one can use this library, but I hope the following example of a controller method helps:

public function index() {
    $response = DistanceMatrix::license($license)
    ->addOrigin('norwich,gb')
    ->addDestination('52.603669, 1.223785')
    ->request();

    $rows = $response->rows();
    $elements = $rows[0]->elements();
    $element = $element[0];

    $distance = $element->distance();
    $duration = $element->duration();

    return View('my.view'), [
        'distance' => $distance,
        'duration' => $duration,
        // and add any additional variables you want from the API here
    ];
}

RobinBastiaan avatar Oct 28 '22 14:10 RobinBastiaan