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

egarcia's avatar
Level 12

Eager loading with API Platform for Laravel

Hello!

Somebody knows how to get to work eager loading using API Platform for Laravel with the extraProperties parameter like this?

The response I'm getting is the IRI of the related resource instead of the data of related model.

Thanks in advance for any help!

0 likes
2 replies
LaryAI's avatar
Level 58

To achieve eager loading with API Platform in Laravel and ensure that related data is embedded instead of just returning the IRI, you need to adjust the way you handle serialization and eager loading in your API resource configuration.

Here's a step-by-step solution:

  1. Ensure Eager Loading in the Controller: You need to ensure that the eager loading is actually being applied when fetching the data. This is typically done in the controller or repository layer. Since you're using API Platform, you might need to customize the query logic.

  2. Modify the API Resource Configuration: The extraProperties parameter is not natively supported by Laravel's Eloquent for eager loading. Instead, you should handle eager loading directly in your query logic. However, you can use the with method in your query to achieve this.

  3. Custom Controller or Data Provider: If API Platform does not natively support eager loading through configuration, you might need to create a custom controller or data provider to handle this. Here's an example of how you might do it in a custom controller:

    use App\Models\Book;
    use Illuminate\Http\Request;
    
    class BookController
    {
        public function getBook($id)
        {
            $book = Book::with('author')->findOrFail($id);
            return response()->json($book);
        }
    
        public function getBooks()
        {
            $books = Book::with('author')->get();
            return response()->json($books);
        }
    }
    
  4. Adjust Serialization: Ensure that your serialization context is set to include the related data. You might need to adjust the normalizationContext to ensure that the related data is serialized correctly.

  5. Check API Platform Configuration: If you're using a specific package or configuration for API Platform, ensure that it supports the features you need. You might need to extend or customize the package to handle eager loading as desired.

By following these steps, you should be able to ensure that the related data is eagerly loaded and embedded in the response, rather than just returning the IRI. If you're using a specific package or library for API Platform, consult its documentation for any additional configuration options that might be available.

vincent15000's avatar

Does this help you ?

https://api-platform.com/docs/laravel/filters/

use ApiPlatform\Metadata\ApiFilter;
use ApiPlatform\Doctrine\Orm\Filter\SearchFilter;

#[ApiFilter(SearchFilter::class, properties: ['author' => 'exact'])]
class Book
{
    // ...
}
public function index(Request $request)
{
    $query = Book::query();

    if ($request->has('include') && $request->input('include') === 'author') {
        $query->with('author');
    }

    return response()->json($query->get());
}
GET /api/books?include=author

Not sure, perhaps also something to do with this.

https://api-platform.com/docs/core/performance/

Please or to participate in this conversation.