Laravel 6: named routes inside a folder doesn’t get the user object

I’m having a UsersController in this path app/Http/Controllers/Admin with the default edit function

public function edit(User $user)
{
    dd($user);
}

and this route
Route::namespace('Admin')->group(function (){
   Route::get('adminUsers', '<a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="a7f2d4c2d5d4e4c8c9d3d5c8cbcbc2d5e7cec9c3c2df">[email protected]</a>')->name('adminUsers');
   Route::get('adminEditUser/{id}', '<a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="613412041312220e0f15130e0d0d04132104050815">[email protected]</a>')->name('adminEditUser');
   Route::get('adminDeleteUser/{id}', '<a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="82d7f1e7f0f1c1edecf6f0edeeeee7f0c2e6e7eee7f6e7">[email protected]</a>')->name('adminDeleteUser');

});

enter image description here

So all controllers get the “Admin” folder name before it instead of adding it for every controller. Now I have this view

@foreach($users as $user)
      <tr>
          <td>{{ $user->name }}</td>
          <td>{{ $user->email }}</td>
          <td><a href="{{ route('adminEditUser', $user->id) }}" rel="nofollow noreferrer noopener" class="btn btn-sm btn-primary">Edit</a>
              <a href="{{ route('adminDeleteUser', $user->id) }}" rel="nofollow noreferrer noopener" class="btn btn-sm btn-primary">Delete</a></td>
      </tr>
@endforeach

The edit link opens this url
http://127.0.0.1:8000/adminEditUser/1

And the $user object doesn’t have any user data (user with id 1 already exists)
AppUser {#1419 ▼
#fillable: array:3 [▶]
#hidden: array:2 [▶]
#casts: array:1 [▶]
#connection: null
#table: null
#primaryKey: "id"
#keyType: "int"
+incrementing: true
#with: []
#withCount: []
#perPage: 15
+exists: false
+wasRecentlyCreated: false
#attributes: []
#original: []
#changes: []
#dates: []
#dateFormat: null
#appends: []
#dispatchesEvents: []
#observables: []
#relations: []
#touches: []
+timestamps: true
#visible: []
#guarded: array:1 [▶]
#rememberTokenName: "remember_token"
}

Why is that and how to solve this?

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

Your route parameter is named id not user. The parameter name has to match what you are type hinting for the Implicit Route Model Binding to work.

Route::get('adminEditUser/{user}', '<a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="fbae889e8988b894958f899497979e89bb9e9f928f">[email protected]</a>')->name('adminEditUser');

Now it will match the method signature you have:
public function edit(User $user)

In your case you were just getting Dependency Injection of a new User object instead of any Model Binding happening.

“Laravel automatically resolves Eloquent models defined in routes or controller actions whose type-hinted variable names match a route segment name.”

Laravel 6.x Docs – Routing – Route Model Binding – Implicit Binding


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x