Rupesh Prabhu
Rupesh Prabhu

Reputation: 35

Laravel Throwing MethodNotAllowedHttpException Error

My Routes.php file for the customer page where I am facing issue when I click on the send email and send SMS button

  Route::get('/customer', 'CustomerController@showCustomers');
Route::get('/customer/add', 'CustomerController@addNewCustomer');
Route::post('/customer/add', 'CustomerController@insertCustomer');
Route::get('/customer/delete/{id}', 'CustomerController@deleteCustomer');
Route::get('/customer/edit', 'CustomerController@editCustomer');
Route::post('/customer/edit', 'CustomerController@updateCustomer');
Route::get('/search-customers', 'CustomerController@searchCustomers');
Route::post('/customer/set_type', 'CustomerController@setType');
Route::get('/customer/send_sms', 'CustomerController@sendNotification');
Route::get('/customer/send_email', 'CustomerController@sendEmail');

Customer.blade file

    <?php
include(app_path().'/includes/reports.php');
?>
@extends('layouts.dashboard')
@section('page_heading','Customers')
@section('section')
<style type="text/css">
    #customer_table_filter {
        display: none;
    }
</style>

<div class="col-sm-12">
    <div class="row">
        <div class="col-lg-3 col-md-6">
                    <div class="panel panel-primary">
                        <div class="panel-heading">
                            <div class="row">
                                <div class="col-xs-3">
                                    <i class="fa fa-users fa-5x"></i>
                                </div>
                                <div class="col-xs-9 text-right">
                                    <div class="huge"><?php echo $rowcount ?></div>
                                    <div>Total Customers</div>
                                </div>
                            </div>
                        </div>
                    </div>
        </div>


    </div>
</div></div>
<hr>

<form role="form" id="search_form" class="" action="{{ url('search-customers')}}" method="get">
<!-- <input type="hidden" name="_token" value="{{ csrf_token() }}"> -->

<div class="col-sm-3">
    <div class="input-group custom-search-form">
            <input type="text" class="form-control" id="customer_search" name="first_name" placeholder="Search Customers by name...">
            <span class="input-group-btn">
            <button class="btn btn-default" type="submit">
                    <i class="fa fa-search"></i>
            </button>
        </span>
    </div>
</div>

<div class="col-sm-3">
    <div class="input-group custom-search-form">
            <input type="text" class="form-control" id="mobile_number" name="mobile_number" placeholder="Search Customers by phone...">
            <span class="input-group-btn">
            <button class="btn btn-default" type="submit">
                    <i class="fa fa-search"></i>
            </button>
    </span>
    </div>
</div>
<br><br><br>
<div class="col-sm-3">
    <div class="input-group custom-search-form">
            <input type="text" class="form-control" id="branch" name="branch" placeholder="Search Customers by branch...">
            <span class="input-group-btn">
            <button class="btn btn-default" type="submit">
                    <i class="fa fa-search"></i>
            </button>
        </span>
    </div>
</div>

<div class="col-sm-3">
    <div class="input-group custom-search-form">
            <input type="text" class="form-control" id="customer_type" name="customer_type" placeholder="Search Customers by type...">
            <span class="input-group-btn">
            <button class="btn btn-default" type="submit">
                    <i class="fa fa-search"></i>
            </button>
    </span>
    </div>
</div>

        <input class="btn btn-primary" name="submit" type="submit" value="Search"/><br><br>
</form>

<div class="col-sm-12">
<div class="row">


    <!-- <form action="{{ url('customer/set_type')}}" class="" id="customer_set_type" method="post" role="form" /> -->

    <form action="" class="" id="customer_set_type" method="post" role="form" />

    <div style="position: absolute;top: -95px;right: 0;">
        <a class="btn btn-primary" href="{{ url('/customer/add') }}">Add new Customer</a>
        <input formaction="{{ url('customer/print_list')}}" class="btn btn-primary" name="print_data" type="submit" value="Print Data"/>
    </div><br><br><br>

    <div style="position: absolute;right: 0;margin-top: -105px;z-index: 999999;">
    <input name="_token" type="hidden" value="{{ csrf_token() }}">
    <input formaction="{{ url('customer/set_type')}}" class="btn btn-primary" name="mark_as_loyal" type="submit" value="Mark As Loyal Customers"/>
    <input formaction="{{ url('customer/set_type')}}" class="btn btn-primary" name="mark_as_regular" type="submit" value="Mark As Regular Customers"/><br><br>
    </div>

@if (Session::has('succMessage'))
   <div class="alert alert-success" style="position: absolute;margin-top: -50px;">{{ Session::get('succMessage') }}</div>
   @elseif (Session::has('errMessage'))
   <div class="alert alert-danger" style="position: absolute;margin-top: -50px;">{{ Session::get('errMessage') }}</div>
