Home » Php » php – laravel route::resource can't find edit function in my controller

php – laravel route::resource can't find edit function in my controller

Posted by: admin February 25, 2020 Leave a comment

Questions:

When I call my edit function I get a 404|not found page.

this is the code in my View

<a href="{{ route('admin.edit', $user->id) }}">
     <button type="button" class="btn btn-primary btn-sm crudbtn">Edit</button>
</a>

This is the code in my web.php

 route::namespace('Admin')->prefix('admin')->middleware(['auth', 'auth.admin'])->name('admin.')->group(function(){
    route::resource('/', 'AdminController');
});

This is my edit function in my AdminController

public function edit($id)
{
    if(Auth::user()->id == $id){
        return redirect()->route('admin.index');
    }

    return view('admin.edit')->with(['user' => user::find($id), 'roles' => Role::all()]);
}

And this is the output of my php artisan route:list

+--------+-----------+------------------------+------------------+------------------------------------------------------------------------+---------------------+
| Domain | Method    | URI                    | Name             | Action                                                                 | Middleware          |
+--------+-----------+------------------------+------------------+------------------------------------------------------------------------+---------------------+
|        | GET|HEAD  | /                      |                  | Closure                                                                | web                 |
|        | POST      | admin                  | admin.store      | App\Http\Controllers\Admin\[email protected]                       | web,auth,auth.admin |
|        | GET|HEAD  | admin                  | admin.index      | App\Http\Controllers\Admin\Ad[email protected]                       | web,auth,auth.admin |
|        | GET|HEAD  | admin/create           | admin.create     | App\Http\Controllers\Admin\[email protected]                      | web,auth,auth.admin |
|        | DELETE    | admin/{}               | admin.destroy    | App\Http\Controllers\Admin\[email protected]                     | web,auth,auth.admin |
|        | PUT|PATCH | admin/{}               | admin.update     | App\Http\Controllers\Admin\[email protected]                      | web,auth,auth.admin |
|        | GET|HEAD  | admin/{}               | admin.show       | App\Http\Controllers\Admin\[email protected]                        | web,auth,auth.admin |
|        | GET|HEAD  | admin/{}/edit          | admin.edit       | App\Http\Controllers\Admin\[email protected]                        | web,auth,auth.admin |

When i call my index function the proper page is loaded, but I can’t figure out why my edit function cant’ be found. What am I missing or doing wrong?

How to&Answers:

Try

route::namespace('Admin')->middleware(['auth', 'auth.admin'])->group(function(){
    route::resource('admin', 'AdminController');
});