Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Ligonsker's avatar

How to forget a column from a collection returned by a query?

Hi,

How can I remove specified columns from a collection after using Query Builder's get()?

For example, I have a query:

$collection = DB::table('table')->get();

And I get a collection of all the columns:

Illuminate\Support\Collection {
  #items: array:100 [
    0 => {
        "id": "1"
        "first_name": "John"
        "last_name": "Doe"
    }
    1 => {
       ...
    }
    2 => {
      ...
    }
    ...
  ]
}

I use the same query in several pages. In some pages I need all the columns, but in some I don't need all, like in the above collection in some pages I don't want the id column, only first_name and last_name.

The forget() method doesn't work, I think because it's nested and the forget() tries to go by the numeric keys

How can I do that? So that my end collection would be:

Illuminate\Support\Collection {
  #items: array:100 [
    0 => {
        "first_name": "John"
        "last_name": "Doe"
    }
    1 => {
       ...
    }
    2 => {
      ...
    }
    ...
  ]
}

thanks

0 likes
11 replies
tisuchi's avatar

@ligonsker You can try this:


$collection = DB::table('table')->get();

$modifiedCollection = $collection->transform(function ($item) {
    unset($item->id);  // If your items are objects
    // or
    // unset($item['id']);  // If your items are arrays

    return $item;
});
1 like
tykus's avatar

Personally, I would fetch whatever columns you want for the individual pages; the database query will be more efficient that looping over the entire collection in PHP.

1 like
gych's avatar

Why not use select with separate query for each page ?

$collection = DB::table('table')->select(['first_name', 'last_name'])->get();
1 like
Ligonsker's avatar

You guys are right I should use select, it's just that I am trying to reuse the same function that does the data fetching. But the data I need varies slightly for example when I want to export to excel and I don't need all the columns.

So perhaps I can add a condition in that function:

// in the controller
public function fetch_data($select = '*')
{
   return DB::table->select($select)->get();
}

And then if I want to change the columns, I can do:

public function download_data(Request $request)
{
   $select = 'first_name, last_name';
   $collection = $this->fetch_data($select);
  
  // continue . .
}

Would that be good?

tykus's avatar

@Ligonsker for this very simple example, I don't know that abstraction offers any benefits over directly querying the database. If your query is more complex, then maybe there is a benefit.

Otherwise, I would suggest passing an array of individual columns names rather than an arbitrary string:

// in the controller
public function fetch_data(array $select = ['*'])
{
   return DB::table->select($select)->get();
}
public function download_data(Request $request)
{
   $select = [
        'first_name',
        'last_name',
    ];
   $collection = $this->fetch_data($select);

It will be clearer to reason about the columns selection(s) IMHO

Ligonsker's avatar

@tykus Wow you read my mind, I was just about to ask if I can pass an array to the select() clause instead of using a string 😅

Snapey's avatar

what are you saving by dropping the odd column? The memory has already been used.

1 like
Ligonsker's avatar

@Snapey I need to drop it when using the excel export packages so not to show all the columns in the excel file for example

But perhaps I should do as others suggested and select only what's needed? And pass the desired columns to the function as I did in the above example? I want to avoid repeating the same query, and also in reality I also get some more query parameters to filter out the results so it means copying almost the exact same function

Snapey's avatar

@Ligonsker I almost always map the columns within the export so its never an issue

Snapey's avatar

@Ligonsker bear in mind that if you want related data you are going to need the primary key

amitsolanki24_'s avatar

If you want only two column you can go with pluck() method.

->pluck('firstname', 'lastname');

Please or to participate in this conversation.