Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[12.x] Added except() method to Model class for excluding attributes #55072

Merged
merged 5 commits into from
Mar 19, 2025

Conversation

vishal2931
Copy link
Contributor

@vishal2931 vishal2931 commented Mar 18, 2025

Overview

This PR introduces a new except() method in the Model class, allowing developers to retrieve model attributes excluding specified keys. This method serves as the inverse of the existing only() method, improving usability and convenience when working with model data.

Currently, Laravel provides an only() method to retrieve a subset of attributes, but there is no built-in way to exclude specific attributes while keeping the rest. This new except() method addresses this gap by working as the inverse of only(), making it easier to manipulate model data efficiently.

Example

public function show(User $user)
{
   return response()->json($user->except('id', 'email'));
}

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
@taylorotwell
Copy link
Member

taylorotwell commented Mar 19, 2025

Can you make the logic more like only instead of calling attributesToArray.

@taylorotwell taylorotwell marked this pull request as draft March 19, 2025 13:56
@vishal2931 vishal2931 marked this pull request as ready for review March 19, 2025 15:42
@taylorotwell taylorotwell merged commit 0026d3b into laravel:12.x Mar 19, 2025
39 checks passed
@vishal2931 vishal2931 deleted the add-except-method branch March 19, 2025 18:11
@decadence
Copy link
Contributor

decadence commented Mar 20, 2025

foreach ($this->getAttributes() as $key => $value)

Am I missing something or this code will ignore virtual Attributes? So you will get only initial Model attributes except passed. Moreover it will ignore Attribute or cast for attribute.

only considers virtual Attributes and casts as it uses getAttribute.

foreach (is_array($attributes) ? $attributes : func_get_args() as $attribute) {
    $results[$attribute] = $this->getAttribute($attribute);
}

@vishal2931
Copy link
Contributor Author

foreach ($this->getAttributes() as $key => $value)

Am I missing something or this code will ignore virtual Attributes? So you will get only initial Model attributes except passed. Moreover it will ignore Attribute or cast for attribute.

only considers virtual Attributes and casts as it uses getAttribute.

foreach (is_array($attributes) ? $attributes : func_get_args() as $attribute) {
    $results[$attribute] = $this->getAttribute($attribute);
}

@decadence you're right. I can do like this : 🤔

$results[$key] = $this->getAttribute($key);

@decadence
Copy link
Contributor

So will you fix this please?

@vishal2931
Copy link
Contributor Author

Yes.

@vishal2931
Copy link
Contributor Author

@decadence I don't think it makes sense to include virtual attributes in the except consideration since they are not part of the raw attributes. We access them using the -> operator anyway. However, I understand why casts should be included. What are your thoughts on this?

@decadence
Copy link
Contributor

@vishal2931
You're right. Since Model doesn't contain list of all Attributes I don't see an easy way to get them into consideration. But casts should be included anyway.

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.

None yet

4 participants