保存前如何根据其他字段值设置字段的值

时间:2019-04-11 00:11:24

标签: laravel model field backpack-for-laravel

我有一个名为“ students”的表,该表具有first_name,last_name和full_name列。我希望在将值保存到数据库之前使用first_name .' '. last_name设置full_name的值。

public function store(StoreRequest $request)是最好的地方,所需的代码是什么?

        $this->crud->addField([
        'name' => 'first_name',
        'type' => 'text',
        'label' => "First Name",
        'tab' => 'Details'
        ]);
        $this->crud->addField([
        'name' => 'last_name',
        'type' => 'text',
        'label' => "Last Name",
        'tab' => 'Details'
        ]);

        $this->crud->addField([
        'name' => 'full_name',
        'type' => 'text',
        ]);



        // add asterisk for fields that are required in StudentRequest
        $this->crud->setRequiredFields(StoreRequest::class, 'create');
        $this->crud->setRequiredFields(UpdateRequest::class, 'edit');
        $this->crud->allowAccess('show');

    }

    public function store(StoreRequest $request)
    {
        // your additional operations before save here

        $redirect_location = parent::storeCrud($request);
        // your additional operations after save here
        // use $this->data['entry'] or $this->crud->entry
        return $redirect_location;
    }

谢谢。

1 个答案:

答案 0 :(得分:1)

执行此操作的正确方法之一是使用laravel雄辩的模型事件。

您可以使用from unknown error: Runtime.executionContextCreated has invalid 'context': {"auxData":{"frameId":"095D324EF0DD4E5E07F5C4EFDE116757","isDefault":true,"type":"default"},"id":1,"name":"","origin":"://"} (Session info: chrome=73.0.3683.86) (Driver info: chromedriver=2.22.397933 (1cab651507b88dec79b2b2a22d1943c01833cc1b),platform=Windows NT 10.0.17134 x86_64) 模型的creating事件。 Student事件在将模型保存到数据库之前发生。

您可以在creating AppServiceProvider函数中绑定学生模型的创建事件

AppServiceProvider

boot