Our website is made possible by displaying online advertisements to our visitors. Please consider supporting us by whitelisting our website.

How to get Column Name from a table in Laravel and PHP

You can get the column name through a raw query. There are other ways too and we will discuss all.

// Through raw query
$query = “SHOW COLUMNS FROM $table_name”;
$data[‘columns’] = \DB::select($query);

It will return the total array of everything.

And here is the magic one.

// Through Schema Facade

$columns = \Schema::getColumnListing(‘table_name’);

or

// Through DB Facade
$columns = \DB::getSchemaBuilder()->getColumnListing(‘table_name’);
// or
$columns = \DB::connection()->getSchemaBuilder()->getColumnListing(“table_name”);

Take a look at the following posts..

How to Run raw insert query or SQL file in Laravel seeder

How to find a column or field name of a table from an entire database?


Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Wordpress Social Share Plugin powered by Ultimatelysocial