提问人:Danish 提问时间:11/17/2023 更新时间:11/17/2023 访问量:26
Laravel Livewire 在尝试更新记录时给出空白 iframe
Laravel Livewire gives blank iframe when attempt to update record
问:
我正在使用引导模型来添加和更新数据。当更新时数据没有变化时,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');
}
}
我使用了多种方法,例如质量分配。还添加了$fillable。在某些方法中,我没有收到任何错误,但它不会更新记录。
答:
0赞
Danish
11/17/2023
#1
在更新方法中,我更改了
$category = ModelsCategory::where('id', $this->categoryId)->first();
自
$category = ModelsCategory::where('id', $this->category->id)->update($validated);
不知道为什么,但链接更新方法有效。谁能解释一下为什么?
评论
0赞
ceejayoz
11/17/2023
如果你也这样做,第一行有效吗?很有可能是 ,不在模型中。$this->category->id
$this->category_id
$this->categoryId
0赞
Danish
11/18/2023
@ceejayoz是的,我试过了,但没有帮助。当我链接更新方法时,它起作用了
1赞
ceejayoz
11/18/2023
我的猜测是你有多行匹配你的;意志意味着你只对其中一个采取行动,意志对所有行动都起作用。where
first()
update()
评论