如何使用背包克隆功能来获得独特的字段形式

how to use backpack clone feature for unique field form

Backpack Crud 有克隆功能。但当我们的 table 具有唯一字段列时,它不起作用。 Backpack clone Documentation 当 table 有独特的列时如何克隆它?

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();
}

您没有指定有关 "unique" 属性的任何内容。随意根据您的需要自定义解析器。