Laravel 将参数从vue传递到php时出错获取null!!通过axios呼叫
chatcontroller.phpLaravel 将参数从vue传递到php时出错获取null!!通过axios呼叫,laravel,vuejs3,Laravel,Vuejs3,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,
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,
]);
}
Route::get('/CreateConversation',[ChatsController::class,'CreateConversation']);
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>
当我从vue console.log调用createconversation时,正确记录数据,但在php中为null
错误消息:“SQLSTATE[23000]:完整性约束冲突:1048列“user2\u id”不能为null
为什么为空我不知道帮帮我!!语法错误吗?您发送的是一个GET请求,而不是一个POST,请尝试
public function CreateConversation($to_id)
{
conversation::create(
[
'user_id' => auth()->user()->id,
'user2_id' => $to_id,
]);
}
是否已完成控制台日志(到\u id)显示一些值或它的null?是的,它显示了正确的值我通过了是的,工作得很好你能解释吗?为什么get-request deos不工作?谢谢如果你使用get-request,那么你就不需要request对象作为方法的参数。当你有POST请求时,会使用request$req。你的知识启发了我,谢谢