phplaravelsearchpagination

search with pagination is not working in laravel


I make it in 1 route with in route

Route::get('/all-students', 'AdminController@studentList')->name('admin.student');
Route::post('/all-students', 'AdminController@searchByClassRoll')->name('search-by-class-roll');

in controller funtion student list

public function studentList(Request $request)
{
    Session::put('url.intended2', URL::current());
    Session::put('url.intended', URL::previous());

    if (isset($request->class)) {
        if (isset($request->roll)) {
            if ($request->class == 'all') {
                $students = DB::table('students')
                    ->where('roll', '=', $request->roll)
                    ->orderBy('id', 'DESC')
                    ->paginate(20);
            } else {
                $students = DB::table('students')
                    ->where('roll', '=', $request->roll)
                    ->where('class', '=', $request->class)
                    ->orderBy('id', 'DESC')
                    ->paginate(20);
            }
        } else {
            if ($request->class == 'all') {
                return redirect()->route('admin.student');
            } else {
                $students = DB::table('students')
                    ->where('class', '=', $request->class)
                    ->orderBy('id', 'DESC')
                    ->paginate(20);
            }
        }
    } else {
        $students = DB::table('students')
            ->orderBy('id', 'DESC')
            ->paginate(20);
    }


    return view('admin.student-list')->with('students', $students);
}

post method

public function searchByClassRoll(Request $request)
{
    $class = $request->class;
    $roll = $request->roll;
    if (isset($request->class)) {
        if (isset($request->roll)) {
            return redirect()->route('admin.student', ['class' => $class, 'roll' => $roll]);
        } else {
            return redirect()->route('admin.student', ['class' => $class]);
        }
    }
}

in blade

@if(method_exists($students,'links'))
    {!! $students->links() !!}
@endif

normally when I click page 2 it returns all-students?page=2 when I search something all-students?class=2 and then i click on pagination page it returns again all-students?page=2 and remove the search data from URL.

form from view

<form action="{{ route('search-by-class-roll') }}" method="post">
    {{ csrf_field() }}
    <input type="hidden" name="type" value="student_list">
    <div class="row gutters-8">
        <div class="col-4-xxxl col-xl-4 col-lg-3 col-12 form-group">
            <div class="ui-alart-box">
                <div class="default-alart">
                    <div class="result" role="alert">
                        @if(app('request')->input('class') || app('request')->input('roll'))
                            <a href="{{route('admin.student')}}">See All</a><span> | </span>
                        @endif
                        Showing results {{$students->count()}} of {{$students->total()}} entries
                    </div>
                </div>
            </div>
        </div>
        <div class="col-3-xxxl col-xl-3 col-lg-3 col-12 form-group">
            <input type="number" name="roll" placeholder="Search by Roll..." class="form-control">
        </div>
        <div class="col-4-xxxl col-xl-3 col-lg-3 col-12 form-group">
            <select class="select2 form-control" name="class" required>
                <option value="all">All Classes</option>
                <option value="baby">baby</option>
                <option value="nursery">nursery</option>
                <option value="1">one</option>
                <option value="2">two</option>
                <option value="3">three</option>
                <option value="4">four</option>
                <option value="5">five</option>
                <option value="6">six</option>
                <option value="7">seven</option>
                <option value="8">eight</option>
                <option value="9">nine</option>
                <option value="10">ten</option>
            </select>
        </div>
        <div class="col-1-xxxl col-xl-2 col-lg-3 col-12 form-group">
            <button type="submit" class="fw-btn-fill btn-gradient-yellow">SEARCH</button>
        </div>
    </div>
</form>

i want to made this because I want this in 1 URL just because if someone manually entered the URL with search data he can see the output. but in here I can't get the result with pagination from blade. when I turned into page 2 it remove the search variable from URL. and paginate the whole data. how can I get result with pagination after search?


Solution

  • Coming directly to your controller. You don't need any other route or method you can handle this filtering on same route and in same method.

    public function studentList(Request $request)
    {
        if(!empty($request)){
            $query = $request->all();
           $students = DB::table('students')->orderBy('id', 'DESC');
           if(isset($request->class) AND $request->class != '' AND $request->class != 'all')
               $students = $students->where('class', '=', $request->class);
           if(isset($request->roll) AND $request->roll != '')
               $students = $students->where('roll', '=', $request->roll);
           $students = $students->paginate(20);
    
            return view('admin.student-list', compact('students','query'));
        }
    
        $students = DB::table('students')
                    ->orderBy('id', 'DESC')
                    ->paginate(20);
       return view('admin.student-list', compact('students'));
    }
    

    Change few things in your view

    <form action="{{ route('admin.student') }}" method="get"> change route and method to get
    {{ csrf_field() }} //remove this
    

    Below your table inside view write this code. The appends() is most important otherwise pagination in your filtered data won't work.

    @if(isset($query))
      {{ $students->appends($query)->links() }}
    @else
      {{ $students->links() }}
    @endif