【问题标题】:Can I use a PHP (laravel) controller directly inside my VueJS view?我可以直接在我的 VueJS 视图中使用 PHP (laravel) 控制器吗?
【发布时间】:2019-09-06 17:19:15
【问题描述】:

我添加了一个带有 VueJS 搜索结果的搜索栏,这非常适合简单的查询。现在,我想做同样的事情,但使用更复杂的过滤器。

这是我的 VueJS 搜索组件

<button class="btn btn-primary" @click="search" @keyup.enter="search" type="button">Search</button>

methods: {
            search() {
                axios.get('/races/find?q=%'+ this.query+'%').then(
                    response => {
                        this.races=response.data;
                    }
                );
            }
        }

在 web.php 中

Route::get('/race/find', 'RacesController@searchRaces');

在 RacesController 中

public function searchRaces(Request $request)
{
    return DB::connection('mysql1')->select(REQUEST);
}

这很好用。

现在我将有一些更复杂的过滤器,包括日期、滑块和多个数据。所以我不能直接在我的查询中传递它,否则我会有一个包含 15 个值的超长请求...... 是否可以在我的 Vue 视图中直接调用我的 Laravel 控制器?

提前非常感谢

【问题讨论】:

    标签: php vue.js laravel-5 vuejs2


    【解决方案1】:

    你不能。控制器的方法需要通过 HTTP 调用。复杂的过滤器,如日期等是典型的,所以不用担心。将您的查询更改为发布,并为其分配数据:

    search() {
        let filters = {
            query: this.query,
            date: this.date,
            // ...
        };
        axios.post('/races/find', filters).then(
            response => {
                this.races=response.data;
            }
        );
    }
    

    web.php 中的路由:

    Route::post('/race/find', 'RacesController@searchRaces');
    

    【讨论】:

    • 非常感谢,我猜我的大脑卡在某个地方了:$
    猜你喜欢
    • 2021-09-03
    • 2016-10-09
    • 1970-01-01
    • 2013-06-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多