joshgallagher24
2 years ago

Fractal include

Posted 2 years ago by joshgallagher24

Hi all,

I am developing a small API using Lumen, JWT Auth and Fractal. I am having trouble implementing some functionality within fractal.

I am returning a user using a transformer, however, in some controller methods I want to include the generated token.

The method I want to do this in:

    public function store(Request $request)
    {
        $this->validateUser($request);

        $user = new User;
        
        $user->email = $request->email;
        $user->password = bcrypt($request->password);

        $user->save();

        $token = $this->jwt->attempt($request->only('email', 'password'));

        return fractal()
            ->item($user)
            ->transformWith(new UserTransformer)
            ->toArray();
    }

How would I take the "$token" variable and add it o the transformer output?

I would like the structure to be on the lines of:

{
    "data": {
        "id": "05720397-c625-4be6-a52f-10e85eb9de5b",
        "email": "[email protected]"
    },
    "meta": {
    "token": Token here
    }
}

Thanks in advance!

Please sign in or create an account to participate in this conversation.