@endif

        @section ('cotable_panel_title','Customer List')
    <div class="col-sm-12">
        @section ('cotable_panel_body')

<!-- <div id="control-panel" onclick="cbclick(event)">
            <label><input type="checkbox" name="checkbox" id="checkbox-filter1"> Loyal Customers</label>&nbsp;&nbsp;
            <label><input type="checkbox" name="checkbox" id="checkbox-filter2"> Regular Customers</label>
</div> -->

        <table class="table table-hover table-striped table-bordered" id="customer_table">
            <thead>
                <tr>
                    <th><input type="checkbox" name="checkAll" id="checkAll"></th>
                    <th>Sr. No.</th>
                    <th>Name</th>
                    <th>Email</th>
                    <th>Address</th>
                    <th>Phone</th>
                    <th>Type</th>
                    <th>Total Orders</th>
                    <th><a href="{{ $purchase_sort }}" class="ui-btn ui-icon-refresh ui-btn-icon-left">Total Amount Purchased</a></th>
                    <th>Branch</th>
                    <th>Actions</th>
                </tr>
            </thead>
                <?php
$i = 1;
?>
            <tbody>
                <tr>
            @foreach ($customers as $key=>$customer)
                    <td><input type="checkbox" name="customer_details[]" class="checkItem" value="{{$customer->id}}|{{$customer->first_name}}|{{$customer->email}}|{{$customer->mobile_number}}|{{$customer->address}}"></td>
                    <td><?php echo $key + 1;?></td>
                    <td><a href="{{ URL::to('customer/edit?id='  . $customer->id) }}">{{ $customer->first_name }}</a></td>
                    <td style ="word-break:break-all;">{{ $customer->email }}</td>
                    <td style ="word-break:break-all;"><?php if (strlen($customer->address) > 25) {
    echo $customer->address = substr($customer->address, 0, 25) . "...";
}
?>
                    </td>
                    <td>{{ $customer->mobile_number }}</td>
                    <td>{{ ($customer->customer_type != "null") ? $customer->customer_type : "-" }}</td>
                    <td>{{ $customer->total_orders }}</td>
                    <td>{{ $customer->total_purchased_amount }}</td>
                    <td>{{ $customer->branch }}</td>
                    <td class="customer-list-actions">
                    <a class="btn btn-primary edit-btn-lime-green" href="{{ URL::to('customer/edit?id='  . $customer->id) }}"><i class="fa fa-edit"></i></a>
                    @if(Auth::user()->hasRole('owner'))
                    <a class="btn btn-primary" href="{{ URL::to('customer/purchase/purchase?customer_id=    '  . $customer->id) }}"><i class="fa fa-shopping-cart"></i></a>
                    <a class="btn btn-danger delete-btn-vivid-red" onclick="return confirm('Are you sure you want to delete this record?')" href="{{ URL::to('customer/delete/delete?delete_customer=   '  . $customer->id) }}"><i class="fa fa-trash-o"></i></a>
                    @endif
                    </td>
                </tr>
        <?php $i++;?>
            @endforeach
            </tbody>
        </table>
            <div style="position: absolute;right: 50px;">
                <input class="btn btn-primary" name="send_sms" type="submit" value="Send SMS"/>
                <input class="btn btn-primary" name="send_email" type="submit" value="Send Email"/><br><br>
            </div>
        <?php
            echo $customers->render();
        ?>

            <div style="position: absolute;right: 50px;">
                <input class="btn btn-primary" name="send_sms_to_all" type="submit" value="Send SMS to all customers"/>
                <input class="btn btn-primary" name="send_email_to_all" type="submit" value="Send Email to all customers"/><br><br>
                </div><br/><br/><br/><br/>
        @endsection
        @include('widgets.panel', array('header'=>true, 'as'=>'cotable'))
        </div>
    </div>
