laravellaravel-11

Get the nullable property of a column using Laravel Schema


Up until Laravel version 10.x, many queries were supplemented with Doctrine DBAL. Starting from Laravel 11, these have been deprecated.

I could easily get column's nullable property using Doctrine DBAL by calling the Column.getNotNull() function. I know I can run my own SQL query for the information; I just want to make sure there is a native alternative to this function that is no longer usable without Doctrine DBAL.

How can I determine whether a column is nullable (or other properties) using only Schema?


Solution

  • Oh, I got it. We have two functions: Schema.getColumnListing and Schema.getColumns. Well, I didn't notice that at first. The getColumns method returns the table columns along with all their important properties.

    Schema::getColumns($tableName);
    

    Result: array of columns

    Column's structure

    [
      "name" => "columnName"
      "type_name" => "integer"
      "type" => "integer"
      "collation" => null
      "nullable" => false        // here a nullable property
      "default" => null
      "auto_increment" => false
      "comment" => null
      "generation" => null
    ]