Search code examples
laravelcontrollercustom-function

Custom function in Controller with resource wont work


I have created my own custom function in my RoomsController

public function join($id){ 
    return $id; 
}

Then I want to pass variable to it and it says MethodNotAllowedHttpException And my Form looks like this

{{Form::open(['action'=>  ['RoomsController@join', $room->id], 'method' => 'POST' ])}} 
{{Form::submit('Join', ['class' => 'btn btn-danger'])}}
{{Form::close()}}

Also have these routes


Route::get('/','PagesController@index');
Route::get('/about', 'PagesController@about');
Route::get('/services', 'PagesController@services');
Route::get('/register', 'PagesController@register');
Route::get('/logout', 'PagesController@logout'); 
Route::get('/rooms/join', 'RoomsController@join'); 
Route::resource('posts','PostsController');
Route::resource('rooms','RoomsController');
Auth::routes();

Route::get('/dashboard', 'DashboardController@index');

I have tried in many different ways i dont know why it is not working. All update edit destroy resource functions are working. Thank's for helping :)


Solution

  • You're submitting a POST request but the route is expecting a GET request. If you change your route to Route::post('/rooms/join', 'RoomsController@join'); it should work