首页 > 解决方案 > 如何使用 Laravel Vue.js 在数据库中保存 user_id

问题描述

我正在创建使用 laravel、pusher 和 vue.js 在按钮单击时获取用户实时位置。一切正常,但我也插入了身份验证 ID,不知道如何在 laravel 中使用 vue.js 使用它。请告诉我如何使用 vue.js 保存用户身份验证 ID。

ExampleComponent.vue

<template>
<div class="container">
    <div class="row justify-content-center">
        <div class="col-md-8">
            <div class="card">
                <div class="card-header">Example Component</div>
                <button class="btn btn-success" @click="updateLocation">Update Position</button>
                <div class="card-body">

                    <div id="realtimemap" style="height: 412px; width: 100%;"></div>

                </div>
            </div>
        </div>
    </div>
</div>
</template>

<script>

     export default{

     data(){

        return{
            map:null,
            marker:null,
            center:{lat: 10, lng: 10},
            data:null,
            lineCoordinates:[]
        }
    },

    methods:{

        mapInit(){


            this.map = new google.maps.Map(document.getElementById('realtimemap'),{
                center: this.center,
                zoom: 8
            });

            this.marker = new google.maps.Marker({
                map: this.map,
                position: this.center,
                animation:"bounce",
            });
        },

        updateMap(){
            let newPosition = {lat:this.data.lat,lng:this.data.long};
            this.map.setCenter(newPosition);
            this.marker.setPosition(newPosition);

            this.lineCoordinates.push(new google.maps.LatLng(newPosition.lat,newPosition.lng));

            var lineCoordinatesPath = new google.maps.Polyline({
                path: this.lineCoordinates,
                geodesic: true,
                map: this.map,
                strokeColor: '#FF000',
                strokeOpacity: 1.0,
                strokeWeight: 2
            });
        },

        updateLocation(){

            let randomNumber=Math.random();

            let position={
                lat:10+randomNumber,
                long:10+randomNumber
            };

            axios.post('/api/map',position).then(response=>{
                console.log(response);
            })
        }

    },

    mounted() {
        console.log('Component mounted.');
        this.mapInit();
    },
    created(){
        Echo.channel('location')
            .listen('SendLocation', (e) => {
                this.data=e.location;

                this.updateMap();
                console.log(e);
        });
        }
       }
    </script>

欢迎.blade.php

<!doctype html>
<html lang="{{ app()->getLocale() }}">
<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <meta name="csrf-token" content="{{ csrf_token() }}">

    <title>Laravel</title>

    <!-- Fonts -->
    <link href="https://fonts.googleapis.com/css?family=Raleway:100,600" rel="stylesheet" type="text/css">

    <link rel="stylesheet" href="{{asset('css/app.css')}}">

</head>
<body>
    <div class="flex-center position-ref full-height">
        <!--@if (Route::has('login'))
            <div class="top-right links">
                @if (Auth::check())
                    <a href="{{ url('/home') }}">Home</a>
                @else
                    <a href="{{ url('/login') }}">Login</a>
                    <a href="{{ url('/register') }}">Register</a>
                @endif
            </div>
        @endif-->

        <div class="content">
            <div class="title m-b-md" id="app">
                <example-component></example-component>
            </div>
        </div>
    </div>

     <script async defer src="https://maps.googleapis.com/maps/api/js?key=AIzaSyDfRzdTIIRmsW1VaQiOzZCuRngBKqw3QUU&callback=initMap" type="text/javascript"></script>
    <script src="{{ asset('js/app.js') }}"></script>
</body>

我得到的错误信息

在此处输入图像描述

请帮我。

提前致谢。

标签: laravelvue.js

解决方案


将此添加到您的updateLocation().

let position={
   lat:10+randomNumber,
   long:10+randomNumber,
   user_id:id_user
};

推荐阅读