Paginate - items per page Hello guys,
I have question about items per page.
public function index(TaskRepository $taskRepo)
{
if(Auth::user()->type != 'admin'){
return redirect()->route('login');
}
$perPage = 2;
if(request()->has('status')) {
$tasks = DB::table('tasks')->where('status', request('status'))->orderBy('id','asc')->paginate($perPage)->appends('status', request('status'));
} else {
$tasks = DB::table('tasks')->orderBy('id','asc')->paginate($perPage);
}
//$tasks = $taskRepo->getAll();
return view('tasks.list', [
"taskList" => $tasks
]);
}
now I have variable $perPage =2; but how can I pass number from view to the controller on change value?
view select:
<form>
<div class="form-group">
<label for="perPage">Example select</label>
<select class="form-control" id="perPage">
<option>5</option>
<option>10</option>
<option>15</option>
<option>20</option>
<option>25</option>
</select>
</div>
</form>
Thanks for any help.
Submit your form as a GET request so that the selected perPage value is appended to the query string. Make sure you have a name attribute on the <select> element.
<form class="form-inline" method="GET" role="form">
<div class="form-group">
<label for="perPage">Example select: </label>
<select class="form-control" id="perPage" name="perPage">
<option>5</option>
<option>10</option>
<option>15</option>
<option>20</option>
<option>25</option>
</select>
</div>
</form>
ok I got it, but still not working
That's because you are explicitly setting the $perPage value in the controller. You should optionally take the request value or the default:
$perPage = request('perPage', 2); // 2 is the default if there is no perPage in the request
I added request, but it is still not working. Hmm
echo $perPage return result as number 2.
Does your URL have a query string ?perPage=15 (or whatever you selected from dropdown?
missing form action ?
dd($request->all()) see the perPage ?
dd($perPage->all());
error:
Call to a member function all() on integer
what should be in form action??
oh wait, the <form> code is inside tasks/list.blade.php ?
<form class="form-inline" method="GET" action="{{ route('type you route here') }}" role="form">
<div class="form-group">
<label for="perPage">Example select: </label>
<select class="form-control" id="perPage" name="perPage">
<!-- all option missing value="" -->
<!-- example -->
<option value="5">5</option>
<option value="10">10</option>
<option value="15">15</option>
<option value="20">20</option>
<option value="25">25</option>
</select>
</div>
</form>
after modifyed the form, check all the variable is passed to your index
public function index(Request $request, TaskRepository $taskRepo)
{
dd($request->all);
Why are you calling all() on $perPage?
The form should make a GET request to the same URL; all it is doing is adding a query string with the number of items your paginator should display on each page:
<form class="form-inline" method="GET" action="{{url()->current()}}">
<div class="form-group">
<label for="perPage">Example select: </label>
<select class="form-control" id="perPage" name="perPage">
<option>5</option>
<option>10</option>
<option>15</option>
<option>20</option>
<option>25</option>
</select>
</div>
</form>
In the controller:
public function index(TaskRepository $taskRepo)
{
if(Auth::user()->type != 'admin'){
return redirect()->route('login');
}
$perPage = request('perPage', 2);
if(request()->has('status')) {
$tasks = DB::table('tasks')
->where('status', request('status'))
->orderBy('id','asc')
->paginate($perPage)
->appends('status', request('status'));
} else {
$tasks = DB::table('tasks')->orderBy('id','asc')->paginate($perPage);
}
//$tasks = $taskRepo->getAll();
return view('tasks.list', ["taskList" => $tasks]);
}
Please sign in or create an account to participate in this conversation.