I am building an application in Laravel 5.7. I have set up a route in my routes/api.php
file which is bound to the user model. When I pass an existing user ID into the route it is being resolved to a blank user model.
I believe I have followed the laravel standard for this by type-hinting for a user model in the controller.
routes/api.php
Route::prefix('role-permissions')->group(function () {
Route::get('by-user/{user}', 'RolePermissionsController@getByUserId')->name('api.role-permissions.by-user');
});
app/Http/Controllers/RolePermissionsController.php
public function getByUserId(User $user)
{
dd($user);
return $this->getAsResourceCollection($this->repository->getByUserId($user));
}
the dd($user);
returns a blank User
object
Expected: A filled user model with the correct id I can confirm a user with Id 1 DOES exist in my table
Actual:
User {#375
#fillable: array:3 [
0 => "name"
1 => "email"
2 => "password"
]
#hidden: array:2 [
0 => "password"
1 => "remember_token"
]
#connection: null
#table: null
#primaryKey: "id"
#keyType: "int"
+incrementing: true
#with: []
#withCount: []
#perPage: 15
+exists: false
+wasRecentlyCreated: false
#attributes: []
#original: []
#changes: []
#casts: []
#dates: []
#dateFormat: null
#appends: []
#dispatchesEvents: []
#observables: []
#relations: []
#touches: []
+timestamps: true
#visible: []
#guarded: array:1 [
0 => "*"
]
#rememberTokenName: "remember_token"
#accessToken: null
}
I had removed the api middleware route the mapApiRoutes
method in the RouteServiceProvider
I don't think the $user should be object. It is the parameter passed in route, so the code should be :
public function getByUserId($user)
{
dd($user);
return $this->getAsResourceCollection($this->repository->getByUserId($user));
}