I'm working on a forum project using Laravel 8, and in this project, I have made a page for getting all the questions with specific tag. Here is the the method to do that:
public function tag($name)
{
$tag = \App\Models\Tag::findOrFail($name);
return view('tag',[
'single' => $tag
]);
}
And then inside tag.blade.php
:
@foreach($single->questions as $one)
...
@endforeach
But now I need to add pagination for this page, so if questions are more than a custom number, they can navigate to other pages.
In fact I had already added pagination to my other page which shows all the entire questions:
public function allQuestions()
{
$all = Question::latest()->paginate(20);
return view('questions.allquestions',[
'all' => $all
]);
}
But I don't know really how to add this feature to tag()
method.
So if you know how to add this pagination, please let me know...
I would really appreciate any idea or suggestion from you guys.
Thanks in advance.
Theres 2 choices for this:
First Choice:
public function tag($name)
{
$tag = \App\Models\Tag::findOrFail($name);
$questions = $tag->question()->paginate(10);
return view('tag',[
'single' => $tag, 'questions' => $questions
]);
}
Then, on your view, you can just loop through the questions.
@foreach ($questions as $question)
... fill this
@endforeach
{!! $questions->render() !!}
Second Choice:
public function tag($name)
{
$tag = \App\Models\Tag::findOrFail($name);
$tag->setRelation('questions',$tag->questions()->paginate(10));
return view('tag',[
'single' => $tag
]);
}
Then, on your view, you can just loop through the questions.
@foreach ($single->questions as $question)
... fill this
@endforeach
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.