Skip to content

[9.x] Add whenHas to JsonResource#45376

Merged
taylorotwell merged 10 commits intolaravel:9.xfrom
michaelnabil230:features-json-resource
Dec 23, 2022
Merged

[9.x] Add whenHas to JsonResource#45376
taylorotwell merged 10 commits intolaravel:9.xfrom
michaelnabil230:features-json-resource

Conversation

@michaelnabil230
Copy link
Copy Markdown
Contributor

@michaelnabil230 michaelnabil230 commented Dec 21, 2022

This PR adds a whenHas method to the JSON resource class giving us the ability to conditionally include attributes in a response when an attribute has in the model.

In some time to need to make many resources for return data to API because we do need to return for example in the function index only id and name for the user. But in the show function, you need all data for the user.

To fix this problem need to make many resources for resolving it.

And I think all developers need this feature

Note: I make some clear and enhancement whenCounted to whenHas function

Before:

class UserController
{
    public function index()
    {
        $users = User::select(['id', 'name'])->get();

        return UserResource::collection($users);
    }

    public function show(User $user)
    {
        return SingleUserResource::make($user);
    }
}
class UserResource extends Resource
{
    public function toArray($request)
    {
        return [
            'id' => $this->id,
            'name' => $this->name,
        ];
    }
}
class SingleUserResource extends Resource
{
    public function toArray($request)
    {
        return [
            'id' => $this->id,
            'name' => $this->name,
            'email' => $this->email,
        ];
    }
}

Now with magic function with the same Resource:

class UserController
{
    public function index()
    {
        $users = User::select(['id', 'name'])->get();

        return UserResource::collection($users);
    }

    public function show(User $user)
    {
        return UserResource::make($user);
    }
}
class UserResource extends Resource
{
    public function toArray($request)
    {
        return [
            'id' => $this->id,
            'name' => $this->name,
            'email' => $this->whenHas('email'),
        ];
    }
}

@taylorotwell
Copy link
Copy Markdown
Member

taylorotwell commented Dec 21, 2022

Why can you not just use whenNotNull($this->email) in your example?

@michaelnabil230
Copy link
Copy Markdown
Contributor Author

@taylorotwell At some times, the default value is null for example email_verified_at
Not can use the whenNotNull($this->email_verified_at) in this case but if use whenHas($this->email_verified_at) return null if the attribute in resource attributes

@michaelnabil230
Copy link
Copy Markdown
Contributor Author

michaelnabil230 commented Dec 21, 2022

If you select email_verified_at from the User model and the default value is null to return a null in the user response does not return the MissingValue class. And on the other hand when using whenNotNull return the MissingValue class.

@taylorotwell taylorotwell merged commit b83a1ce into laravel:9.x Dec 23, 2022
@michaelnabil230 michaelnabil230 deleted the features-json-resource branch December 23, 2022 21:44
@MasterRO94
Copy link
Copy Markdown
Contributor

As for me naming is inconvenient to other Laravel classes. It would be better name it whenExists or whenPresent

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.

3 participants