It is required to select a value for foreign key even if it is nullable, first value from parent table always selected in relation select dropdown.
It is not possible to remove all value for a many to many relation even if it is nullable. Almost one value must exists saving the master table.
Regards
any update on this? or at least a workaround?
I wrote a quick 'n dirty JavaScript workaround until this gets fixed:
var nullable_relations = [
'categories.parent_category_id',
'users.neighbourhood_id'
];
nullable_relations.forEach(function (relation_key) {
[table, field] = relation_key.split('.');
var select_item = jQuery('body.voyager.'+table+' [name='+field+']');
// Add the "None" option
select_item.prepend(
jQuery("<option></option>")
.attr('value','')
.text('None')
);
// Select it when editing an item that has a null relation
if (jQuery('body.voyager.'+table+' [name='+field+'] option:selected').attr('selected') === undefined) {
select_item.val('').change();
}
});
Change the nullable_relations array according to your needs.
Save it as public/js/nullable_relationship_hack.js
Then add it in config/voyager.php under additional_js:
'additional_js' => [
'js/nullable_relationship_hack.js',
],
@emptynick i think #2931 is not related with this issue,
problem here is to allow NULL value in belongsTo relationships dropdown , the fix you referenced is about belongsToMany .
It is not possible to remove all value for a many to many relation even if it is nullable. Almost one value must exists saving the master table.
In the original issue.
you're right, i did not see that part, sorry.
btw the title and the first part is related to belongsTo relation :
It is required to select a value for foreign key even if it is nullable, first value from parent table always selected in relation select dropdown.
this is still true,
so the issue is partialy solved (and should have been divided in two).
i guess the best way to deal with it is to open a new issue ?
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 wrote a quick 'n dirty JavaScript workaround until this gets fixed:
Change the nullable_relations array according to your needs.
Save it as public/js/nullable_relationship_hack.js
Then add it in config/voyager.php under additional_js: