Mawunyo's avatar

Laravel livewire select2 multiple select

Hi everyone I use select2 multiple select with laravel livewire, when I slick to store data I get this error :

ErrorException implode(): Invalid arguments passed.

But when I remove select2 tag in form select everything is going well.

How can I use select2 multiple select with laravel livewire.

<script>
    $(document).ready(function() {

        $('.Payscouverts2').select2({
          
        }).on('change', function(){
            @this.set('pays_couverts', e.target.value);
        });
    });
</script>
0 likes
4 replies
devingray_'s avatar

This error is due to PHP's implode() function. Can you add the PHP code that you are using on the store method?

1 like
Mawunyo's avatar
<?php

namespace App\Http\Livewire\Partenaires;

use App\Partenaire;
use Livewire\Component;
use Illuminate\Support\Facades\Auth;

class Edit extends Component
{
    public $partenaire_id, $user_id, $nom_societe, $secteur_activite, $pays_presence, $nom_point_focal,
    $phone_point_focal, $email, $honoraires, $commission, $commentaires;

    public $pays_couverts = [];

    public function mount($id){
        $parternaire = Partenaire::find($id);

        $this->partenaire_id = $parternaire->id;
        $this->user_id = $parternaire->user_id;
        $this->nom_societe = $parternaire->nom_societe;
        $this->secteur_activite = $parternaire->secteur_activite;
        $this->pays_presence = $parternaire->pays_presence;
        $this->pays_couverts = $parternaire->pays_couverts;
        $this->nom_point_focal = $parternaire->nom_point_focal;
        $this->phone_point_focal = $parternaire->phone_point_focal;
        $this->email = $parternaire->email;
        $this->honoraires = $parternaire->honoraires;
        $this->commission = $parternaire->commission;
        $this->commentaires = $parternaire->commentaires;
    }

    public function render()
    {
        return view('livewire.partenaires.edit');
    }

    public function update(){
        $this->validate([
            'nom_societe' => 'required',
            'secteur_activite' => 'required',
            'pays_presence' => 'required',
            'nom_point_focal' => 'required',
            'email' => 'required|email',
            'honoraires' => 'required',
            'commission' => 'required',
            'commentaires' => 'required',
        ]);

        $this->pays_couverts = implode(',',   $this->pays_couverts);
        
        $partenaire = Partenaire::find($this->partenaire_id);

        $partenaire->update([
            'user_id' => Auth::user()->id,
            'nom_societe' => $this->nom_societe,
            'secteur_activite' => $this->secteur_activite,
            'pays_presence' => $this->pays_presence,
            'pays_couverts' => $this->pays_couverts,
            'nom_point_focal' => $this->nom_point_focal,
            'phone_point_focal' => $this->phone_point_focal,
            'email' => $this->email,
            'honoraires' => $this->honoraires,
            'commission' => $this->commission,
            'commentaires' => $this->commentaires,
        ]);

        session()->flash('message', 'Fiche partenaire modifiée avec succes.');

        return redirect()->route('partenaires.index');

    }
}
devingray_'s avatar

What is the outcome of this

dd($this->pays_couverts);

For it to be able to work with the implode() it will need to be an array ['example', 'example2']

1 like
Lokesh003Coding's avatar

######## INSIDE LIVEWIRE COMPONENT

public array $locationUsers = [];
protected $listeners = ['locationUsersSelected'];

public function locationUsersSelected($locationUsersValues)
{
  $this->locationUsers = $locationUsersValues;
}

######## INSIDE LIVEWIRE BLADE

<div class="col-md-12 mb-3" wire:ignore>
	<label for="locationUsers">Select Users</label>
	<select id="locationUsers" class="form-control select2" multiple="multiple">
		<option value="">--select--</option>
        @foreach($this->users as $id => $name)
        	<option value="{{ $id }}">{{ $name }}</option>
        @endforeach
	</select>
</div>

######## INSIDE LIVEWIRE SCRIPTS

document.addEventListener('livewire:load', function () {
  $('#locationUsers').on('select2:select', (e) => {
    @this.emit('locationUsersSelected', $('#locationUsers').select2('val'));
  });

  $('#locationUsers').val(@this.get('locationUsers')).trigger('change');
});

Please or to participate in this conversation.