分形 laravel 转换器中的条件属性

Conditional Attributes in Fractal laravel transformers

我正在使用 Fractal laravel package 作为复杂数据输出的表示层和转换层。

我写了一个 UserTransformer 这样的:

public function transform(User $user)
    {
        return [
            'user_id'            => (int)$user->user_id,
            'name'               => $user->name,
            'family'             => $user->family,
            'username'           => $user->username,
            'token'              => $user->token,
            'mobile'             => $user->mobile,
            'email'              => $user->email,
            'sex'                => $user->sex,
            'tel'                => $user->tel,
            'province'           => $user->province,
            'city'               => $user->city,
            'picture'            => $user->picture,
            'birthday'           => $user->birthday,
            'wedding_date'       => $user->wedding_date,
            'wife_birthday'      => $user->wife_birthday,
            'desc'               => $user->desc,
            'active'             => (bool) $user->active,
            'supervisor'         => $user->supervisor,
            'two_factor_enabled' => (bool) $user->two_factor_enabled,
            'address'            => $user->address,
            'created_at'         => $user->created_at,
        ];
    }

现在在某些情况下,我想 return User 模型的特定字段作为这样的集合:

public function index()
    {
        return $this->collection(User::get(['user_id','name','family','username','created_at']), new UserTransformer());
    }

但在这种情况下结果是这样的:

"result": [
        {
            "user_id": 1,
            "name": "ahmad",
            "family": "badpey",
            "username": "09139616246",
            "token": null,
            "mobile": null,
            "email": null,
            "sex": null,
            "tel": null,
            "province": null,
            "city": null,
            "picture": null,
            "birthday": null,
            "wedding_date": null,
            "wife_birthday": null,
            "desc": null,
            "active": false,
            "supervisor": null,
            "two_factor_enabled": false,
            "address": null,
            "created_at": {
                "date": "2017-11-15 10:01:24.000000",
                "timezone_type": 3,
                "timezone": "Asia/Tehran"
            }
        }
]

如您所见,未包含在选择 User 模型中的字段具有 null 值和 returned。但我只想要包含字段 return。我怎样才能做到这一点 ?

使用数组过滤器删除它们,像这样

public function transform(User $user)
    {
        return array_filter([
            'user_id'            => (int)$user->user_id,
            'name'               => $user->name,
            'family'             => $user->family,
            'username'           => $user->username,
            'token'              => $user->token,
            'mobile'             => $user->mobile,
            'email'              => $user->email,
            'sex'                => $user->sex,
            'tel'                => $user->tel,
            'province'           => $user->province,
            'city'               => $user->city,
            'picture'            => $user->picture,
            'birthday'           => $user->birthday,
            'wedding_date'       => $user->wedding_date,
            'wife_birthday'      => $user->wife_birthday,
            'desc'               => $user->desc,
            'active'             => (bool) $user->active,
            'supervisor'         => $user->supervisor,
            'two_factor_enabled' => (bool) $user->two_factor_enabled,
            'address'            => $user->address,
            'created_at'         => $user->created_at,
        ], function($item){
            return !is_null($item);
        });
    }

请注意,我不使用 Laravel,但这通常是您从数组中批量删除内容的方式。

http://php.net/manual/en/function.array-filter.php

Iterates over each value in the array passing them to the callback function. If the callback function returns true, the current value from array is returned into the result array. Array keys are preserved.

http://php.net/manual/en/function.is-null.php

Finds whether the given variable is NULL. Returns TRUE if var is null, FALSE otherwise.