You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Refactor Places Autocomplete component and update README
- Enhanced the PlacesAutocompleteComponent with additional properties for better configuration.
- Updated the Blade view to utilize the new properties and improved event handling.
- Added a GIF demo to the README and expanded documentation for clarity.
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.
[](https://packagist.org/packages/alexpechkarev/laravel-places-autocomplete)
A Laravel package that provides a simple Blade component to integrate the Google Places Autocomplete functionality into your application with minimal setup.
9
+
10
+
It handles API loading, session tokens for cost-effective usage, and fetching place details, allowing you to focus on building your application.
11
+
12
+
## Features
13
+
14
+
-**Simple Integration:** Add address autocomplete to any form with a single Blade component.
15
+
-**Cost-Effective:** Automatically handles session tokens to reduce Google API costs.
16
+
-**Customizable:** Configure the component's behavior through an options array.
17
+
-**Event-Driven:** Dispatches a custom event with the selected place data for easy handling in your frontend JavaScript.
18
+
19
+
## Live Demo
20
+
21
+
See a comprehensive live demo of the underlying JavaScript library in action: [pacservice.pages.dev](https://pacservice.pages.dev/)
22
+
23
+
<imgsrc="places-autocomplete-js.gif"alt="A video demonstrating the Places Autocomplete JavaScript component in action, showing address suggestions and selection.">
24
+
25
+
## Requirements
26
+
27
+
- Laravel 9.x or higher
28
+
- A Google Maps API Key with the **Places API** enabled. You can get one from the [Google Cloud Console](https://console.cloud.google.com/google/maps-apis).
1. Publish the configuration file. This will create a `config/places-autocomplete.php` file where you can set default options. The view file will be published to `resources/views/vendor/places-autocomplete` and the JavaScript files to `public/vendor/places-autocomplete/`.
2. Add your Google Maps API key to your `.env` file. This is a required step.
47
+
48
+
```dotenv
49
+
// filepath: .env
50
+
GOOGLE_MAPS_API_KEY="YOUR_API_KEY_HERE"
51
+
```
52
+
12
53
## Usage
13
-
In your Blade view, you can use the `<x-places-autocomplete>` component to render the autocomplete input field. You can pass options as an array to customize the behavior of the component.
54
+
55
+
### 1. Add the Component
56
+
57
+
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.
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.
89
+
90
+
Add the following JavaScript to your application.
91
+
92
+
```javascript
93
+
// filepath: resources/js/app.js
94
+
document.addEventListener("DOMContentLoaded", function () {
95
+
// Listen for the custom event emitted by the PlacesAutocomplete component
96
+
document
97
+
.getElementById("address-wrapper")
98
+
.addEventListener("pac-response", function (event) {
99
+
constplaceDetails=event.detail;
100
+
//console.log('Place Selected:', placeDetails);
101
+
102
+
// Populate other fields based on the selected place details
You can customize the component by passing an `:options` array. These options are passed to the underlying [places-autocomplete-js](https://github.com/alexpechkarev/places-autocomplete-js) library.
|`placeholder`| string | Placeholder text for the input field. |
131
+
|`debounce`| integer | Delay in milliseconds before sending a request (default: 100). |
132
+
|`clear_input`| boolean | Whether to clear the input after selection. |
133
+
|`language`| string | The language code for results (e.g., 'en-GB'). |
134
+
135
+
For a full list of options, please refer to the [JavaScript library's documentation](https://github.com/alexpechkarev/places-autocomplete-js?tab=readme-ov-file#configuration).
136
+
137
+
**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.
20
138
21
139
## Support
22
140
23
-
[Please open an issue on GitHub](https://github.com/alexpechkarev/laravel-places-autocomplete/issues)
141
+
If you encounter any issues or have questions, please [open an issue on GitHub](https://github.com/alexpechkarev/laravel-places-autocomplete/issues).
24
142
25
143
## License
26
144
27
-
Collection of Google Maps API Web Services for Laravel is released under the MIT License. See the bundled
This package is open-source software licensed under the [MIT license](https://github.com/alexpechkarev/laravel-places-autocomplete/blob/master/LICENSE).
0 commit comments