The GET method is not supported for this route. Supported methods: PUT. Why?

Bruce Gust
Bruce Gust used Ask the Experts™
on
Here is the error I'm getting:

The GET method is not supported for this route. Supported methods: PUT.

I'm trying to build an "update" functionality in Laravel. In previous tutorials, the idea of using "store" for both updating and creating a record was recommended and simply prefacing the incoming data with this:

public function store(CreatePageRequest $request)
    {
	
		[b]$page = $request->isMethod('PUT') ? Page::findOrFail($request->page_id) : new Page; //ternary IF statement determining whether or not this is a new Page or not[/b]
		
		$page->id = $request->input('page_id');
		$page->title=$request->input('title');
		$page->body=$request->input('body');
		$page->userId=$request->input('user_id');
		$success='Page was successfully created!';
		
		if($page->save()) {
			return View::make('/admin/displayPage')
			->with('page', $page)
			->with('newPage', 'Here\'s the page you just entered!');
		}
    }

Open in new window


So, here's my "showPage.blade.php" page:

@extends('../layouts.satellite')
@section('content')
<!-- Section: intro -->
<section id="intro" class="intro">
	<div class="satellite-content">
		<div class="container">
			<div class="row">
				<div class="col-xs-12 satellite_row">
					<h5>NOMAS<sup>&reg;</sup> International Admin New Page Form</h5>
					<div class="row">
						<div class="col-xs-12">
						Hello, {{ Auth::user()->name }}!
						<br><br>
						To make any changes to the page that you've just selected, simply edit the content of the fields below and click on "submit."
						<br><br>
						If you have any questions or need any assistance, contact Bruce Gust at <a href="mailto:bruce@brucegust.com">bruce@brucegust.com</a>.
						<br><br>
						Thanks!
						</div>
					</div>
					<div class="row">
						<div class="col-xs-12"><hr></div>
					</div>
					@if($errors->any())
						<div class="alert alert-danger">
							<ul>
								@foreach($errors->all() as $error)
									<li>{{ $error }} </li>
								@endforeach
							</ul>
						</div>
					@endif
					<form method="Put" action="{{ route('adminUpdatePage') }}">
						<div class="form-group">
							<input type="hidden" value="{{csrf_token()}}" name="_token">
							<div class="row">
								<div class="col-xs-12">
									<label for="title">Page Title</label>
									<input type="text" class="form-control" name="title" id="title" value="{{ $page->title }}">
								</div>
							</div>
							<div class="row">
								<div class="col-xs-12">
									<label for="title">Body</label>
									<textarea class="form-control" id="body" name="body">{{ $page->body }}</textarea>
								</div>
							</div>
							<div class="row">
								<div class="col-xs-12" style="text-align:center;"><input type="hidden" name="user_id" value="{{Auth::user()->id}}"><input type="hidden" name="page_id" value="{{ $page->id }}"><br>
									<input type="image" name="submit" src="{{ asset('assets/img/nomas_submit.jpg') }}" style="width:150px;">
								</div>
							</div></form>
						</div>
					</form>
				</div>
			</div>
		</div>
	</div>
</section>
@endsection

Open in new window


...and here are my routes:

Route::prefix('admin')->group(function() {
    Route::get('/dashboard', 'AdminController@dashboard')->name('adminDashboard');
    Route::get('/users', 'AdminController@dashboard')->name('adminUsers');
	Route::get('/list/page', 'PageController@index')->name('adminListPages');
	Route::get('/insert/page', 'PageController@insertPage')->name('adminInsertPage');
	Route::get('/show/page/{id}', 'PageController@show')->name('adminShowPage');
	Route::post('/edit/page', 'PageController@edit')->name('adminEditPage');
	Route::post('/create/page', 'PageController@store')->name('adminStorePage');
	Route::put('/update/page', 'PageController@store')->name('adminUpdatePage');	
});

Open in new window


And, just in case this is relevant in a way that I'm not seeing, here's the view that gets fired after a successful "save" with my Controller:

