Abiyyu
Abiyyu

Reputation: 1

Laravel Livewire sync($request->input)

public function modelData()
{
    $user = User::create([
        'name' => $this->name,
        'npm' => $this->npm,
        'email' => $this->email,
        'jurusan' => $this->jurusan,
        'fakultas' => $this->fakultas,
        'password' => Hash::make($this->password),

    ]);
    $user->roles()->sync($this->input('roles', []));
}

this is my code. please someone help me how to use request -> input in livewire

class Usermanajemen extends Component
{
    public $role = [];
    public $users;
    public $name;
    public $npm;
    public $email;
    public $password;
    public $jurusan;
    public $fakultas;
    use WithPagination;
    public $modalFormVisible = false;
    public $modelid;

    public function render()
    {
        $this->users = User::orderBy('created_at', 'DESC')->get();
        $roles = Role::pluck('title', 'id');
        return view('livewire.usermanajemen', compact('roles'));
    }

    public function create()
    {
        $this->validate();
        User::create($this->modelData());
        $this->modalFormVisible = false;
        $this->reset();
    }

    public function closeModal()
    {
        $this->modalFormVisible = false;
    }

    public function createShowModal()
    {
        $this->resetValidation();
        $this->reset();
        $this->modalFormVisible = true;
    }

    public function mount()
    {

        $this->resetPage();
    }

   
    /**
     * The update function.
     *
     * @return void
     */

    public function rules()
    {
        return [
            'name' => ['required', 'string', 'max:255'],
            'npm' => ['required', 'numeric', Rule::unique('users', 'npm')->ignore($this->modelid)],
            'email' => ['required', 'string', 'email', 'max:255', 'unique:users'],
            'password' => ['required', 'min:6'],
            'jurusan' => 'required',
            'fakultas' => 'required',
        ];
    }

/** * The data for the model mapped * in this component. * * @return void */

public function modelData()
    {
        $user = User::create([
            'name' => $this->name,
            'npm' => $this->npm,
            'email' => $this->email,
            'jurusan' => $this->jurusan,
            'fakultas' => $this->fakultas,
            'password' => Hash::make($this->password),

        ]);
        $user->roles()->sync($this->role);
    }
}

this is my livewire usermanajemen i change my role in create and add role=[]in public

   <form>
            <div class="bg-white px-4 pt-5 pb-4 sm:p-6 sm:pb-4">
                <div class="">
                    <div class="mb-4">
                        <label for="name" class="block text-gray-700 text-sm font-bold mb-2">Nama:</label>
                        <input type="text" class="shadow appearance-none border rounded w-full py-2 px-3 text-gray-700 leading-tight focus:outline-none focus:shadow-outline" id="name" wire:model="name">
                        @error('name') <span class="text-red-500">{{ $message }}</span>@enderror
                    </div>
                    <div class="mb-4">
                        <label for="npm" class="block text-gray-700 text-sm font-bold mb-2">Npm:</label>
                        <input type="text" class="shadow appearance-none border rounded w-full py-2 px-3 text-gray-700 leading-tight focus:outline-none focus:shadow-outline" id="npm" wire:model="npm">
                        @error('npm') <span class="text-red-500">{{ $message }}</span>@enderror
                    </div>
                    <div class="mb-4">
                        <label for="email" class="block text-gray-700 text-sm font-bold mb-2">Email:</label>
                        <input type="email" class="shadow appearance-none border rounded w-full py-2 px-3 text-gray-700 leading-tight focus:outline-none focus:shadow-outline" id="email" wire:model="email">
                        @error('email') <span class="text-red-500">{{ $message }}</span>@enderror
                    </div>
                    <div class="mb-4">
                        <label for="password" class="block text-gray-700 text-sm font-bold mb-2">Password:</label>
                        <input type="password" class="shadow appearance-none border rounded w-full py-2 px-3 text-gray-700 leading-tight focus:outline-none focus:shadow-outline" id="password" wire:model="password">
                        @error('password') <span class="text-red-500">{{ $message }}</span>@enderror
                    </div>
                    <div class="mb-4">
                        <label for="jurusan" class="block text-gray-700 text-sm font-bold mb-2">Jurusan:</label>
                        <input type="text" class="shadow appearance-none border rounded w-full py-2 px-3 text-gray-700 leading-tight focus:outline-none focus:shadow-outline" id="jurusan" wire:model="jurusan">
                        @error('jurusan') <span class="text-red-500">{{ $message }}</span>@enderror
                    </div>
                    <div class="mb-4">
                        <label for="fakultas" class="block text-gray-700 text-sm font-bold mb-2">Fakultas:</label>
                        <input type="text" class="shadow appearance-none border rounded w-full py-2 px-3 text-gray-700 leading-tight focus:outline-none focus:shadow-outline" id="fakultas" wire:model="fakultas">
                        @error('fakultas') <span class="text-red-500">{{ $message }}</span>@enderror
                    </div>
                    <div class="px-4 py-5 bg-white sm:p-6">
                        <label for="role" class="block font-medium text-sm text-gray-700">Roles</label>
                        <select wire:model = "role" name="role" id="role" class="form-multiselect block rounded-md shadow-sm mt-1 block w-full" multiple="multiple">
                            @foreach($roles as $id => $role)
                                <option value="{{ $id }}"{{ in_array($id, old('roles', [])) ? ' selected' : '' }}>{{ $role }}</option>
                            @endforeach
                        </select>
                        @error('roles')
                            <p class="text-sm text-red-600">{{ $message }}</p>
                        @enderror
                    </div>
                </div>
            </div>

this is my blade file for create i change in my livewire and my blade and still error but users added

Upvotes: 0

Views: 1635

Answers (1)

Claymore
Claymore

Reputation: 130

Why do you want to use request ?

You can add a property like public roles = []; in the class

and bind this property with select/checkbox ie: <select wire:model="role" multiple>

and then you can use sync like this : $user->roles()->sync($this->roles);

PS: I assumed that you already defined many to many relationship

Upvotes: 2

Related Questions