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

uminnkhantnaing's avatar

Route Model Binding Not Working in API Routes

I have mentioned the routes as follow. I tried a few ways to achieve the model binding but no success.

1st attempt
  • I tried to have the parameter as {organization:slug} but only empty array is returned.
2nd attempt
  • I put getRouteKeyName function in the Organization model as per laravel docs and keep route parameter as {organization} but same empty array is returned.
3rd Third attempt
  • I tried to get the organization by sending ID instead of 'slug' in the api request and still the same.

If I pass the plain slug or id, it returns the string version of it and I can query the model using that value. However, I would like to leverage the dependency injection.

/api/v1/myroutes/organization.php

Route::prefix('/organizations')->group(function () {
   Route::get('/', [OrganizationController::class, 'index']);
   Route::get('/{organization}', function(Organization $organization) {
      return response()->json($organization);
   });
});
0 likes
9 replies
aletopo's avatar

Hi!

Are you using /api/v1/myroutes/organization.php as URL to request ? It supposed to be used as /api/v1/organizations/123 or /api/v1/organizations/the-organization-slug

uminnkhantnaing's avatar

Hi @aletopo, it's the file path in which I put the routes declaration related to the organization. I have different route files for different domains of the business. And as I mentioned, everything except for the dependency injection is working. This is the route called by postman or frontend app, /api/v1/admins/myroutes/organizations/first-organization

Snapey's avatar

monitor db queries to see if it is trying to find your record

what are you passing to the api?

uminnkhantnaing's avatar

@Snapey Can you please let me know where to check the DB Queries? This is the route /api/v1/admins/myroutes/organizations/first-organization and first-organization is the slug.

aletopo's avatar

@uminnkhantnaing simply try using ddd() function to examine all contextual information, like ddd($organiztion) in the route. If the model uses SoftDeletes or any other scoped query may be not be visible for any reason, and that could why always is getting an empty array. Check the model.

uminnkhantnaing's avatar

@Snapey The following is my route directory sample. i just did it since I don't want all the api routes in the same file.

- routes (directory)
- - api (directory)
- - - v1 (directory)
- - - - admins (directory)
- - - - - myroutes (directory)
- - - - - - organization.php (file)
- - - - - - there are also other route files in this directory (file)
- - - index.php (file - this file requires other route files from different directories)
- - web.php (file)

@aletopo I got the error when ddd(). Exception: Dump, Die, Debug in file /path-to-project/vendor/spatie/laravel-ignition/src/helpers.php on line 18

uminnkhantnaing's avatar

@Snapey Yes, it does. As I mentioned, everything is working fine except for the dependency injection.

Please or to participate in this conversation.