【问题标题】:How to get uri address of a nested resources using vue js and laravel如何使用 vue js 和 laravel 获取嵌套资源的 uri 地址
【发布时间】:2016-09-25 22:02:53
【问题描述】:

我在使用 vue js 时获取嵌套资源的 uri 时遇到问题。请参阅下面的代码。

ReservationController.php

public function index($id)
    {   

        $student = Student::with(['sectionSubjects','sectionSubjects.section', 
                    'sectionSubjects.subject'])->findOrFail($id);

        return $student->sectionSubjects;   

    }

all.js

methods:{

        fetchSubjects: function(){
            var self = this;
            this.$http({
                url: 'http://localhost:8000/reservation/id/student',
                method: 'GET'
            }).then(function (reservations){
                this.$set('reservations', reservations.data);
                console.log('success');

            }, function (response){
                console.log('failed');
            });
        }
    }

这里的问题是,获取这个urlhttp://localhost:8000/reservation/id/student的id的值。有没有办法获取 index() 方法的参数的 id?这也是我的路线列表http://imgur.com/8MWaCpO

【问题讨论】:

  • 您的应用需要知道您尝试向其发送请求的 ID,因此您需要通过 http 请求获取预订。
  • @Jeff 是的,这是我的问题,我无法在获取 uri 之前找到获取该 ID 的方法。你能给我举个例子吗?

标签: laravel laravel-5 laravel-5.1 laravel-5.2 vue.js


【解决方案1】:

如果我正确理解了您的问题,您希望从控制器中的 index 方法返回学生 ID?

如果是这样,您需要在控制器中这样做:

public function index($id)
{

    $student = Student::with(['sectionSubjects','sectionSubjects.section',
                'sectionSubjects.subject'])->findOrFail($id);

    return [
        'student_id' => $id,
        'reservations' => $student->sectionSubjects
    ];

}

并像这样在您的 JS 上检索学生 ID:

methods: {

    fetchSubjects: function() {
        var self = this;
        this.$http({
            url: 'http://localhost:8000/reservation/id/student',
            method: 'GET'
        }).then(function (response) {
            this.$set('studentId', response.student_id); // <-- change here
            this.$set('reservations', response.reservations.data); // <-- change here
            console.log('success');
        }, function (response) {
            console.log('failed');
        });
    }
}

【讨论】:

  • 这将失败,因为 id 将未定义 - localhost:8000/reservation/id/student'
  • 好的,我误解了你的问题,你能显示你用来加载调用 fetchSubjects 的页面的代码吗?理想情况下,您的控制器和视图代码。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2022-01-25
  • 2012-05-05
  • 2014-09-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多