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

popcone's avatar

Remove item from collection

My collection

Elasticquent\ElasticquentCollection {#553
  #items: array:1 [
    0 => App\Company {#545
      #connection: null
      #table: null
      #primaryKey: "id"
      #keyType: "int"
      #perPage: 15
      +incrementing: true
      +timestamps: true
      #attributes: array:2 [
        "id" => 1
        "cname" => "Outside New York Style Studio"
      ]
      #original: array:2 [
        "id" => 1
        "cname" => "Outside New York Style Studio"
      ]
      #relations: array:1 [
        "sic" => Illuminate\Database\Eloquent\Collection {#556
          #items: array:1 [
            0 => App\Sic {#555
              #table: "sics"
              #primaryKey: "code"
              #connection: null
              #keyType: "int"
              #perPage: 15
              +incrementing: true
              +timestamps: true
              #attributes: array:1 [
                "title" => "Beauty Shops"
              ]
              #original: array:4 [
                "title" => "Beauty Shops"
                "pivot_company_id" => 1
                "pivot_sic_code" => "7231"
                "pivot_is_primary" => 1
              ]
              #relations: array:1 [
                "pivot" => Illuminate\Database\Eloquent\Relations\Pivot {#554
                  #parent: App\Company {#532
                    #connection: null
                    #table: null
                    #primaryKey: "id"
                    #keyType: "int"
                    #perPage: 15
                    +incrementing: true
                    +timestamps: true
                    #attributes: []
                    #original: []
                    #relations: []
                    #hidden: []
                    #visible: []
                    #appends: []
                    #fillable: []
                    #guarded: array:1 [
                      0 => "*"
                    ]
                    #dates: []
                    #dateFormat: null
                    #casts: []
                    #touches: []
                    #observables: []
                    #with: []
                    +exists: false
                    +wasRecentlyCreated: false
                    #usesTimestampsInIndex: true
                    #isDocument: false
                    #documentScore: null
                    #documentVersion: null
                  }
                  #foreignKey: "company_id"
                  #otherKey: "sic_code"
                  #guarded: []
                  #connection: null
                  #table: "company_sic"
                  #primaryKey: "id"
                  #keyType: "int"
                  #perPage: 15
                  +incrementing: true
                  +timestamps: false
                  #attributes: array:3 [
                    "company_id" => 1
                    "sic_code" => "7231"
                    "is_primary" => 1
                  ]
                  #original: array:3 [
                    "company_id" => 1
                    "sic_code" => "7231"
                    "is_primary" => 1
                  ]
                  #relations: []
                  #hidden: []
                  #visible: []
                  #appends: []
                  #fillable: []
                  #dates: []
                  #dateFormat: null
                  #casts: []
                  #touches: []
                  #observables: []
                  #with: []
                  +exists: true
                  +wasRecentlyCreated: false
                }
              ]
              #hidden: []
              #visible: []
              #appends: []
              #fillable: []
              #guarded: array:1 [
                0 => "*"
              ]
              #dates: []
              #dateFormat: null
              #casts: []
              #touches: []
              #observables: []
              #with: []
              +exists: true
              +wasRecentlyCreated: false
            }
          ]
        }
      ]
      #hidden: []
      #visible: []
      #appends: []
      #fillable: []
      #guarded: array:1 [
        0 => "*"
      ]
      #dates: []
      #dateFormat: null
      #casts: []
      #touches: []
      #observables: []
      #with: []
      +exists: true
      +wasRecentlyCreated: false
      #usesTimestampsInIndex: true
      #isDocument: false
      #documentScore: null
      #documentVersion: null
    }
  ]
}

What's the best way to remove items[$key]->relations->sic->items[$key]->relations->pivot

That's how can we remove all the [pivot] from the collection?

0 likes
5 replies
popcone's avatar

@M4rk3tt0 thank you.. I was thinking of that. Looping through the entire collection and forget() the pivots.

Is there any simpler way to quickly capture all 'pivots' and apply forget()?

popcone's avatar
popcone
OP
Best Answer
Level 2

I have done like this

$collection->each(function ($item, $itemKey) use($collection) {
    $collection[$itemKey]->sic->each(function ($sic, $sicKey) use($collection, $itemKey) {
        unset($collection[$itemKey]->sic[$sicKey]->pivot);
    });
});

Any one has a better solution?

M4rk3tt0's avatar

If you don't want to see the pivot table in your collection you can try to hide it from your model with:

protected $hidden = array('pivot');
popcone's avatar

@M4rk3tt0 protected $hidden not works for me. My 'pivot' is for a relation table. That might be the reason. Is there any way to specify hidden column like tablename.pivot?

Please or to participate in this conversation.