【问题标题】:Passing parameter from vue to php error getting null!! via axios call将参数从 vue 传递给 php 错误得到 null !通过 axios 调用
【发布时间】:2021-03-22 16:27:49
【问题描述】:

chatcontroller.php

public function CreateConversation(Request $req)
{
    $user_id = Auth::user()->id;
    echo $req->to_id;
    conversation::create(
        [
            'user_id'  => $user_id,
            'user2_id' => $req->to_id,
        ]);
}

Createconversation.vue

<script>
export default{
components:
{
    AppLayout,
},
data()
{
    return{
       users: [],
       to_id: '',
    }
},

methods:
{
    
    CreateConversation(to_id)
    {
        this.to_id = to_id;
        console.log(to_id);
        axios.get('CreateConversation',{to_id: this.to_id,});
    }
}
}
</script>

Web.php

Route::get('/CreateConversation',[ChatsController::class,'CreateConversation']);

当我从 vue 控制台调用 createconversation 时,正确记录数据但在 php 中为空

错误消息:“SQLSTATE[23000]:违反完整性约束:1048 列 'user2_id' 不能为空

为什么它是空的,我不知道帮帮我!!语法错了吗?

【问题讨论】:

  • console.log(to_id) 是显示了某个值还是它的空值?
  • 是的,它显示了我通过的正确值

标签: laravel vuejs3


【解决方案1】:

您发送的是 GET 请求而不是 POST,所以试试吧。

public function CreateConversation($to_id)
{
    conversation::create(
        [
            'user_id'  => auth()->user()->id,
            'user2_id' => $to_id,
        ]);
}

【讨论】:

  • 是的,效果很好,你能解释一下吗?为什么 get request deos 不起作用?谢谢
  • 如果你使用 GET 请求,那么你不需要请求对象作为你方法的参数。
  • 那个Request $req在你有一个POST请求的时候使用。
  • 你的知识让我很受启发,谢谢
猜你喜欢
  • 1970-01-01
  • 2018-10-03
  • 1970-01-01
  • 2017-04-14
  • 1970-01-01
  • 1970-01-01
  • 2018-03-13
  • 2020-11-06
  • 2018-05-20
相关资源
最近更新 更多