2

In my PageTemplates.php I have a field like this:

$this->crud->addField([
    'name' => 'adres',
    'label' => 'Adres',
    'type' => 'address',
    'fake' => true,
]);

Now I would like to save also the latitude and longitude of the address they give in (if it can be found). I've copied the PageCrudController and changed the config in config/backpack/pagemanager.php to:

return [
    'admin_controller_class' => 'App\Http\Controllers\Admin\PageCrudController',
    'page_model_class'       => 'App\Models\Page',
];

In my store function I have:

public function store(StoreRequest $request)
{
    $address = $request->request->get('adres');
    $addressObj = app('geocoder')->geocode($address)->get()->first();

    if($addressObj)
    {

    }

    $this->addDefaultPageFields(\Request::input('template'));
    $this->useTemplate(\Request::input('template'));

    return parent::storeCrud();
}

But what do I place in the if statement? How can I add (= set) an extra field to the extras field in my database?

nielsv
  • 6,540
  • 35
  • 111
  • 215

3 Answers3

2

In backpack 4.1, I solved my issue by the following way : Override the store method in my controller, set my extra field in request and then call the backpack store method Don't forget to add include backpack trait Hope the solution will help someone

use \Backpack\CRUD\app\Http\Controllers\Operations\CreateOperation { store as traitStore; }

public function store()
{
    $this->crud->setOperationSetting('saveAllInputsExcept', ['save_action']);
    $this->crud->getRequest()->request->add(['updated_by' => backpack_user()->id]);     
        
    return $this->traitStore();
}
Maamun7
  • 39
  • 1
  • 9
0

Fixed it by doing the following:

Add latitude and longitude as hidden fields:

$this->crud->addField([
    'name' => 'latitude',
    'type' => 'hidden',
    'fake' => true,
]);

$this->crud->addField([
    'name' => 'longitude',
    'type' => 'hidden',
    'fake' => true,
]);

Set attributes by doing the following:

if($addressObj)
    {
        $request['latitude'] = $addressObj->getCoordinates()->getLatitude();
        $request['longitude'] = $addressObj->getCoordinates()->getLongitude();
    }
}

Change parent::updateCrud to parent::updateCrud($request);.

nielsv
  • 6,540
  • 35
  • 111
  • 215
0

For people still looking at this issue, I'd recommend you follow the advice in the note under the Callbacks section of Laravel Backpack's docs if you don't just want to observe changes made from the Backpack admin panel, you just need to create an Observable.

To do this you can do the following:

  1. Create an Observer class: php artisan make:observer YourObserver --model=YourModel

  2. Add your code to the generated event methods you wish to observe.

  3. Register the Observer by calling the observe method on the model you wish to observe in your EventServiceProvider's boot method like so:

    public function boot()
    {
        YourModel::observe(YourObserver::class);
    }
    

Or equally you can register the Observer to the $observers property of your applications' EventServiceProvider class:

protected $observers = [
    YourModel::class => [YourObserver::class],
];
Cu Janeway
  • 91
  • 1
  • 5