Anthony Shoshi Gomes
Anthony Shoshi Gomes

Reputation: 73

How can I get some specific columns name from laravel table. (not all columns name)

I need to get columns name from laravel table. but I get all columns name. How can I get some specific columns name that I need from this table?

I have already tried below code to get all columns name:

$columnName = DB::getSchemaBuilder()->getColumnListing('table');

It returns ["id","name","email","phone_number","created_at","updated_at"] But I am looking for only ["name","email","phone_number"] not all("id","created_at","updated_at") columns. That's what I called specific columns name.

Upvotes: 4

Views: 607

Answers (1)

dparoli
dparoli

Reputation: 9161

You can use the filter() function of laravel collection, i.e.:

$columnNames = collect(DB::getSchemaBuilder()->getColumnListing('table'));
$columnNames = $columnNames->filter(function ($value, $key) {
    return in_array($value, ['id', 'created_at', 'updated_at']) === false;
});

Upvotes: 2

Related Questions