我正在使用引导模型来添加和更新数据。当数据在更新时没有变化时,livewire工作正常。但是当我尝试更新数据时,它给出了一个空白的黑色iframe屏幕。
下面是我的代码更新代码:
<?php
namespace App\Livewire\Admin;
use App\Models\Category as ModelsCategory;
use Illuminate\Database\Eloquent\Collection;
use Illuminate\Support\Facades\DB;
use Livewire\Attributes\Title;
use Livewire\Component;
use Livewire\WithFileUploads;
#[Title('Manage Categories')]
class Category extends Component
{
use WithFileUploads;
public $categories = [], $name, $description, $image, $categoryId;
protected $rules = [
'name' => ['required', 'string', 'unique:categories,name', 'max:50'],
'description' => ['required', 'string', 'max:5000'],
'image' => ['required', 'image', 'mimes:jpg,jpeg,png,webp', 'max:1024'],
];
protected function get(): Collection
{
return ModelsCategory::with('media')->orderByDesc('id')->get(['id', 'name', 'description']);
}
public function render()
{
$this->categories = $this->get();
return view('livewire.admin.category', ['categories' => $this->categories]);
}
public function store()
{
$validated = $this->validate();
$category = ModelsCategory::create($validated);
$category->addMedia($this->image)->toMediaCollection('images');
$this->dispatch('closeAddModel');
session()->flash('success', 'Category has been added');
}
public function edit(ModelsCategory $category)
{
$this->categoryId = $category->id;
$this->name = $category->name;
$this->description = $category->description;
$this->dispatch('openEditModel');
}
public function update()
{
$validated = $this->validate([
'name' => ['required', 'string', 'unique:categories,name,' . $this->categoryId, 'max:50'],
'description' => ['required', 'string', 'max:5000'],
]);
$category = ModelsCategory::where('id', $this->categoryId)->first();
$category->update([
'name' => $this->name,
'description' => $this->description,
]);
if ($this->image) {
$category->clearMediaCollection('images');
$category->addMedia($this->image)->toMediaCollection('images');
}
$this->reset();
$this->dispatch('closeEditModel');
session()->flash('success', 'Category has been updated');
}
}
字符串
我使用了多种方法,比如mass assignment。还添加了$fillable。在某些方法中,我不会得到任何错误,但它不会更新记录。
1条答案
按热度按时间oknwwptz1#
在更新方法中,我改变了
字符串
到
型
不知道为什么,但链接更新方法工作。有人能解释为什么吗?