Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 565
Anthony Shoshi Gomes Avatar asked Oct 27 '25 23:10

Anthony Shoshi Gomes


1 Answers

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;
});
like image 145
dparoli Avatar answered Oct 30 '25 15:10

dparoli