@spl3s I don’t really understand your question. If you want JSON returned from a controller, then you have to return JSON or something JSON-able. If you return a view then yes, a HTML view is going to be returned and not JSON.
Nov 3, 2021
3
Level 1
API routes to return json from controllers that return view
It might sound silly, but I never had interaction with API stuff until now. I need to prepare API routes for the app, I tried using the middleware group for the API header to return JSON of the model, but it always returns the whole HTML view.
I just copy/pasted web.php to api.php routes.
My question is if there is a way to get a JSON from a model when the return is view?
my current controller
public function index() {
$products = Product::paginate(30);
return view('products.products', compact('products'));
}
or do I have to create a separate controller for all API routes that will be called like this and middleware group passes as JSON?
public function index() {
$products = Product::paginate(30);
return $products;
}
Level 80
Please or to participate in this conversation.