futureperfect90's avatar

throw new MethodNotAllowedHttpException($others)

Hello Guys,

I am trying to update a field but want to check if the field is empty, if the field has something in it then everything is fine and updates successfully, however if the field is blank then i get:

 throw new MethodNotAllowedHttpException($others)

I have looked everywhere and everyone says its because i am posting to a get request however this is not the case as when the field has data in its fine. For my routes i am using route::Resource and have used this on other edit pages and it works fine just not on this page. All i want is the validation to come up saying that this field is required which works if i go back from the error page and it displays at the top. I'm at my wits end and don't know what it is. Someone help!

My Routes:

Route::resource('observations', 'ObservationsController');

My Blade:

@extends('layouts.app')
@section('title', 'Update Observation')
@section('content')
    <div class='col-lg-4 col-lg-offset-4'>
        <h2><i class='fa fa-key'></i>Update Observation</h2>
        <hr>
        {{--Form model binding to automatically populate our fields with observation data--}}
        {{ Form::model($childObservation, ['route' => ['observations.update', $childObservation->_id], 'method' => 'POST', 'files' => true]) }}
        {{method_field('PATCH')}}
        <table>
            {{Form::hidden('Observation', json_encode($selectedOb))}}
            @foreach($selectedOb as $observation)
                @foreach($observation as $categoryNameKey => $categoryNameValue)
                    <tr><td><h3><b>Observation Category</b></h3></td></tr>
                    <tr><td><h4>{{$categoryNameKey}}</h4></td></tr>
                    <tr><td>&nbsp;</td></tr>
                    @foreach($categoryNameValue as $subCatKey => $subCat)

                        <tr><td><b>Observation Sub Category</b></td></tr>
                        <tr><td>{{$subCatKey}}</td></tr>
                        <tr><td>&nbsp;</td></tr>
                        @foreach($subCat as $ageKey => $statement)
                            <tr><td><b>Observation Age Range</b></td></tr>
                            <tr><td>{{$ageKey}}</td></tr>
                            <tr><td>&nbsp;</td></tr>
                            <tr><td><b>Observation Statements</b></td></tr>
                            <tr><td>{{$statement}}</td></tr>
                        @endforeach

                        <tr><td>&nbsp;</td></tr>
                        <tr><td><b>Stage</b></td></tr>
                        <tr>
                            <td>
                                {{Form::select('Stage[]', array('Ent' => 'Entry', 'Dev' => 'Developing', 'Sec' => 'Secure'), ['class' => 'form-control']) }}
                            </td>
                        </tr>
                        <tr><td>&nbsp;</td></tr>
                        <tr><td><b>Level</b></td></tr>
                        <tr>
                            <td>
                                {{Form::select('Level[]', array('1' => '1', '2' => '2', '3' => '3'), ['class' => 'form-control']) }}
                            </td>
                        </tr>
                        <tr><td>&nbsp;</td></tr>
                        <tr><td><hr></td></tr>

                    @endforeach

                @endforeach
            @endforeach
            <tr><td>{{Form::label('ObservationNotes', 'Observation Notes')}}</td></tr>
            <tr><td>{{Form::textarea('ObservationNotes', $childObservation->ObservationNotes, ['class' => 'form-control'])}}</td></tr>
            <tr><td>&nbsp;</td></tr>
            <tr><td>{{Form::label('CurrentImages', 'Current Images')}}</td></tr>
            @if ($childObservation->UploadImage != null)
                @foreach($childObservation->UploadImage as $images)
                <tr><td><img class="imageSize" src ="{{asset('storage/'.$images)}}"></td></tr>
                @endforeach
                @else <tr><td><p>No Images Stored.</p></td></tr>
            @endif
            <tr><td>{{Form::label('ImageUpload', 'Upload Image')}}</td></tr>
            <tr><td>{{Form::file('ImageUpload[]', ['multiple'=>true])}}</td></tr>
            <tr><td>&nbsp;</td></tr>
        </table>
        {{ Form::submit('Update Observation', array('class' => 'btn btn-primary')) }}

        {{ Form::close() }}
    </div>

@endsection

My Controller function:


public function update(Request $request, $id)
    {
        $data = [];
        //multiple file upload
        if ($request->hasfile('ImageUpload')) {
            foreach ($request->file('ImageUpload') as $image) {
                $name = $image->getClientOriginalName();
                $extension = $image->getClientOriginalExtension();
                $tmpFileName = $image->getFilename();
                Storage::disk('local')->put($tmpFileName . '.' . $extension, File::get($image));
                $data = $tmpFileName . '.' . $extension;
            }
        }

        $this->validate($request, [
            'ObservationNotes' => 'required',
            'ImageUpload.*' => 'image|mimes:jpeg, png, jpg, gif, svg',
            'UpdatingStaffId' => 'nullable'
        ]);

        $childObservation = ChildObservation::findOrFail($id);
        $updatedStaffId = Auth::id();


        $childObservation->ObservationDate = $request->ObservationDate;
        $childObservation->Observation = $request->Observation;
        $childObservation->Stage = $request->Stage;
        $childObservation->Level = $request->Level;
        $childObservation->ObservationNotes = $request->ObservationNotes;
        $childObservation->UpdatingStaffId = $updatedStaffId;

        //if there is no new images are added then $data is empty, if it is empty use the value of the hidden field using the last images used or null if no images
        if (empty($data)) $data = json_decode($request->OldChildUpload);

        $childObservation->UploadImage = $data;

        $childObservation->save();

        return redirect()->route('observations.index')
            ->with('flash_message',
                'Observation edited successfully!');
    }
0 likes
5 replies
Screenbeetle's avatar

Maybe unrelated but I don't think the form facade needs the separate {{method_field('PATCH')}} statement. You can just add patch instead of Post here:'method' => 'PATCH',

Laravel Collective no longer support the Form facade and pulled the docs .. which makes it a little tricky to cross reference your code.

I do vaguely recall having similar oddball issues with the Forms facade in the past though. My fall back approach is to just rewrite the form not using the facade. Even if this doesn't fix the issue it often helps to rethink and reveal what's going wrong.

futureperfect90's avatar

@SCREENBEETLE - Thanks for getting back to me, tbh the only reason that the patch is in there is because someone said i might work but didn't anyway and just not removed it. Yeah i think thats what it is going to come to and just rewrite and go from there

Snapey's avatar

what field are you talking about that might be empty?

Snapey's avatar

You might be getting an error because of the route that loaded this form.

When you get a validation error, it will do a GET of the previous URL. If the previous URL was as a result of a POST then you can break the request because the framework does a GET of the previous URL

I don't know what came before, but if you return a view as a result of a POST then this could be the issue. Always finish a POST request with a redirect and never return a view.

Please or to participate in this conversation.