PHP code example of laspi94 / livewire-select2

1. Go to this page and download the library: Download laspi94/livewire-select2 library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

laspi94 / livewire-select2 example snippets


return [

    /*
    |--------------------------------------------------------------------------
    | Theme
    |--------------------------------------------------------------------------
    |
    | You can cinfugre your theme, provider by select2.org
    |
    | can be: classic or bootstrap5
    */

    'theme' => 'bootstrap-5',

    /*
    |--------------------------------------------------------------------------
    | Optional prefix for routes
    |--------------------------------------------------------------------------
    |
    | Assigns some optional prefix for the dynamically generated routes from the 
    | configuration file.
    |
    */

    'prefix' => 'select2',

    /*
    |--------------------------------------------------------------------------
    | Middleware for ajax routes
    |--------------------------------------------------------------------------
    |
    | Assign the middleware through which you need your requests to pass in each call
    |
    */

    'middleware' => ['web'],

    /*
    |--------------------------------------------------------------------------
    | Config and publish ajax route
    |--------------------------------------------------------------------------
    |
    | Here you can specify your route settings to make the ajax calls, assigning 
    | the controller, URL patch, name and method to whatever the field calls, via 
    | an alias.
    | 
    |
    */

    'routes' => [
        //
    ],

    /*
    |--------------------------------------------------------------------------
    | Validation
    |--------------------------------------------------------------------------
    |
    | You can create your own template to display an error message for validations
    | issued by livewire, the element will be created after the input.
    |
    */

    'error_template' => '<span class="invalid-feedback" role="alert"><strong>:message</strong></span>',
];

    'routes' => [
        'alias' => [
          'uri' => '/list-select2',
          'controller' => \App\Http\Controllers\ExampleController::class,
          'method' => 'listSelect2',
          'name' => 'list.select2',
          'placeholder' => 'Select one',
          'showIdOption' => false,
          'defaultValue' => null
        ];
    ],

    use Livewire\Select2\Traits\LivewireSelect2;
    
    class SomeAwesomeComponent extends Component
    {
        use LivewireSelect2;

    public function mount()
    {
        $this->select2Field([
            'field' => 'alias'
        ]);
    }

    public function someMehotdAfterInit()
    {
        $this->loadSelect2();
    }

    use LivewireSelect2;

    public function listSelect2(Request $request)
    {
        try {
            $data = $request->all();

            switch ($data['queryParams']) {
                case 'case1':
                    $extraQuery = function ($query) {
                        $query->someScope()
                            ->whereNotIn('asociate.column', ['someValue'])
                            ->orderBy('another_asociate.column', 'asc');
                    };
                    break;
                default:
                    $extraQuery = function ($query) {
                        $query->someScopeByDefault();
                    };
                    break;
            }

            return $this->select2PaginateList(Model::class, $data['q'], $extraQuery, $data['paginate'], $data['initialValue']);
        } catch (\Throwable $th) {
            return response()->json(['success' => false, 'message' => $th->getMessage(), 'error' => $th->getMessage()], 500);
        }
    }

  public $select2filter = [
       'id' => 'table.id',
       'text' => 'table.description',
       'filter' => 'table.another_column_filter_on_serch',
       'filter2' => 'table.another_column_filter_on_serch',
  ];

    $someModel = Model::find($this->ubicacion_cliente);

    if ($someModel) {
      $this->select2Field['someField']['queryParams']['anotherSelect2FieldValue'] = $this->anotherSomeField;

      $this->select2Field['someField']['defaultValue'] = [
        'id' => $someModel->id,
        'text' => $someModel->description
      ];
    } else {
      $this->select2Field['someField']['defaultValue'] = null;
    }

    $this->loadSelect2();

  $this->validate();

  $this->resetErrorBagSelect2();