Voyager: Allow NULL in select dropdown for relation

Created on 30 Oct 2017  路  7Comments  路  Source: the-control-group/voyager

  • Laravel Version: 5.5.19
  • Voyager Version: 1.0.6
  • PHP Version: 7.0
  • Database Driver & Version: Mysql 5.6

Description:

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

Steps To Reproduce:

Most helpful comment

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();
    }
});
  1. Change the nullable_relations array according to your needs.

  2. Save it as public/js/nullable_relationship_hack.js

  3. Then add it in config/voyager.php under additional_js:

'additional_js' => [
    'js/nullable_relationship_hack.js',
],

All 7 comments

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();
    }
});
  1. Change the nullable_relations array according to your needs.

  2. Save it as public/js/nullable_relationship_hack.js

  3. 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.

Was this page helpful?
0 / 5 - 0 ratings