Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

JohnEC's avatar

How to return JSON response from a query with eloquent?

How to return JSON response from a query with eloquent?

0 likes
1 reply
kingmaker_bgp's avatar

Just return the Collection or Model instance from the Controller.

public function someAction()
{
	$collection = Model::conditions()
		->get();
	$model = Model::conditions()
		->first();

	return $collection;
	// OR
	return $model;
}

If you want to Transform the JSON in some particular way, use API Resource (Docs)

Please or to participate in this conversation.