In this article you will read how to set up Application Programming Interfaces (APIs) via Laravel and Fractal. 

API Development is one of the things we are strongly committed to at Codana. We like to build strong APIs for different purposes.
When we use Laravel to build an application we always use Fractal.

There are a lot of examples in the documentation and on the internet how this is easy to implement in Laravel.
Here we have listed some recommended practices, which we use in some of our own projects.

What's Fractal for Laravel?

Fractal is a library written by the PHP League, a group that creates open source framework-independent libraries. To make this easy to use within Laravel applications a wrapper is provided.

You use fractal to return consistent responses from your API, to easily convert your data to JSON/YAML, because you send everything systematically through the same transformers you always make sure that data types are consistently the same. This layer between your output and your data models ensures that schema changes do not automatically get through to your API. So you monitor the backwards compatibility of your API much better.

Use the injected service to build responses.

There are facades to make this easier, but by doing so, your IDE can help you build your results.

public function __construct(Fractal $fractal)
	$this->fractal = $fractal;

public function respondWithData(User $user): JsonResponse
	return $this->fractal
      ->transformWith(new UserDetailsTransformer())


Use includes where they are useful and reusable transformers.

It is very tempting to build up specific responses by giving extra data from a specific transformer.
In order to maintain as much consistent data as possible to the consumers of the API, it is recommended to reuse transformers where possible.



Extra data is passed on to the constructor of the transformer.

If you need extra data in a transformer, you can give it to the constructor.

So don't do it this way.

// The bad way.
  ->item(['data' => $data, 'currentUser' => $user])
  ->transformWith(new DataForUserTransformer());

It is clearer to do this, you can typehint on `transform` in the DataForUserTransformer (on the two arguments).

// The better way.
      ->transformWith(new DataForUserTransformer($user));


Use a NullTransformer where necessary.

Sometimes you build an endpoint that is not strictly restful. For example, an endpoint where the average sales of the past week are calculated. In such cases, you can use a transformer that easily returns what you put in, without the need for other ways of building up responses.

final class NullTransformer extends TransformerAbstract
    public function transform(array $data): array
        return $data;

class Controller {

public function respondWithData(Request $request): JsonResponse
  $data = calculateForRequest($request);
  return $this->fractal
      ->transformWith(new NullTransformer())


Eager loading is your best friend.

If you have configured fractal to always require an include, or otherwise display related data, make sure that the eager is loaded before you send it to fractal.

Adding eager loading before sending this data to fractal ensures that the data is already available in memory and does not need to be retrieved from the database on an item-by-item basis. This provides free performance gains for this endpoint.


Fractal always ensures that your code has a better structure and that your API is consistent and easy to consume. By using these tips you can have even more fun.

Author: Joris Vercammen
Joris Vercammen

More insights

Codana wins Digital Champ of the year award 2022

Codana is the winner of the FeWeb Digital Champ award 2022! 

Author: Joris De Groot
Strategic Director and Managing Partner
Joris De Groot
FeWeb Digital Champ award Codana

Life as a React developer at Codana: 1 year in service

"You can start working with us as a React developer!". Bliss! Needless to say, I was super happy to hear this! But, I had never written a line of React before.

Author: Thomas Timmermans
Frontend Developer
Thomas Timmermans
Ik na 1 jaar in dienst

The ideal first workplace

I'm Ward Vandevoort, 22 years old and early November 2021 I started at Codana as a junior backend developer. Discover my story at Codana!

Author: Ward Vandevoort
Ward Vandevoort


We have a great teambuilding in Antwerp!

Author: Noah Gillard
PHP / Laravel Developer
Noah Gillard AI generated Face
groepsfoto codana burgerij

Nova 4.0 is available!

De belangrijkste nieuwigheden uitgelicht.

Author: Noah Gillard
PHP / Laravel Developer
Noah Gillard AI generated Face
Thumbnail Blog Nova 4 Release Codana Styled

Laracon Online Winter 2022

This is what we learned

Author: Tom Van den Eynden
Web Architect | Coordinator
Tom Van den Eynden
Laracon winter 2022 Codana Banner