Skip to content

[7.x] Added appends($attributes) method to Eloquent collections - #32324

Merged
taylorotwell merged 4 commits into
laravel:7.xfrom
jazerix:7.x
Apr 13, 2020
Merged

taylorotwell merged 4 commits into
laravel:7.xfrom
jazerix:7.x

Conversation

@jazerix

@jazerix jazerix commented Apr 10, 2020 •

Copy link
Copy Markdown
Contributor

Much like the makeVisible($attributes) and makeHidden($attributes) methods, appends($attributes) allows the developer to utilize Laravel's accessors to append specific attributes across an entire eloquent collection.

$collection->append($attribute);

If you wan't to achieve something similar right now, you would have to do something like this:

$collection->each(function($model) {
    $model->append($attribute)
});

Otherwise your only option is to specify the attributes to be appended directly within the model, where it will be appended every time.

The downside here is that you'd have to remove/unset/hide the attribute whenever you don't need that specific one.

As an end user, this method allows me to more quickly choose when certain attributes should be appended.

The method has simply been added to the Collections.php class and should not cause any breaking changes.

@GrahamCampbell GrahamCampbell changed the title appends($attributes) method added to Eloquent Collections. [7.x] Added appends($attributes) method to Eloquent collections Apr 11, 2020
Comment thread src/Illuminate/Database/Eloquent/Collection.php Outdated
Comment thread tests/Database/DatabaseEloquentCollectionTest.php Outdated
@jazerix
jazerix requested a review from GrahamCampbell April 11, 2020 00:19
Comment thread src/Illuminate/Database/Eloquent/Collection.php Outdated
@jazerix

jazerix commented Apr 11, 2020

Copy link
Copy Markdown
Contributor Author

@GrahamCampbell it should be fixed now, if not can you point me in the direction where I'm going wrong? :)

@jazerix
jazerix requested a review from GrahamCampbell April 11, 2020 10:36
@taylorotwell
taylorotwell merged commit 6f426d6 into laravel:7.x Apr 13, 2020
@d-damien

Copy link
Copy Markdown

Won't that cause confusion with expected behavior (apppend being synonymous with push) ? Maybe eachAppend instead, like there is eachSpread ?

salmanarshad321 pushed a commit to salmanarshad321/framework that referenced this pull request Sep 23, 2026
…vel#32324)

* Added appends($attribute) method to Eloquent Collections

* Revert "Added appends($attribute) method to Eloquent Collections"

This reverts commit 8053f6d.

* Fixed styling

* Fixed docblock styling
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants