当前位置: 首页 > 知识库问答 >
问题:

Laravel邮政控制器不工作(Symfony\Component...)

赫连黎昕
2023-03-14

我的整个laravel控制器都坏了。当我对这个控制器index()执行get请求时,它工作得非常好。但当我向这个控制器发出post请求以存储()时,它就不起作用了。

当我试图解决问题时,我开始注释代码或使用dd()。然后很快注意到,当我注释掉我的整个控制器时,它对错误没有任何改变。(或者当我dd($user_id)时,什么都没改变)。

我的错误:

Symfony \ Component \ HttpKernel \ Exception \ MethodNotAllowedHttpException
No message

路由文件:

<?php

Route::get('/', function () {
    return view('welcome');
});


Route::get('/test','TestController@index');

Auth::routes();

Route::get('/home', 'HomeController@index')->name('home')->middleware('auth');
Route::get('/inspirations','InspirationsController@index')->middleware('auth');
Route::get('/spaces','SpacesController@index');
Route::get('/user/{id}','UserController@index'); // other profiles
Route::get('/user','UserController@myprofile'); // my profile
Route::get('/mymessages','MessagesController@index'); // messages


Route::get('/testauth/', function()
{
    var_dump(Auth::user()->id);
    // your code here
});

Route::post('/pins/{inspiration_id}/{room_id}','PinsController@store')->middleware('auth');
Route::post('/editRoom/{id}/{name}/{description}','RoomsController@update');
// how i was doing it --> Route::post('/sendmessage/{receive_id}/{message}','MessagesController@store');
Route::post('/sendmessage','MessagesController@store');


Auth::routes();

我的控制器:

    <?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 = post('id');
    $message = post('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");

}

我的post请求通过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];
                }
            }
        })
    }

**帖子请求的更改(由Tschallacka询问)**

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

        axios.post('/sendmessage', { receive_id: receive_id, message: 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];
                    }
                }
            })}

共有1个答案

麻烨
2023-03-14

不要将POST请求用作GET请求。您可能会遇到浏览器对URL长度的限制。

转弯

axios.post('/sendmessage/'+receive_id+'/'+message, {

进入

axios.post('/sendmessage', { id: receive_id, message: message })

然后在控制器中进行更改

public function store(Request $request,$receive_id, $message)

public function store(Request $request)
{
    $receive_id = $request->input('id');
    $message = $request->input('message');

要排除任何其他错误,请打开开发控制台。按F12。单击网络选项卡并选择XHR日志记录。

提出请求。它将显示为错误500请求。单击文件名(chrome中的红色)并单击响应。查看错误并进行诊断。

在你的情况下

“消息”:“SQLSTATE[42S22]:未找到列:字段列表中的1054未知列‘更新的地址’”(SQL:插入消息“”(接收、消息、发送、更新的地址、创建的地址)值(3,测试,2018-08-08 13:00:542018-08-08 13:00:54))

添加$schema-

 类似资料:
  • 我真的很难理解JavaFX控制器,我的目标是写一个TextArea来充当日志。 我的代码在下面,但我希望能够改变值等从另一个类,我可以调用时需要。我试图创建一个扩展初始化的控制器类,但我无法使它工作。有谁能指引我正确的方向吗?

  • 嗨,我用spring初始化器创建了一个简单的Spring Boot应用程序。我在主应用程序类的同一文件夹中添加了一个控制器。 这是给我以下错误的网址http://localhost:8080/welcome 此应用程序没有针对/错误的显式映射,因此您将其视为回退。 Sat Dec 19 12:51:44 IST 2020出现意外错误(类型=未找到,状态=404)。 如果我使用@restContro

  • 我想用这段代码买它在程序中保持不变,特别是在你喜欢的时候。。。。 Jquery: Html 路线 控制器 当我点击“Calcula”按钮“Procesando,espere por favor…”屏幕上显示apears,但它不加载成功代码,有解决方案吗?? 谢谢!!

  • 我变得非常困惑,我试图通过laravels query builder运行一个应该可以工作的查询,但它会抛出奇怪的错误。

  • 我想允许一个域的跨源请求。我的项目使用Spring,所以我想利用新的CORS支持。 我遵循下面的示例https://spring.io/blog/2015/06/08/cors-support-in-spring-framework#disqus_thread并尝试了第一个版本。我的控制器注释如下所示: 如果我理解正确的话,mvc-config是一种替代方法。我也试过了: 对于这两种方法,响应似乎