【问题标题】:Laravel Livewire sync($request->input)Laravel Livewire 同步($request->input)
【发布时间】:2023-03-27 07:42:01
【问题描述】:
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', []));
}

这是我的代码。请有人帮我如何使用请求->在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',
        ];
    }

/** * 模型映射的数据 * 在这个组件中。 * * @return 无效 */

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);
    }
}

这是我的 livewire usermanajemen 我在创建中更改了我的角色并在公共中添加角色=[]

   <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>

这是我用于创建的刀片文件 我更换了我的 livewire 和我的刀片,但仍然出现错误,但用户添加了

【问题讨论】:

  • 第一件事是在您的create() 方法中您不需要调用User::create() 因为您已经在modelData() 中调用了它,其次您的modelData() 正在执行创建用户的所有功能和同步。所以只需调用$this-&gt;modelData(); 而不是User::create($this-&gt;modelData()); 或将您的modelData() 方法重命名为create()

标签: laravel laravel-livewire


【解决方案1】:

为什么要使用请求?

您可以在

中添加public roles = [];之类的属性

并将此属性与选择/复选框绑定,即:&lt;select wire:model="role" multiple&gt;

然后你可以像这样使用同步:$user-&gt;roles()-&gt;sync($this-&gt;roles);

PS:我假设你已经定义了多对多关系

【讨论】:

  • 嘿,谢谢你的回复,我照你说的做,但是像这样的错误'TypeError Argument 1 pass to Illuminate\Database\Eloquent\Builder::create() must be of type array, null given,调用'但添加了用户@Claymore
  • 奇怪!你定义了所有的属性吗?我可以看看你的控制器和刀片模板吗?
  • 没有看到你的错误很难弄清楚。我想你已经在角色和用户模型中定义了 belongstomany 关系,然后创建了数据透视表。现在在用户组件类(app/http/livewire)中,您应该将公共属性角色定义为数组,并在您的user create 方法中,创建用户后您应该为该用户提供syncattach 角色。在您的用户中的资源/视图/livewire 中,选择您应该绑定角色属性。一切都应该正常。
猜你喜欢
  • 2021-09-13
  • 1970-01-01
  • 2022-01-27
  • 1970-01-01
  • 2021-05-02
  • 1970-01-01
  • 2016-04-18
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多