alexpechkarev/laravel-places-autocomplete
Composer 安装命令:
composer require alexpechkarev/laravel-places-autocomplete
包简介
Easily integrate the Places (New) Autocomplete - JavaScript library into Laravel Blade views. Provides a user-friendly way to search for and retrieve detailed address information in any web application.
关键字:
README 文档
README
A Laravel package that provides a simple Blade component to integrate the Google Places Autocomplete functionality into your application with minimal setup.
It handles API loading, session tokens for cost-effective usage, and fetching place details, allowing you to focus on building your application.
Features
- Simple Integration: Add address autocomplete to any form with a single Blade component.
- Cost-Effective: Automatically handles session tokens to reduce Google API costs.
- Customizable: Configure the component's behavior through an options array.
- Event-Driven: Dispatches a custom event with the selected place data for easy handling in your frontend JavaScript.
Live Demo
See a comprehensive live demo of the underlying JavaScript library in action: pacservice.pages.dev
Requirements
- Laravel 9.x or higher
- A Google Maps API Key with the Places API enabled. You can get one from the Google Cloud Console.
Installation
You can install the package via Composer:
composer require alexpechkarev/laravel-places-autocomplete
Configuration
- Publish the configuration file. This will create a
config/places-autocomplete.phpfile where you can set default options. The view file will be published toresources/views/vendor/places-autocompleteand the JavaScript files topublic/vendor/places-autocomplete/.
php artisan vendor:publish --provider="PlacesAutocomplete\PlacesAutocompleteServiceProvider"
- Add your Google Maps API key to your
.envfile. This is a required step.
// filepath: .env GOOGLE_MAPS_API_KEY="YOUR_API_KEY_HERE"
Usage
1. Add the Component
Use the <x-places-autocomplete> component in your Blade views. Because the component's root element has a dynamically generated ID, the recommended approach is to wrap it in your own div with a stable ID. This makes it easy to target with JavaScript.
// filepath: resources/views/your-form.blade.php <form> <label for="address">Address</label> {{-- Wrap the component in a div with a known ID --}} <div id="address-wrapper"> <x-places-autocomplete root-div-id="address-wrapper" :options="[ 'placeholder' => 'Start typing your address...', ]" /> </div> {{-- Other fields to be populated by JavaScript --}} <div class="mt-4"> <label for="city">City</label> <input type="text" id="city" name="city" class="form-control"> </div> <div> <label for="postcode">Postcode</label> <input type="text" id="postcode" name="postcode" class="form-control"> </div> </form>
2. Handle the Response
When a user selects an address, the component dispatches a pac-response custom event on its root div. You can listen for this event to receive the place data.
Add the following JavaScript to your application.
// filepath: resources/js/app.js document.addEventListener("DOMContentLoaded", function () { // Listen for the custom event emitted by the PlacesAutocomplete component document .getElementById("address-wrapper") .addEventListener("pac-response", function (event) { const placeDetails = event.detail; //console.log('Place Selected:', placeDetails); // Populate other fields based on the selected place details if (placeDetails.addressComponents) { placeDetails.addressComponents.forEach((component) => { if (component.types.includes("postal_town")) { document.getElementById("city").value = component.longText; } if (component.types.includes("postal_code")) { document.getElementById("postcode").value = component.longText; } }); } }); // Handle errors from the PlacesAutocomplete component document .getElementById("address-wrapper") .addEventListener("pac-error", function (event) { console.error("Autocomplete Error:", event.detail); }); });
Component Options
You can customize the component by passing an :options array. These options are passed to the underlying places-autocomplete-js library.
| Option | Type | Description |
|---|---|---|
placeholder |
string | Placeholder text for the input field. |
debounce |
integer | Delay in milliseconds before sending a request (default: 100). |
clear_input |
boolean | Whether to clear the input after selection. |
language |
string | The language code for results (e.g., 'en-GB'). |
For a full list of options, please refer to the JavaScript library's documentation.
Note: The name attribute of the generated <input> is not currently configurable. The input's data will not be submitted with a standard form post; you must use JavaScript to handle the pac-response event and populate your form fields as shown in the example above.
Support
If you encounter any issues or have questions, please open an issue on GitHub.
License
This package is open-source software licensed under the MIT license.
alexpechkarev/laravel-places-autocomplete 适用场景与选型建议
alexpechkarev/laravel-places-autocomplete 是一款 基于 JavaScript 开发的 Composer 扩展包,目前已累计 355 次下载、GitHub Stars 达 1, 最近一次更新时间为 2025 年 06 月 22 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「laravel」 「Google Places Api」 「address autocomplete」 「Blade component」 「Google Autocomplete」 「places autocomplete」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 alexpechkarev/laravel-places-autocomplete 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 alexpechkarev/laravel-places-autocomplete 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 alexpechkarev/laravel-places-autocomplete 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Google integration settings and helpers for Laravel Enso
Google Maps PHP SDK.
Modern Google Places API client for PHP
Magento 2 Social Login extension is designed for quick login to your Magento 2 store without procesing complex register steps
Client for Google Directions API to add interpolated points to a route consisting of given coordinates.
Alfabank REST API integration
统计信息
- 总下载量: 355
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 1
- 点击次数: 20
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2025-06-22
