【问题标题】:How to save data on different database tables in Laravel Backpack如何在 Laravel Backpack 中将数据保存在不同的数据库表中
【发布时间】:2021-06-10 03:40:14
【问题描述】:

如何将特征字段中的数据保存在不同的表中。下面链接中的示例,它将 Features 表字段保存在数据库的 JSON 字段中。但是,我想将这些特征数据保存到另一个表中。

https://demo.backpackforelavel.com/admin/product/211/Edit

我会回到这里发布我的答案。我设法安顿下来,我来这里是为了帮助其他开发者。

我已经解决了问题的第一部分。但是现在我无法从表单中的 Features 字段中获取数据。

以下是我能够保存和编辑表单数据的源代码。但是,我无法从 Feature 字段中携带数据。有人知道我如何在 Feature 中携带字段数据

class ProductCrudController extends CrudController
{
    use ListOperation;
    use CreateOperation {
        store as traitStore;
    }
    use UpdateOperation {
        update as traitUpdate;
    }
    use DeleteOperation;

    public function store()
    {
        // insert item in the db
        $item = $this->crud->create($this->crud->getRequest()->except(['save_action', '_token', '_method']));

        $features = json_decode($this->crud->getRequest()->input('features'));

        foreach ($features  as $itens) {
            $obj = new Feature();
            $obj->product_id = $item->getKey();
            $obj->name = $itens->name;
            $obj->save();
        }

        // show a success message
        \Alert::success(trans('backpack::crud.insert_success'))->flash();

        return redirect('admin/product');
    }

    public function update()
    {
        $featureForm = json_decode($this->crud->getRequest()->input('features'));

        // insert item in the db
        $item = $this->crud->update($this->crud->getRequest()->id, $this->crud->getRequest()->except(['save_action', '_token', '_method', 'features']));

        $objF = Feature::where('product_id', $item->getKey())->get();

        foreach ($objF as $itens) {
            Feature::where('id', $itens->id)->delete();
        }

        foreach ($featureForm as $itens) {
            $obj = new Feature;
            $obj->product_id = $item->getKey();
            $obj->name = $itens->name;
            $obj->save();
        }

        // show a success message
        \Alert::success(trans('backpack::crud.insert_success'))->flash();

        return redirect('admin/product');
    }
}

我会回到这里发布我的答案。我设法安顿下来,我来这里是为了帮助其他开发者。

我能够在“功能”字段中以表单版本的形式提供数据。我在模型中使用了 Mutators - https://laravel.com/docs/8.x/eloquent-mutators

下面是我做的例子。

在官方文档或演示项目中有一个这样的例子会很有趣。我相信这对其他开发者也有很大帮助。

    //model Product
    public function getFeaturesAttribute($value)
    {
        $objects = Features::where('product_id', $this->id)->get();
        $array = [];
        foreach ($objects as $itens) {
            $obj = new stdClass;
            $obj->name = $itens->name;
            $array[] = $obj;
        }
        return \json_encode($array);
    }

【问题讨论】:

  • 你可以覆盖 store 方法,并保存你想要的方式
  • @OMR 这个问题的第一部分我已经解决了。如您所见,我通过添加您所说的内容来更新疑问。但是现在我无法从表单中的 Features 字段中获取数据。

标签: laravel laravel-backpack


【解决方案1】:

有几种方法可以解决这个问题。就我个人而言,我更喜欢在模型中创建访问器和修改器 - https://laravel.com/docs/8.x/eloquent-mutators。它不仅使 CrudController 保持清洁(如此可更新),而且还允许您从其他表单(例如:面向用户的表单)保存/加载内容。

    //model Product
    public function getFeaturesAttribute($value)
    {
        $objects = Features::where('product_id', $this->id)->get();
        $array = [];
        foreach ($objects as $itens) {
            $obj = new stdClass;
            $obj->name = $itens->name;
            $array[] = $obj;
        }
        return json_encode($array);
    }

【讨论】:

    猜你喜欢
    • 2019-12-20
    • 1970-01-01
    • 1970-01-01
    • 2022-06-10
    • 1970-01-01
    • 2020-09-15
    • 2020-08-25
    • 2020-06-01
    • 2021-03-16
    相关资源
    最近更新 更多