2017-03-21 1 views
0

Laravel 5.4에 API가 있고이 백엔드를 사용하기 위해 Vue 리소스를 사용했습니다. 그러나 요청을 할 때 나는 데이터베이스에있는 모든 정보를 가져오고 그 질문은 약 10k입니다. 예를 들어 100 개의 질문으로 제한하고 다음 요청을하십시오.API의 Larvel 요청 데이터 제한

내 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; 
    } 
} 

내 뷰 코드

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

그리고 요청이 뷰에서처럼 무엇을 볼 것인가? –

+0

오 기다려 라. VueJS에서'count()'를 호출하면 총 개수를 얻을 수 있기 때문에'all()'을 그대로두고 paginate()라는 또 다른 함수를 만들어야한다. 그러나 페이지 매김 (paginate)은 전체 카운트를 제공합니다. 단순히 다른 사람들에게 전화하는 것과 같은 방식으로 호출하고'console.log (response)'를 호출하면 리턴되는 내용을 볼 수 있습니다. – EddyTheDove

+0

많이 감사합니다. 귀하의 의견을 많이 도와주세요. –