如何使用vue js和laravel

时间:2016-05-27 14:08:48

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

使用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');
            });
        }
    }

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

1 个答案:

答案 0 :(得分:0)

如果我正确理解了您的问题,您希望从控制器中的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');
        });
    }
}