0

I'm trying to call a route by pressing a button but it gives me Route [makeAdmin] not defined. (View: \resources\views\admin\showUser.blade.php) even though the route is defined.

my blade view:

 <div style="padding-bottom:10px;">
    @if($user->isAdmin == 0)
      <form action="{{route('makeAdmin', $user->id)}}" method="POST">
        @csrf
        @method('PUT')
         <div class="form-group">
      <button class="btn btn-warning" type="submit">Make Admin</button>
         </div> 
     </form>
    @else
      <button class="btn btn-warning" disabled="disabled">Already Admin</button>
    @endif
  </div>

my routes:

Route::GET('/', 'CoursesController@root')->name('root');
Route::GET('/courses/create', 'CoursesController@create')->name('courses.create');
Route::POST('/courses', 'CoursesController@store')->name('courses.store');
Route::GET('/courses', 'CoursesController@index')->name('courses.courses');
Route::GET('/courses/{id}', 'CoursesController@show')->name('courses.show');
Route::GET('/admin/courses', 'AdminController@getCourses')->name('admin.courses');
Route::GET('/admin/users', 'AdminController@getUsers')->name('admin.users');
Route::GET('/admin', 'AdminController@index')->name('admin');
Route::GET('/admin/courses/{id}', 'AdminController@showCourse')->name('admin.showCourse');
Route::PUT('/admin/courses/{course}', 'AdminController@editCourse')->name('admin.editCourse');
Route::DELETE('/admin/courses/{course}', 'AdminController@destroyCourse')->name('admin.destroyCourse');
Route::GET('/admin/users/{id}', 'AdminController@showUser')->name('admin.showUser');
Route::PUT('/admin/users/{user}', 'AdminController@makeAdmin')->name('makeAdmin');
Route::PUT('/admin/users/{user}', 'AdminController@editUser')->name('admin.editUser');
Route::DELETE('admin/users/{user}', 'AdminController@destroyUser')->name('admin.destroyUser');



Auth::routes();
Route::GET('/home', 'HomeController@index')->name('home');

I've tried route:clear and route:cache but the problem still appears. And the route doesn't appear in route:list Your help would be appreciated.

3
  • That route is in there twice: Route::PUT('/admin/users/{user}'... Commented May 2, 2020 at 11:07
  • Thank you ! That was the issue, I removed the duplicate route and made making admin feature into the edit user form. Thank you again. Commented May 3, 2020 at 8:11
  • Not a problem, glad it works Commented May 3, 2020 at 10:08

4 Answers 4

2

The problem appeared to be that I have a duplicate route inside web.php

Route::PUT('/admin/users/{user}', 'AdminController@makeAdmin')->name('makeAdmin');
Route::PUT('/admin/users/{user}', 'AdminController@editUser')->name('admin.editUser');

Appearently, this is not how things work in Laravel, so I removed the makeAdmin route and added the feature as a part of editUser form I have in my blade view. So web.php will only have this:

Route::PUT('/admin/users/{user}', 'AdminController@editUser')->name('admin.editUser');

Thanks all for participating. And a special thanks to kerbholz.

Sign up to request clarification or add additional context in comments.

Comments

0

If i understand correctly it looks like every /admin route in your route.php is named prefixied with admin.

So maybe try to name the route as admin.makeAdmin and call it in your view like so:

<form action="{{route('admin.makeAdmin', $user->id)}}" method="POST">

See if it works.

1 Comment

Yeah that was the oriignal code I just tried to edit the name so it might work. However, it still doesn't work.
0

Use like this

<form action="{{route('makeAdmin',['user'=>$user->id])}}" method="POST">

3 Comments

I've tried your solution but the same exception keeps appearing.
@RedJohn I update my answer. And It works nice for me
It was a great idea to associate the value with array element but still doesn't work for me. However, thank you very much.
-1

In the route for makeAdmin just specify POST instead of PUT. Because in form HTML you have mentioned that method

2 Comments

If you use put in route, you must be use put in form. If problem was put or post, laravel get error, method not allowed
Not really a good solution since I can specifiy inside blade form that I use put.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.