Voyager: Foreing keys are being set to NULL, using a Belongs to many relationship

Created on 10 Jul 2019  路  7Comments  路  Source: the-control-group/voyager

Version information

  • Laravel: v5.8.19
  • Voyager: v1.2
  • PHP: 7.3
  • Database: MySQL 5.7.26

Description

Foreing keys are being set to NULL, using a Belongs to many relationship

Steps To Reproduce

Steps to reproduce the behavior:

  1. Create a Belongs to many relationship
  2. Create child Entity
  3. Foreing keys are set to null even when the request->foreign_key has a value

Expected behavior

Not being set to null

Screenshots

dd

The database
dd2

 public function store(Request $request)
    {
       dd($request->all()); // The screenshot
        $slug = $this->getSlug($request);

        $dataType = Voyager::model('DataType')->where('slug', '=', $slug)->first();

        // Check permission
        $this->authorize('add', app($dataType->model_name));

        // Validate fields with ajax
        $val = $this->validateBread($request->all(), $dataType->addRows)->validate();
        $data = $this->insertUpdateData($request, $slug, $dataType->addRows, new $dataType->model_name());

        event(new BreadDataAdded($dataType, $data)); // I think this might be the problem


        $product_id = Producto::latest()->first()->id;

        if ($request->productos_relacionados) {
            foreach ($request->productos_relacionados as $producto_relacionado) {
                $producto_producto = new ProductoProducto();
                $producto_producto->parent_id = $product_id;
                $producto_producto->producto_id = $producto_relacionado;
                $producto_producto->save();
            }
        }

        return redirect()
            ->route("voyager.{$dataType->slug}.index")
            ->with([
                'message' => __('voyager::generic.successfully_added_new') . " {$dataType->display_name_singular}",
                'alert-type' => 'success',
            ]);
    }

Also I noticed somehitng: If you, for instance, modified or even click the json structure, to a foreign key, even after you select that foreign key,for a Belongs to many relationship, it still appears in the BREAD views. So maybe, this is a BUG. $request->categoria_id has the corresponding calues, but, since I chose to not show that field, Voyager "thinks" that is a null value. I guess.

dd3

Most helpful comment

I was out of time so I fixed it as follows:
Copied as my custom controller and set it in Bread and copied insertUpdateData method to this controller and renamed it to insertUpdateData2 and used it in store method and add

    $data['user_id']=Auth::user()->id;

before

    $data->save();

All 7 comments

I believe you've misunderstood a few things.

Voyager is "smart" with it's relationships. If you have a thing_id field and a BelongsTo relationship for Things, then it will ONLY display the relationship field (the *_id field is rendered hidden). Having said that, the controller method receiving that form submission is not quite as smart. If it doesn't see the *_id field, it doesn't update the database.

So basically, you just need to leave the BREAD checkboxes checked for those *_id fields and they should save your relationships just fine

I was out of time so I fixed it as follows:
Copied as my custom controller and set it in Bread and copied insertUpdateData method to this controller and renamed it to insertUpdateData2 and used it in store method and add

    $data['user_id']=Auth::user()->id;

before

    $data->save();

Assuming this is solved due to lack of response. Closing

It's just a quick fix not the actual fix, you need to add this possibility in the project itself.

Why did you do that? What's the point?

BTW. I solved it, doing what fletch3555 said.

@guillermoprojaslema sorry but I couldn't fix it with that, could you explain more about it?

This issue has been automatically locked since there has not been any recent activity after it was closed. If you have further questions please ask in our Slack group.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

MikadoInfo picture MikadoInfo  路  3Comments

abacram picture abacram  路  3Comments

winex01 picture winex01  路  3Comments

wislem picture wislem  路  3Comments

IvanBohonosiuk picture IvanBohonosiuk  路  4Comments