You can use the forget() method. Documentation here
Oct 28, 2016
5
Level 2
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?
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?
Please or to participate in this conversation.