2 years ago

Laravel 5.3 and Spark API, routes not returning JSON

Posted 2 years ago by roemer

Hi, I'm using Spark and Laravel 5.3.19 to setup a JSON API. I can see that Spark does it like so:


$router->get('/settings/api/tokens', 'Settings\API\[email protected]');


    public function all(Request $request)
        return $this->tokens->all($request->user());

And repo:

    public function all($user)
        return $user->tokens()
                    ->where('transient', false)
                    ->orderBy('created_at', 'desc')

I like to do it similarly, where I can just do return $myRepo->all(); and it returns JSON to the client. I've had no problem setting up the repository and controller but now when I go to the route I've configured, it just redirects me back to my homepage without any errors or anything. My application does recognise the route because I don't get a NotFoundHttpException. I've placed my route in routes/api.php and tried it inside and outside of the 'middleware' => 'auth:api' block.

What confuses me is that the Spark route does not seem to have any further configuration that tells it to spit out JSON. Can anyone explain this to me?

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