Ad

Paginate Doesn't Work As Expected Uisng Laravel And Jq

I am working on a filtering data. As there is <input> and if someone start typing I am calling jq function to send a request to my controller. It is working fine. Even I am getting filtered data as well. But if I click on page 2 then it affects a design. So then after I got to know it is change my url.

As an example - After filtering data (This works fine)

code.test/?page=1

But if you click on page 2, it is redirecting to

code.test/filter?page=2

Here is my code for main.blade-

<div class="container">
        <div class="row">
            <div class="col-md-9 col-sm-12">
                <div class="form-group">
                    <input type="text" class="form-control" id="search" placeholder="Enter email" name="email">
                </div>
            </div>
            <div class="col-md-3 col-sm-12">
                <div class="form-group">
                    <select id="department">
                        <option value="0">All Departments</option>
                        @foreach($department as $d)
                        <option value="{{$d->id}}">{{$d->name}}</option>
                        @endforeach
                    </select>
                </div>
            </div>
        </div>
        <div id="filter">
            <div class="row">
                <?php
                    $count = count($data);//dd($data[0]->fname);
                ?>
                @if($count > 0)
                    @foreach($data as $d)
                        <div class="col-md-12">
                            {{$d->fname}}, {{$d->lname}}<br>
                            {{$d->profile}}<br>
                            <b>{{$d->departments->name}}</b>
                        </div>
                    @endforeach
                @else
                    No data found
                @endif
            </div>
        </div>
        {{ $data->appends($data)->links() }}
    </div>

This is my jq function -

function filter(){
    var str = $("#search").val();
    var dep = $('#department option:selected').val();
    // /alert(dep);
    $.ajaxSetup({
        headers: {
            'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
        }
    });
    $.ajax({
        type: "GET",
        url: '/filter',
        data: {
            str: str,
            dep: dep,
        },
        success: function(data){
            console.log(data);
            $('#filter').html(data);
        },
    });
}

$(document).ready(function(){
    $("#search").on('input', function(){
        filter();
    });
    $("#department").change(function () {
        filter();
    });
});

Now in controller I am returning a view after filter-

public function filter(Request $request){
        $str    =   $request->str;
        $dep    =   $request->dep;//dd($dep);

        $s  =   new Staff;
        $d  =   new Department;

        //If input and dropdown values are available
        if($str != null && $dep != 0){
            $data = $s::with('departments')
                        ->where('department', $dep)
                        ->where(function($q) use ($str) {
                            $q->where('fname', 'like', '%'.$str.'%')
                            ->orWhere('lname', 'like', '%'.$str.'%');
                        })
                        ->paginate(10)
                        ->appends(['dep'=> $dep, 'str'=> $str]);

            $data_count = count($data);

            return view('search', compact('data', 'data_count'));

        } else if($str != null && $dep == 0){ //If input value is set and dropdown value set to all departments
            $data = $s::with('departments')
                        ->where(function($q) use ($str) {
                            $q->where('fname', 'like', '%'.$str.'%')
                            ->orWhere('lname', 'like', '%'.$str.'%');
                        })
                        ->paginate(10)
                        ->appends(['dep'=> $dep, 'str'=> $str]);

            $data_count = count($data);

            return view('search', compact('data', 'data_count'));

        } else if($str == null && $dep != 0){ //If dropdown value is not null and input is null
            $data = $s::with('departments')
                        ->where('department', $dep)
                        ->paginate(10)
                        ->appends(['dep'=> $dep, 'str'=> $str]);

            $data_count = count($data);

            return view('search', compact('data', 'data_count'));

        } else if($str == null && $dep == 0){ //If dropdown value is null and input is null
            $data = $s::with('departments')->paginate(10)->appends(['dep'=> $dep, 'str'=> $str]);

            $data_count = count($data);

            return view('filter', compact('data', 'data_count'));
        }
    }

Please help me out with this.

Thank you in advance.

Ad

Answer

It's bit a late to answer but this may help you -

  1. In your main.blade instead of loading data directly you can pass that data to other view and then @include that new blade file to main.blade . Like this -
<div class="ui-block">
            <div class="ui-block-content">
                <div class="row">
                    <div class="col col-xl-9 col-lg-9 col-md-9 col-sm-12 col-12">
                        <div class="form-group">
                            <input type="text" class="form-control" id="search" placeholder="Start typing keywords.." name="search">
                        </div>
                    </div>
                    <div class="col col-xl-3 col-lg-3 col-md-3 col-sm-12 col-12">
                        <div class="form-group">
                            <select id="department" class="form-control">
                                <option value="0">All Departments</option>
                                @foreach($department as $d)
                                <option value="{{$d->id}}">{{$d->name}}</option>
                                @endforeach
                            </select>
                        </div>
                    </div>
                </div>        
            </div>
        </div>

        <div id="filter">
            @include('search')
        </div>
  1. Create new blade as an example search.blade and paste whatever you have in <div id="filter"> /* ---- This lines ---*/ in search.blade.

  2. In you js function change url of main.blade -

$.ajax({
        type: "GET",
        url: '/',//change this to main.blade's url
        data: {
            str: str,
            dep: dep,
        },
        success: function(data){
            console.log(data);
            $('#filter').html(data);
        },
    });
  1. Make changes in controller. Just check whether request is from ajax or not and then process that data.
public function main(Request $request)
    {
        $data = Staff::with('departments')->orderBy('created_at', 'DESC')->paginate(10);
        $department = Department::all();

        //If request is from ajax, then processing to filter data
        if($request->ajax()) {
            $str    =   $request->str;
            $dep    =   $request->dep;//dd($dep);

            $s  =   new Staff;
            $d  =   new Department;

            if($str != null && $dep != 0){ //If input and dropdown values are available
                $data = $s::with('departments')
                        ->where('department', $dep)
                        ->where(function($q) use ($str) {
                            $q->where('fname', 'like', '%'.$str.'%')
                            ->orWhere('lname', 'like', '%'.$str.'%')
                            ->orWhere('profile', 'like', '%'.$str.'%');
                        })
                        ->orderBy('created_at', 'DESC')
                        ->paginate(10);


            } else if($str != null && $dep == 0){ //If input value is set and dropdown value set to all departments
                $data = $s::with('departments')
                            ->where(function($q) use ($str) {
                                $q->where('fname', 'like', '%'.$str.'%')
                                ->orWhere('lname', 'like', '%'.$str.'%')
                                ->orWhere('profile', 'like', '%'.$str.'%');
                            })
                            ->orderBy('created_at', 'DESC')
                            ->paginate(10);

                $data_count = count($data);

                return view('search', compact('data', 'data_count'));

            } else if($str == null && $dep != 0){ //If dropdown value is not null and input is null
                $data = $s::with('departments')
                            ->where('department', $dep)
                            ->orderBy('created_at', 'DESC')
                            ->paginate(10);

            } else if($str == null && $dep == 0){ //If dropdown value is null and input is null
                $data = $s::with('departments')->orderBy('created_at', 'DESC')->paginate(10);
            }

            // returning data to view
            return view('search', ['data' => $data])->render();
        }

        //returning data if request is not from ajax
        return view('main', compact('data', 'department'));
    }

Hope this will work for you. Thank you.

Ad
source: stackoverflow.com
Ad