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())
]);
}
そして、何が要求がVueの中のように見えるのでしょうか? –
ああ待ってください。あなたは 'count()'を呼び出すときにVueJSから総計を与えるので、 'all()'をそのまま残して、 'paginate()'という別の関数を作成しなければなりません。しかし、ページ番号はあなたに合計カウントを与えるでしょう。他の人と同じように呼び出すだけで、 'console.log(response)'を呼び出すだけで返ります。 – EddyTheDove
ありがとう。あなたのコメントは私に多くの助けになります。 –