背包Crud具有克隆功能。但当我们的表具有唯一字段列时,它不起作用。 Backpack clone Documentation 当表有唯一列时如何克隆它?
public function clone($id)
{
$this->crud->hasAccessOrFail('clone');
$this->crud->setOperation('clone');
$clonedEntry = $this->crud->model->findOrFail($id)->replicate();
return (string) $clonedEntry->push();
}
最佳答案
最近我也遇到了同样的问题。这是我的解决方案:
public function clone($id)
{
$this->crud->hasAccessOrFail('clone');
$this->crud->setOperation('clone');
$clonedEntry = $this->crud->model->findOrFail($id)->replicate();
// now resolve the value for unique attribute before save. e.g.
$slug = Str::slug($clonedEntry->name, '-');
$count = $this->crud->model->whereRaw("slug RLIKE '^{$slug}(-[0-9]+)?$'")->count();
$clonedEntry->slug = $count ? "{$slug}-{$count}" : $slug;
// when you are done, save changes
return (string) $clonedEntry->push();
}
您没有指定有关“唯一”属性的任何内容。您可以根据您的需求随意定制解析器。
关于laravel-backpack - 如何利用背包克隆功能打造独特的 field 形态,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61007310/