如何从选择字段中获取值



我有一个livewire CRUD组件来发布帖子。在我尝试添加类别选择之前,它运行得很好。这是一段代码,我试图在其中选择一个类别:

<select class="form-select" aria-label="Default select example">
<option wire:model="category_id" selected>Выберите категорию</option>
@foreach($categories as $category)
<option value="{{ $category->id }}">
{{ $category->name }}
</option>
@endforeach
@error('category_id') <span class="text-red-500">{{ $message }}</span>@enderror
</select>

我可以显示所有类别并选择一个,但我无法在组件中获取它。我尝试使用复选框元素。无论我是否选择了一个类别,我甚至都得到了验证。选择字段根本没有经过验证。我可以从数据库中输出所有类别。

这是我的CRUD组件。

<?php
namespace AppHttpLivewire;
use AppModelsCategory;
use LivewireComponent;
use AppModelsPost;
use LivewireWithPagination;
use LivewireWithFileUploads;
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])->layout('layouts.app');
}
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', 'Пост успешно удален.');
}
}

为select设置name会解决问题吗?

<select class="form-select" aria-label="Default select example" name="category_id">
<option wire:model="category_id" selected>Выберите категорию</option>
@foreach($categories as $category)
<option value="{{ $category->id }}">
{{ $category->name }}
</option>
@endforeach
@error('category_id') <span class="text-red-500">{{ $message }}</span>@enderror
</select>

目前,框架似乎无法识别该输入的名称

相关内容

  • 没有找到相关文章

最新更新