laravel-backpacklaravel-backpack-5

How can I override the store function in backpack-for-laravel v5?


I've updated my app from backpack-for-laravel v4.1 to v5, but now I'm having issues with overriding the store function. I followed the upgrade guide and step 15: https://backpackforlaravel.com/docs/5.x/upgrade-guide#step-15

but unfortunately, my preferred option A is not working for me. this is how my function looked in v4.1:

public function store()
{
    // do something before validation, before save, before everything
    $this->crud->setOperationSetting('saveAllInputsExcept', ['save_action']); //somehow required...
    $end = strtotime('+1 hour', strtotime($this->crud->getRequest()->request->get('start'))); //set end automatically after 1h  
    $this->crud->getRequest()->request->add(['end'=> $end]);        
    
    $response = $this->traitStore();
    // do something after save
    return $response;
}

I've converted it now to match the v5 requirements as below:

public function store()
{
    $this->crud->set('strippedRequest', function($request) {
        $end = '2023-02-01 14:00:00';
        $request->add([ 'end' => $end ]);
        return $request->except(['_save_action', '_token', '_http_referrer']);
    });

    return $this->traitStore();
}

I even tried it with a static value, but I keep getting the error from SQL that the field 'end' doesn't have a default value...meaning no value for end has been added to the request.

Appreciate any kind of support. Thanks


Solution

  • Thanks for the question. Indeed there is an issue in the upgrade guide example.

    I've just submitted a PR to docs to fix it: https://github.com/Laravel-Backpack/docs/pull/404

    Should be merged soon!

    You can see the actual working solution in the PR diff.

    Cheers