Link to home
Start Free TrialLog in
Avatar of Bruce Gust
Bruce GustFlag for United States of America

asked on

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

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?
ASKER CERTIFIED SOLUTION
Avatar of Chris Stanyon
Chris Stanyon
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Bruce Gust

ASKER

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?
Chris!

I just wrote a separate route for the updating function.

We're gold!

Thanks!