Reputation: 552
I have an API in laravel 5.4 and used Vue Resource to consume this backend. But when making the requests I bring all the information that is in the database and that is about 10k of questions. I would limit this for example to 100 questions and make a next request.
My api repository
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;
}
}
My Vue code
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
},
My Question Controller
//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())
]);
}
Upvotes: 0
Views: 1096
Reputation: 13259
Use pagination. Either change your all()
function or make a new function function.
public function pagination($limit = 100){
$data = $this->question->paginate($limit);
return $data;
}
More here: https://laravel.com/docs/5.4/pagination
Upvotes: 1