0

I'm getting the following error:

Symfony \ Component \ HttpKernel \ Exception \ MethodNotAllowedHttpException
No message

I've seen many posts on this error but nothing has helped.

This is my laravel controller:

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Models\Messages;
use App\User;
use Auth;

    class MessagesController extends Controller
    {
        public function index()
        {
            // We need to be able to see each user that has corresponded with this particular user. And only display them once on their users list.
            // Hence we made a 'correspondence_id' so we can filter that later on in vue.

            // Grab current user.
            $user_id = Auth::user()->id;

            // Grab all messages related to this user.
            $messages = Messages::where('send_id', $user_id)->orWhere('receive_id', $user_id)->get();

            foreach($messages as $message) {

                // for each message we want to grab the first and last name of the person we received or send the message to.
                if($user_id == $message['send_id']) {
                    // User_id is my id, so we don't want that name.
                } else {
                    // We want to grab their name.
                    $user = User::where('id', $message['send_id'])->first();

                    // Add this user to the message.
                    $message['firstname'] = $user['firstname'];
                    $message['lastname'] = $user['lastname'];
                    // Add profile_img url.
                    $message['profile_img'] = $user['profile_img'];
                    // Add id of user you are speaking to.
                    $message['correspondence_id'] = $message['send_id'];
                }

                if($user_id == $message['receive_id']) {
                    // User_id is my id, so we don't want that name.
                } else {
                    // We want to grab their name.
                    $user = User::where('id', $message['receive_id'])->first();

                    // Add his first and last name to the message.
                    $message['firstname'] = $user['firstname'];
                    $message['lastname'] = $user['lastname'];

                    // This should have the image of the profile who is receiving the image (not the other user).
                    $currentUser = User::where('id', $message['send_id'])->first();
                    $message['profile_img'] = $currentUser['profile_img'];

                    // Add id of user speaking to you.
                    $message['correspondence_id'] = $message['receive_id'];

                }

            }

            return compact('messages');

        }

        public function store(Request $request,$receive_id, $message)
        {
            // Grab current user.
            $user_id = Auth::user()->id;

            $messages = new Messages();

            $messages->fill($request->all());
            $messages->send_id = $user_id;
            $messages->receive_id = $receive_id;
            $messages->message = $message;

            $messages->save();

            $text = "Message stored";

            return compact("text");

        }

    }

Route:

Route::post('/sendmessage/{receive_id}/{message}','MessagesController@store');

Db structure: id, send_id, receive_id, created_at, message The id and created_at are filled in automatically.

Post Request from Axios (vuex)

sendMessage({ commit }, payload){
        var receive_id = payload.receive_id;
        var message = payload.message;
        console.log(payload)

        axios.post('/sendmessage/'+receive_id+'/'+message, {
        }).then(function (response) {
            console.log(commit);
            console.log("success");
        }).catch((response) => {
            // Get the errors given from the backend
            let errorobject = response.response.data.errors;
            for (let key in errorobject) {
                if (errorobject.hasOwnProperty(key)) {
                    console.log(errorobject[key]);
                    this.backenderror = errorobject[key];
                }
            }
        })
    }

The data is correctly being sent to the request, and when I run it in vue I see this error in the console:

app.js:26133 POST http://mysite.local/sendmessage/2/test 500 (Internal Server Error)
Otto
  • 663
  • 3
  • 17
  • 33
  • 1
    Please add your entire routes file. I'm pretty sure there's another route conflicts with this one – Ravisha Hesh Aug 07 '18 at 16:23
  • How are you sending the request? – Devon Bessemer Aug 07 '18 at 16:25
  • @Devon The request is sent an axios request from vuex. However I've also tried going to the url directly (with the correct information in url)., and it also doesn't work. – Otto Aug 07 '18 at 17:33
  • Can you add the code of how you initiating the request – Seva Kalashnikov Aug 07 '18 at 17:38
  • Do you have a route like `Route::get('/sendmessage/{anything},'...` – Ravisha Hesh Aug 07 '18 at 17:43
  • @Otto well, show how you make the request... I asked for a reason. Just visiting that page wouldn't be a POST request, so that makes sense. It seems odd that message is a route parameter here... – Devon Bessemer Aug 07 '18 at 18:05
  • @Devon I added how I make the request above, sorry it is late here! Thanks for the help (it is an axios post request from vuex). – Otto Aug 07 '18 at 20:19
  • @Otto, so you're actually showing a different error there. You have a 500 error in your AJAX request, not a 405. Seems like the route is working, but you have an error in your controller possibly. – Devon Bessemer Aug 07 '18 at 20:20
  • @Devon I've added my entire controller, but I can't seem to find an error:/ can you? – Otto Aug 08 '18 at 07:59

0 Answers0