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)