get column names from table laravel code example

Example 1: get table column names sql laravel

use Illuminate\Support\Facades\Schema;

use Illuminate\Support\Facades\DB;

public function getTableColumns($table)
{
    return DB::getSchemaBuilder()->getColumnListing($table);

    // OR

    return Schema::getColumnListing($table);

}

Example 2: laravel get column field name

// You may need to run
composer require doctrine/dbal
// Retrieve laravel column type
$columnType = DB::getSchemaBuilder()->getColumnType($tableName, $colName)
// for example
$columnType = DB::getSchemaBuilder()->getColumnType('user', 'age')

Tags:

Sql Example