Foreing keys are being set to NULL, using a Belongs to many relationship
Steps to reproduce the behavior:
Not being set to null

The database

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.

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.
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
insertUpdateDatamethod to this controller and renamed it to insertUpdateData2 and used it in store method and addbefore