What seems to be the problem:
My select input doesn’t send any value to a CRUD component
Steps to Reproduce:
This is my livewire CRUD component.
namespace App\Http\Livewire; use App\Models\Category; use Livewire\Component; use App\Models\Post; use Livewire\WithPagination; use Livewire\WithFileUploads; class Posts extends Component { public $title, $categories, $category_id, $body, $post_id, $search, $img; public $isOpen = 0; use WithFileUploads; use WithPagination; public function mount() { $this->categories = Category::all(); } public function render() { $search = '%' . $this->search . '%'; $posts = Post::where('title', 'LIKE', $search) ->orWhere('body', 'LIKE', $search) ->latest()->paginate(5); return view('livewire.posts.posts', ['posts' => $posts]); } public function create() { $this->resetInputFields(); $this->openModal(); } public function openModal() { $this->isOpen = true; } public function closeModal() { $this->isOpen = false; } private function resetInputFields() { $this->category_id = ''; $this->title = ''; $this->body = ''; $this->post_id = ''; } public function store() { $this->validate([ 'category_id' => 'required', 'title' => 'required', 'body' => 'required', 'img' => 'image|max:1024' ]); Post::updateOrCreate( ['id' => $this->post_id], ['category_id' => $this->category_id, 'title' => $this->title, 'body' => $this->body, 'img' => $this->img->hashName(), ]); if(!empty($this->img)) { $this->img->store('public/docs'); } session()->flash('message', $this->post_id ? 'Пост успешно обновлен.' : 'Пост успешно создан.'); $this->closeModal(); $this->resetInputFields(); } public function edit($id) { $post = Post::findOrFail($id); $this->category_id = $post->category_id; $this->post_id = $id; $this->title = $post->title; $this->body = $post->body; $this->img = $post->img; $this->openModal(); } public function delete($id) { Post::find($id)->delete(); session()->flash('message', 'Пост успешно удален.'); } }
This is a select input from my blade view ‘create new post’.
<select class="form-select" aria-label="Default select example">
<option selected>Выберите категорию</option>
@foreach($categories as $category)
<option wire:modal="category_id" value="{{ $category->id }}">
{{ $category->name }}
</option>
@endforeach
@error('category_id') <span class="text-red-500">{{ $message }}</span>@enderror
</select>
Even my validation doesn’t work. I know exactly, that the problem is in select input, cuz my CRUD component worked perfectly before I added categories there. Please, help me
Are you using the latest version of Livewire:
yes