Search code examples
phplaravellaravel-8database-migration

In Laravel migrations what is the parameter in ->nullable($value = true) for?


I came across a migration that uses this syntax: ->nullable('true'); which seems strange, what is that parameter for? I can't find a description for it online. All I know is that it is called $value.


Solution

  • Basically nullable() has a default parameter of true

    $table->string('nickname')->nullable();
    

    It is the same as

    $table->string('nickname')->nullable(true);
    

    And thus, it is not needed to add the parameter in this case.


    A common question/confusion is why have this parameter at all? Would we ever ->nullable(false); since that is the same as just not writing ->nullable() at all.

    Imagine a case where you had a nickname on the user, and you want to change this nickname field from nullable to not nullable. The original migration might have looked like the ones above, and to change it, we could, for explicitness of intent, do the following:

    $table->string('nickname')->nullable(false)->change();