@extends('../layouts.satellite')
@section('content')
<!-- Section: intro -->
<section id="intro" class="intro">
	<div class="satellite-content">
		<div class="container">
			<div class="row">
				<div class="col-xs-12 satellite_row">
					<h5>NOMAS<sup>&reg;</sup> International Admin New Page Form</h5>
					<div class="row">
						<div class="col-xs-12">
						@if($newPage)
							<div class="alert alert-success">{{ $newPage }}</div>
						@endif
						Click here to return to the list of pages.
						<br><br>
						If you have any questions or need any assistance, contact Bruce Gust at <a href="mailto:bruce@brucegust.com">bruce@brucegust.com</a>.
						<br><br>
						Thanks!
						</div>
					</div>
					<div class="row">
						<div class="col-xs-12"><hr></div>
					</div>
					@if(Session::has('success'))
						<div class="alert alert-success">{{ Session::get('success') }}</div>
					@endif
					
					@if($errors->any())
						<div class="alert alert-danger">
							<ul>
								@foreach($errors->all() as $error)
									<li>{{ $error }} </li>
								@endforeach
							</ul>
						</div>
					@endif
					<form method="Post" action="{{ route('adminStorePage') }}">
						<div class="form-group">
							<input type="hidden" value="{{csrf_token()}}" name="_token">
							<div class="row">
								<div class="col-xs-12">
									<label for="title">Page Title</label>
									<input type="text" class="form-control" name="title" id="title" value="{{ $page->title }}">
								</div>
							</div>
							<div class="row">
								<div class="col-xs-12">
									<label for="title">Body</label>
									<textarea class="form-control" id="body" name="body">{{ $page->body }}</textarea>
								</div>
							</div>
							<div class="row">
								<div class="col-xs-12" style="text-align:center;"><br>
									<input type="image" name="submit" src="{{ asset('assets/img/nomas_submit.jpg') }}" style="width:150px;">
								</div>
							</div></form>
						</div>
					</form>
				</div>
			</div>
		</div>
	</div>
</section>
@endsection

Open in new window


What am I missing? Why does Laravel insist that the functionality I'm trying to access is tailored for "GET" when I don't have anything in my routing (that I can see) that would inspire that error?

What am I doing wrong?
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Most Valuable Expert 2018
Distinguished Expert 2018
Commented:
Hey Bruce,

Although the HTTP Protocol supports GET, POST, PUT, PATCH, DELETE etc, unfortunately Browsers don't support them all - they only support GET and POST. This means that you can't do this:

<form method="Put" action="{{ route('adminUpdatePage') }}">

Laravel works around this by adding additional form fields so that it knows when to process a PUT request, so you need to create your form with an additional @method Blade directive, like this:

<form action="{{ route('adminUpdatePage') }}" method="POST">
    @method('PUT')
    ...

Open in new window

Now when you submit your form, Laravel knows it's a PUT request.
Bruce GustPHP Developer

Author

Commented:
Alright, Chris!

What you've recommended works, but could you bear with me for one minute?

This is the code that I have for "store" - code that I learned through a tutorial that is designed to serve as that which can handle both the creation and the updating of a record depending on the method.

Up until your counsel, I was stuck in that PUT wasn't being recognized. But now that it is, I've got another dilemma.

Here's my "store" method:

    public function store(CreatePageRequest $request)
    {
	
		$page = $request->isMethod('PUT') ? Page::findOrFail($request->page_id) : new Page; //ternary IF statement determining whether or not this is a new Page or not
		
		$page->id = $request->input('page_id');
		$page->title=$request->input('title');
		$page->body=$request->input('body');
		$page->userId=$request->input('user_id');
		$success='Page was successfully created!';
		
		if($page->save()) {
			return View::make('/admin/displayPage')
			->with('page', $page)
			->with('newPage', 'Here\'s the page you just entered!');
		}
    }

Open in new window


The problem is that my "CreatePageRequest" is designed specifically for new records in that one of the rules is that the title has to be unique.

Is there a way that I can alter that CreatePageRequest file so that the rules are changed depending on the method being used?

So, if that file looks like this currently:

<?php

namespace App\Http\Requests;

use Illuminate\Foundation\Http\FormRequest;

class CreatePageRequest extends FormRequest
{
    /**
     * Determine if the user is authorized to make this request.
     *
     * @return bool
     */
    public function authorize()
    {
        return true;
    }

    /**
     * Get the validation rules that apply to the request.
     *
     * @return array
     */
    public function rules()
    {
        return [
            //
			'title'		=> 'required | unique:pages',
			'body'	=> 'required'
        ];
    }
	public function messages()
    {
        return [
            'title is.required' => 'title is required!',
            'content is.required' => 'content is required!'
        ];
    }
}

Open in new window


How can I change it so "'title' => 'required | unique:pages'," is "title' => 'required' if the method is PUT?
Bruce GustPHP Developer

Author

Commented:
Chris!

I just wrote a separate route for the updating function.

We're gold!

Thanks!

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial