PHPackages                             serjoagronov/livewire3-select2 - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Framework](/categories/framework)
4. /
5. serjoagronov/livewire3-select2

ActiveLibrary[Framework](/categories/framework)

serjoagronov/livewire3-select2
==============================

Simple to use Livewire3 component for Select2

v2(1y ago)127MITPHPPHP ^8.2

Since Sep 23Pushed 1y agoCompare

[ Source](https://github.com/SerjoA/livewire3-select2)[ Packagist](https://packagist.org/packages/serjoagronov/livewire3-select2)[ Docs](https://github.com/serjoa/livewire3-select2)[ GitHub Sponsors](https://github.com/JackBayliss)[ RSS](/packages/serjoagronov-livewire3-select2/feed)WikiDiscussions main Synced 1mo ago

READMEChangelog (2)Dependencies (14)Versions (3)Used By (0)

Simple to use Livewire component for Select2
============================================

[](#simple-to-use-livewire-component-for-select2)

[![Latest Version on Packagist](https://camo.githubusercontent.com/8851fccbede32729d0e7ca260fcec732343252769c01c5de2fbbf91b91dc779f/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f6a61636b6261796c6973732f6c697665776972652d73656c656374322e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/jackbayliss/livewire-select2)[![GitHub Tests Action Status](https://camo.githubusercontent.com/489021c02f04608e7bac57c04b5e173775ea17a3063ee9d44bcadeb10796fdea/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f616374696f6e732f776f726b666c6f772f7374617475732f6a61636b6261796c6973732f6c697665776972652d73656c656374322f72756e2d74657374732e796d6c3f6272616e63683d6d61696e266c6162656c3d7465737473267374796c653d666c61742d737175617265)](https://github.com/jackbayliss/livewire-select2/actions?query=workflow%3Arun-tests+branch%3Amain)[![GitHub Code Style Action Status](https://camo.githubusercontent.com/63b96fb095d81d92ad68be27de0f7ab801736ce5eedcf678e54f88896e1a964b/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f616374696f6e732f776f726b666c6f772f7374617475732f6a61636b6261796c6973732f6c697665776972652d73656c656374322f6669782d7068702d636f64652d7374796c652d6973737565732e796d6c3f6272616e63683d6d61696e266c6162656c3d636f64652532307374796c65267374796c653d666c61742d737175617265)](https://github.com/jackbayliss/livewire-select2/actions?query=workflow%3A%22Fix+PHP+code+style+issues%22+branch%3Amain)[![Total Downloads](https://camo.githubusercontent.com/591c6299fc92ee618eda2dd5972c6ea76becfb11803b9413d8467907d1b25d3c/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f6a61636b6261796c6973732f6c697665776972652d73656c656374322e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/jackbayliss/livewire-select2)

Easy to use Livewire component specifically for Select2.

Installation
------------

[](#installation)

You can install the package via composer: This currently works on Livewire v3, ie Laravel 11.

I haven't tested 2 yet.

```
composer require jackbayliss/livewire-select2
```

Initial Setup
-------------

[](#initial-setup)

First of all, ensure you install jQuery, and select2- for example, the below. You can also install these via npm and import it into your app.js. Main thing is ensuring the scripts are included anywhere you want select2 to work.

```

```

Usage
-----

[](#usage)

The package is quite straight forward, and can be used directly in a **LIVEWIRE** component, see below the basic usage

```

```

Component parameters
--------------------

[](#component-parameters)

#### options (required) - array

[](#options-required---array)

The select2 component expects an array of options, this can be for example - you must pass this into the options param as per the above.

```
public $vehicles = ['Ford','Vauxhall','Seat'];
```

#### onchange (required) - string

[](#onchange-required---string)

The select2 component expects a string of the listener function to call. For example, `triggerMyFunction`- you can see how to do this below. The onchange function, expects one param. The param returned from this component is an array of data - which includes the following:

##### Name - The name param you passed in, this can be used in your custom function if required - this is null if not supplied.

[](#name---the-name-param-you-passed-in-this-can-be-used-in-your-custom-function-if-required---this-is-null-if-not-supplied)

##### Data - This is the values returned from the select2 change, if you have not set the multiple param, this is a string- otherwise its an array. Please see  if theres any confusion.

[](#data---this-is-the-values-returned-from-the-select2-change-if-you-have-not-set-the-multiple-param-this-is-a-string--otherwise-its-an-array-please-see-httpsgithubcomjackbaylisslivewire-select2tabreadme-ov-filebase-component-if-theres-any-confusion)

Important

Ensure your function includes one parameter, for example `triggerMyFunction($select2)`

#### name (not required) - string

[](#name-not-required---string)

You can pass this to your component if you need specific logic based on the name.

#### model (not required) - string

[](#model-not-required---string)

You can pass an option value in as model, this then auto selects the option- useful in a case where a selection has already been made.

```

```

#### How to add a custom class

[](#how-to-add-a-custom-class)

This can apply a custom CSS class to the Livewire Select2 component, see below for an example

```

        .example{
            width: 50% !important;
        }

```

```

```

#### multiple (not required) - blank param

[](#multiple-not-required---blank-param)

Tip

If this is used, the data will be returned as an array, rather than a string.

This allows you to select multiple options, as per select2 usually does and should be used like the following:

```

```

Creating a Listener
-------------------

[](#creating-a-listener)

Tip

A listener is required for the onchange param, in order to create a listener, you should do the below. This means, whenever the select2 component is changed - ie an option clicked, it will call the function you defined, and you can do as you wish with the logic.

```

```

```
    protected $listeners = ['triggerMyFunction'];

    public function triggerMyFunction($data){
        dd($data['name'],$data['data']);
    }
```

Important

Whatever string you pass to the onchange param, ensure you create a listener and function like the above. **Your function must accept one parameter, this is the array of data returned which includes name and data, name being the name you set on the component (if you did set one) and the data ie the value selected.**

Example / How it works in practice
----------------------------------

[](#example--how-it-works-in-practice)

#### Base component

[](#base-component)

I have an initial component, which I am calling as livewire:test in my blade. The component is below:

```
