2017-03-21 13 views
0

私はAPIをlaravel 5.4に持っていて、このバックエンドを消費するためにVueリソースを使いました。しかし、リクエストをする際には、データベースにあるすべての情報を持っています。それは約10kの質問です。私はこれを例えば100の質問に限定し、次の要求をします。Api Lravelのリクエストのデータを制限する

マイAPIリポジトリ

class QuestionRepository implements QuestionRepositoryInterface 
    { 

     protected $question; 

     public function __construct(Question $question) 
     { 
      $this->question = $question; 
     } 

     public function all(){ 
      $data = $this->question->all(); 

      return $data; 
     } 

     public function find($id) 
     { 
      return $this->question->find($id); 
     } 

     public function findByTopic($id) 
     { 
      $questions = $this->question->where('topic_idtopic', '=', $id)->get(); 

      return $questions; 
     } 
    public function findBySubject($id) 
    { 
     $questions = $this->question->where('subject_idsubject', '=', $id)->get(); 

     return $questions; 
    } 
} 

マイVueのコード

mounted(){ 

    this.$http.get(window.api+'subject').then((response) => { 
     this.subjects = response.data 
    }); 

    this.$http.get(window.api+'questions/count').then((response) => { 
     this.count = response.body 
    }); 

    this.status = 1 
}, 

私の質問コントローラー

//Todas as questões 
    public function index() 
    { 
     $questions = $this->questionRepository->all(); 

     return Response::json([ 
      'questions' => $this->questionTransformer->transformCollection($questions->all()) 
     ]); 
    } 

    public function count() 
    { 
     $count = $this->questionRepository->all()->count(); 

     return Response::json($count); 
    } 

    public function show($id) 
    { 
     $questions = $this->questionRepository->find($id); 

     if(!$questions) return $this->responseNotFound('Question doesn\'t exist'); 

     return Response::json([ 
      'questions' => $this->questionTransformer->transformCollection($questions->all()) 
     ]); 
    } 
    public function showBySubject($id) 
    { 
     $questions = $this->questionRepository->findBySubject($id); 

     return Response::json([ 
      'questions' => $this->questionTransformer->transformCollection($questions->all()) 
     ]); 
    } 

答えて

1

使用ページネーション。 all()関数を変更するか、新しい関数関数を作成してください。ここ

public function pagination($limit = 100){ 
    $data = $this->question->paginate($limit); 
    return $data; 
} 

より:https://laravel.com/docs/5.4/pagination

+0

そして、何が要求がVueの中のように見えるのでしょうか? –

+0

ああ待ってください。あなたは 'count()'を呼び出すときにVueJSから総計を与えるので、 'all()'をそのまま残して、 'paginate()'という別の関数を作成しなければなりません。しかし、ページ番号はあなたに合計カウントを与えるでしょう。他の人と同じように呼び出すだけで、 'console.log(response)'を呼び出すだけで返ります。 – EddyTheDove

+0

ありがとう。あなたのコメントは私に多くの助けになります。 –

関連する問題