php - Laravel 后 Controller 不工作(Symfony\Component...)

标签 php laravel laravel-5 laravel-5.2

我的整个 Laravel Controller 无法正常工作。当我向这个 Controller index() 发出 get 请求时,它工作得很好。但是当我向这个 Controller 发出一个 post 请求到 store() 时,它不起作用。

当我尝试解决问题时,我开始注释掉代码或使用 dd()。然后很快注意到,当我注释掉我的整个 Controller 时,它没有对错误进行任何更改。 (或者当我 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();

我的 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 = 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");

}

} 错误: enter image description here

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

发帖请求时出错: enter image description here

最佳答案

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

转身

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

进入

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

然后在你的 Controller 中改变

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 中为红色),然后单击响应。查看错误并进行诊断。

example in chrome chrome 示例

你的情况

"message": "SQLSTATE[42S22]: Column not found: 1054 Unknown column 'updated_at' in 'field list' (SQL: insert into messages` (receive_id, message, send_id, updated_at, created_at) values (3, test, 1, 2018-08-08 13:00:54, 2018-08-08 13:00:54))"

要么将 $schema->timestamps() 添加到您的迁移文件中,要么在您的 Messages 模型中设置属性 public $timestamps = false;

关于php - Laravel 后 Controller 不工作(Symfony\Component...),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51746464/

相关文章:

php - 类 'App\Carbon\Carbon' 未找到 Laravel 5

laravel - CloudFlare SSL 证书无效

mysql - 将 Controller 在 Blade 文件中获取的值传递给查询

php - 使用 Laravel AWS SDK 在 Route53 中创建记录集时出错

php - Laravel phpunit 没有得到正确的 url

javascript - 稍后可以在函数中将值绑定(bind)到 Vue 元素吗?

php - 绑定(bind)变量的数量与 token 的数量不匹配

javascript - 如何重复通过ajax检索数据的文本字段

php - PHP 什么时候需要在创建类之前查看类?

php - 返回 Phalcon 模型中的虚拟列