throw new MethodNotAllowedHttpException($others)

Posted 2 months ago by futureperfect90

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!');
    }

Please sign in or create an account to participate in this conversation.

Reply to

Use Markdown with GitHub-flavored code blocks.