Someone
Someone

Reputation: 904

Laravel Backpack : Storing Belongs To Many relationships using custom view

I have a flight class and this flight has a custom view field like so:

enter image description here

This represents a belongs to many relationship which stores website_id / flight_id and pricing as pivot data in a pivot table.

The custom view uses JS to send this data back to the controller in this format:

{"1":{"price_adult":"434","price_child":"545"},"2":{"price_adult":"323","price_child":"324"},"3":{"price_adult":"434","price_child":"43"}}

Trying to send this data with the request doesn't create the relations fields, and because I do not have a flight ID at the point of creating this within the controller I can not loop this JSON to make the relations manually.

Can anyone point out what the best course of action is or if there is support for this? I took a look at the docs but they are woefully short and patchy in terms of being much help.

EDIT: I should have said I can probably make this work using a custom name attribute on the model for the relation, then add a set mutator to loop this data and update the prices relation but I don't want to go down this route if there is support for this I am missing out of the box in backpack.

EDIT2:

Someone asked about the relation:

$this->belongsToMany(Website::class, 'website_pricing')->withPivot('price_adult', 'price_child');

This is working fine its not a problem with the relation working its how can I get backpack to store the data as a relation when the flight has no ID yet, or how can I pass the data I posted above in such a way that the backpack crud controller can handle it?

Upvotes: 0

Views: 1952

Answers (2)

Someone
Someone

Reputation: 904

Basically thought I should post what I did because no one could provide an answer to this.

So basically you have to copy the store / update functions from the parent, changing a few lines.

    $this->crud->hasAccessOrFail('create');

    // fallback to global request instance
    if (is_null($request)) {
        $request = \Request::instance();
    }

    // replace empty values with NULL, so that it will work with MySQL strict mode on
    foreach ($request->input() as $key => $value) {
        if (empty($value) && $value !== '0') {
            $request->request->set($key, null);
        }
    }

    // insert item in the db
    $item = $this->crud->create($request->except(['save_action', '_token', '_method']));
    $this->data['entry'] = $this->crud->entry = $item;

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

    // save the redirect choice for next time
    parent::setSaveAction();

    return parent::performSaveAction($item->getKey());

Basically any line which references a function in the parent class using $this->method needs to be changed to parent::

This line is what I used to submit the relations JSON string passed to the controller as relations $item->prices()->sync(json_decode($request->input('prices'), true));

This is done after the line containing $item = $this->crud->create as the item id that just got stored will be available at that point.

Upvotes: 0

Raza Mehdi
Raza Mehdi

Reputation: 941

You may need to create a flight first, if no flight id is being provided. Can you explain the database relational structure more?

Upvotes: 0

Related Questions