【问题标题】:laravel + vue count number of viewslaravel + vue 统计浏览次数
【发布时间】:2021-07-30 09:19:19
【问题描述】:

我有一个基于 Laravel 和 Vue 的文章网站,我想统计每篇文章的访问次数(当用户打开文章时,访问次数增加一),在 Laravel 中我使用了 eloquent-viewable 包,但是使用 Laravel + Vue 我不知道该怎么做。

你能帮帮我吗

【问题讨论】:

    标签: laravel vue.js


    【解决方案1】:

    你可以试试这个方法

    在你的挂载事件中注册这个函数

    mounted() {
            
            this.view();
        },
    methods: {
            view() {
                axios.get('/home/food_view/' + this.$route.params.id).then((response) => {
                    if (response.data.error)
                        Toast.fire({
                            icon: 'error',
                            title: response.data.error,
                        })
                });
            },
        }
    

    并在您的迁移中创建一个列

    $table->integer('view')->default(0); 
    

    然后在你的控制器中做类似的事情

    $model = Model::find($id);
    $model->view += 1;
    $model->save();
    

    更新你可以在 route.js 中使用这些路由

    {
                    path: '/food_details/:id/:slug',
                    name: 'food_details',
                    component: foodDetails
                },
    

    【讨论】:

    • 你能在API文件中写下Route应该是怎样的吗?
    猜你喜欢
    • 2020-10-27
    • 2012-09-29
    • 1970-01-01
    • 2021-12-11
    • 2010-11-02
    • 2019-09-02
    • 1970-01-01
    • 1970-01-01
    • 2021-12-25
    相关资源
    最近更新 更多