0

我需要从 API 资源发送特定字段

这是我的User资源代码

namespace App\Http\Resources;

use App\Models\MediaFile;
use Illuminate\Http\Resources\Json\JsonResource;
use Illuminate\Support\Facades\Storage;

class User extends JsonResource
{
    /**
     * Transform the resource into an array.
     *
     * @param \Illuminate\Http\Request $request
     * @return array
     */
    public function toArray($request)
    {
        $attributes = $this->getAttributes();
        unset($attributes['password']);

        return [
            'type' => 'users',
            'attributes' => $attributes,
        ];
    }
}

下面是我的属性

"attributes": {
        "id": "1",
        "email": "email",
        "full_name": "Name",
        "permission": "admin",
        "security_key": "alpha",
        "token": "encrypted token",
        "two_factor_enabled": "true",
        "created_at": "2020-05-15 08:56:50",
        "updated_at": "2020-05-15 08:56:57",
      }

我想隐藏特定路线中的特定字段。我该如何实施?

4

1 回答 1

0

使用makeHidden

$user = \App\Models\User::firstOrFail();

$user->makeHidden(['email', 'phone']);

return $user;
于 2020-05-15T15:25:46.290 回答