2016-05-27 7 views
0

vue jsを使用する場合、ネストされたリソースのURIを取得する際に問題が発生しています。下記のコードを参照してください。ここvue jsとlaravelを使用してネストされたリソースの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'); 
      }); 
     } 
    } 

問題は、このURL http://localhost:8000/reservation/id/studentのIDの値を取得し、です。 index()メソッドのパラメータのidを取得する方法はありますか?私のルートリストもありますhttp://imgur.com/8MWaCpO

+0

アプリを知っている必要がありますあなたがリクエストを送信しようとしているIDなので、httpリクエストで予約を取得する必要があります。 – Jeff

+0

@Jeffええ、それは私の問題です。私はそのIDをフェッチする前に、URIを取得する方法を見つけることができません。あなたは私に例を挙げることができますか? – Jearson

答えて

0

あなたの質問を正しく理解していれば、あなたのコントローラのindexメソッドからStudent 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'); 
     }); 
    } 
} 
+0

これはidが未定義であるため正しく実行されません - http:// localhost:8000/reservation/id/student ' – Jearson

+0

あなたの質問に間違いがあると思いますが、fetchSubjectsが呼び出されたページを読み込むコードを表示できますか?理想的には、コントローラとビューコード。 –

関連する問題