</div>
</form>
<script type="text/javascript">
$(".send_noti").click(function(e) {
    if (!$('input:checkbox').is(':checked')) {
        alert("Please select atleast one customer to send notification!");
        e.preventDefault();
        };

$("#checkAll").click(function() {
    $('input:checkbox').not(this).prop('checked', this.checked);
});

$('form#search_form').submit(function() {

    var input1 = $.trim($('#customer_search').val());
    var input2 = $.trim($('#mobile_number').val());
    var input3 = $.trim($('#branch').val());
    var input4 = $.trim($('#customer_type').val());

    if (input1 === '' && input2 === '' && input3 === '' && input4 === '') {
        alert('Please enter your search query!');
        return false;
    }
});

</script>
@stop

I have checked for all possible solution but each time if the admin hits the send sms or send email button it shows MethodNotAllowed error Here's the error shown below.

MethodNotAllowedHttpException in RouteCollection.php line 207:
in RouteCollection.php line 207
at RouteCollection->methodNotAllowed(array('GET', 'HEAD')) in RouteCollection.php line 194
at RouteCollection->getRouteForMethods(object(Request), array('GET', 'HEAD')) in RouteCollection.php line 142
at RouteCollection->match(object(Request)) in Router.php line 729
at Router->findRoute(object(Request)) in Router.php line 652
at Router->dispatchToRoute(object(Request)) in Router.php line 628
at Router->dispatch(object(Request)) in Kernel.php line 214
at Kernel->Illuminate\Foundation\Http\{closure}(object(Request))
at call_user_func(object(Closure), object(Request)) in Pipeline.php line 141
at Pipeline->Illuminate\Pipeline\{closure}(object(Request)) in VerifyCsrfToken.php line 43
at VerifyCsrfToken->handle(object(Request), object(Closure)) in VerifyCsrfToken.php line 17
at VerifyCsrfToken->handle(object(Request), object(Closure)) in Pipeline.php line 125
at Pipeline->Illuminate\Pipeline\{closure}(object(Request)) in ShareErrorsFromSession.php line 55
at ShareErrorsFromSession->handle(object(Request), object(Closure)) in Pipeline.php line 125
at Pipeline->Illuminate\Pipeline\{closure}(object(Request)) in StartSession.php line 61
at StartSession->handle(object(Request), object(Closure)) in Pipeline.php line 125
at Pipeline->Illuminate\Pipeline\{closure}(object(Request)) in AddQueuedCookiesToResponse.php line 36
at AddQueuedCookiesToResponse->handle(object(Request), object(Closure)) in Pipeline.php line 125
at Pipeline->Illuminate\Pipeline\{closure}(object(Request)) in EncryptCookies.php line 40
at EncryptCookies->handle(object(Request), object(Closure)) in Pipeline.php line 125
at Pipeline->Illuminate\Pipeline\{closure}(object(Request)) in CheckForMaintenanceMode.php line 42
at CheckForMaintenanceMode->handle(object(Request), object(Closure)) in Pipeline.php line 125
at Pipeline->Illuminate\Pipeline\{closure}(object(Request))
at call_user_func(object(Closure), object(Request)) in Pipeline.php line 101
at Pipeline->then(object(Closure)) in Kernel.php line 115
at Kernel->sendRequestThroughRouter(object(Request)) in Kernel.php line 84
at Kernel->handle(object(Request)) in index.php line 53

Upvotes: 1

Views: 737

Answers (3)

Emeka Mbah
Emeka Mbah

Reputation: 17545

First off: Next time post only relevant code. DON'T POST all your source code its rather confusing.

MethodNotAllowedHttpException means you are either making a POST to a GET route or vice-versa

These routes are expecting a 'POST' request and will throw MethodNotAllowedHttpException if they receive a GET request instead:

Route::get('/customer/send_sms', 'CustomerController@sendNotification');
Route::get('/customer/send_email', 'CustomerController@sendEmail');

By clicking these buttons below which are "Submit buttons" type=submit a post requests is fired to the server

<input class="btn btn-primary" name="send_sms" type="submit" value="Send SMS"/>
<input class="btn btn-primary" name="send_email" type="submit" value="Send Email"/>

Solution

Make your routes to accept POST requests like so:

Route::post('/customer/send_sms', 'CustomerController@sendNotification');
Route::post('/customer/send_email', 'CustomerController@sendEmail');

Point form action to the correct route URL

<form action="/customer/send_email" id="customer_set_type" method="post" role="form" />

Upvotes: 0

Sergio Rodrigues
Sergio Rodrigues

Reputation: 974

Your form is set with the POST method while the routes are as GET

<form action="" class="" id="customer_set_type" method="post" role="form" />

Route::get('/customer/send_sms', 'CustomerController@sendNotification');
Route::get('/customer/send_email', 'CustomerController@sendEmail');

Also note that all buttons are like submit to the same place. Maybe it's better to use JavaScript to send this requests.

Upvotes: 0

Alexey Mezenin
Alexey Mezenin

Reputation: 163788

If you want to sent email on click, change this:

<form action="" class="" id="customer_set_type" method="post" role="form" />

To this:

<form action="/customer/send_email" id="customer_set_type" method="post" role="form" />

And change route to:

Route::post('/customer/send_email', 'CustomerController@sendEmail');

If you want to send both SMS and emails, you'll need to create two forms or add some flag to the code.

Upvotes: 1

Related Questions