1

I have 2 controller in route file for get data from Ajax. i want to get data from ajax and handle that in simple controller by post method

unfortunately i get this error:

BadMethodCallException in Controller.php line 283: 

Method [sendSmsToUser] does not exist.

controllers :

Route::group(['middleware' => 'auth'], function () {
    Route::post('sendSmsToUser', 'NotificationsController@sendSmsToUser');
    Route::post('sendEmailToUser', 'NotificationsController@sendEmailToUser');
});

NotificationsController:

class NotificationsController extends Controller
{
    /**
     * @param Request $request
     * @return boolean
     */
    public function postSendSmsToUser(Request $request)
    {
        $info = User::find($request::input('user_id'));
        $send = SendSMS::sendSms($info->mobile_number, $request->input('message'));
        if ($send) {
            Log::info("sms successfull send to user id" + $request->input('user_id'));
            return true;
        } else {
            Log::emergency("sms dont send to user");
            return false;
        }
    }
}

Ajax Request:

$('[id^="send_sms-"]').click(function () {
    var id = $(this).attr('id').split('-');
    var message = $('#sms_message-' + id[1]).val();
    $.ajax({
        type: "POST",
        cache: false,
        encoding: "UTF-8",
        url: "{{ url('sendSmsToUser') }}",
        data: {user_id: id[1], message: message},
        success: function (data) {
        }
    });
    return false;
});
2
  • The method in the controller is called postSendSmsToUser but the route is looking for NotificationsController@sendSmsToUser isn't it? Do they need to match? Commented Dec 11, 2015 at 17:00
  • 1
    This is likely confusion between the methods Route::post() and Route::controller() where the method postSendEmailToUser() would have responded to that route. Commented Dec 11, 2015 at 17:42

1 Answer 1

1

The line Route::post('sendSmsToUser', 'NotificationsController@sendSmsToUser'); is calling the function sendSmsToUser in your controller. That method doesn't exist. It should probably read:

Route::post('sendSmsToUser', 'NotificationsController@postSendSmsToUser');

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.