Reputation: 91
I'm building a laravel package the encapsulates a rest api, and I'm running into some issues with implicit route model binding. All I'm getting back when trying to retrieve a single record is an empty array. The id that i'm trying to retrieve is present in the database (its the only record in the table) Using debugbar, it looks like its not running the query, which implies that the route binding is failing before it has a chance to run (more on that at the bottom).
api.php:
Route::apiResources([
'trackers' => TrackerController::class,
'tracker/entry' => TrackerEntryController::class,
'tracker/types' => TrackerTypeController::class
]);
pertinent artisan route:list output:
| Method | URI | Middleware |
+-----------+-----------------------+------------+
| GET|HEAD | tracker/entry/{entry} | |
| GET|HEAD | tracker/types/{type} | |
| GET|HEAD | trackers/{tracker} | |
+-----------+-----------------------+------------+
Show method from TrackerTypeController:
use Oxthorn\Trackers\Models\TrackerType as Type;
public function show(Type $type)
{
return $type;
}
So, as far as I see, my code is using the correct naming scheme for implicit route binding.
If I change the controller show method to this:
public function show(Type $type, $id)
{
$type2 = Type::findOrFail($id);
return [
[get_class($type), $type->exists, $type],
[get_class($type2), $type2->exists],
];
}
I get this output:
[
[
"Oxthorn\\Trackers\\Models\\TrackerType",
false,
[]
],
[
"Oxthorn\\Trackers\\Models\\TrackerType",
true
]
]
This seems to mimic the behavior in this StackOverflow issue: Implicit Route Model Binding, where the last posted theory was that SubstituteBindings
middleware was not running. I'm not sure at this point what steps I need to take to ensure its running before my code execute, so I am here asking for advice on where to go from here.
Upvotes: 1
Views: 1333
Reputation: 91
You know, sleeping on a problem does wonders. For anyone that runs into this same issue while developing a package, I had to change my route code to this to solve the issue:
Route::apiResource('trackers', TrackerController::class)->middleware('bindings');
Route::apiResource('tracker/entry', TrackerEntryController::class)->middleware('bindings');
Route::apiResource('tracker/types', TrackerTypeController::class)->middleware('bindings');
Upvotes: 4