0
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

1
  • first thing is that in your create() method you dont need to call User::create() because you already called it in modelData() and second your modelData() is doing all the function to create the user and sync.So just call the $this->modelData(); instead of User::create($this->modelData()); or rename your modelData() method to create() Commented Jan 15, 2021 at 9:38

1 Answer 1

2

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

Sign up to request clarification or add additional context in comments.

3 Comments

hey thanks for respons and i do what you say but error like this 'TypeError Argument 1 passed to Illuminate\Database\Eloquent\Builder::create() must be of the type array, null given, called in' but users is added @Claymore
weird! did you defined all the properties? Can i see your controller and blade template ?
Without seeing your error it is difficult to figure out. I supposed you already define belongstomany relationship in roles and user model then created pivot table. Now in user component class(app/http/livewire), you should define public property roles as array and in your user create method, after creating user you should sync or attach roles to that user. In resources/view/livewire in your user create view on select you should bind roles property. Everything should work fine.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.