我遇到以下错误:
Symfony \ Component \ HttpKernel \ Exception \ MethodNotAllowedHttpException
No message
我已经看到很多关于此错误的帖子,但没有任何帮助。
这是我的laravel 控制器:
<?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::post('/sendmessage/{receive_id}/{message}','MessagesController@store');
Db结构:
id
,send_id
,receive_id
,created_at
,message
id和created_at将自动填写。
来自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];
}
}
})
}
数据已正确发送到请求,并且在vue中运行它时,我在控制台中看到此错误:
app.js:26133 POST http://mysite.local/sendmessage/2/test 500 (Internal Server Error)
答案 0 :(得分:0)
这个问题的答案可以在类似的帖子中找到:
[Laravel Post Controller not working (Symfony \ Component...)