Problem with search and pagination in Laravel?

2

The code below returns all the records of the database and also searches for the term typed in input of search with pagination.

The problem is that by clicking for example on page 2 it already refreshes the page returning all the records. How can I only remain with the search result and display all results only when I submit in the input search in white ?

public function index(Request $request)
{
    $search = $request->search;
    $query = School::orderBy('social_name', 'ASC');
    if(!empty($search)) {
        $query = School::where('social_name', 'LIKE', '%'.$search.'%');
    }
    $schools = $query->paginate(5);
    return view('/dashboard/school/index', compact('schools'))
         ->with(['active' => 'escola']);
}

View:

@section('content') 
<div class="row">
<div class="col-lg-12">
    <div class="card-box">

        <div class="row">
            <div class="col-md-9">
                <form method="GET" action="{{ url('/dashboard/school/create') }}">
                    <button type="submit" class="btn btn-primary w-md waves-effect m-t-5">Incluir</button>
                </form>
            </div>
            <div id="input-busca" class="col-md-3">
                <form method="GET" action="{{ url('/dashboard/school/index') }}">
                    <div class="input-group m-t-4">          
                        <input type="text" id="example-input2-group2" name="search" class="form-control" placeholder="Buscar">
                        <span class="input-group-btn">
                            <button type="submit" class="btn waves-effect waves-light btn-primary"><i class="fa fa-search"></i></button>
                        </span>  
                    </div>
                </form>
            </div>
        </div>

    </div>
    <div class="card-box">

        <div class="table-responsive">
            <table class="table m-0 table-hover">
                <thead>
                    <tr>
                        <th>Nome da Escola</th>
                        <th>Detalhes</th>
                        <th>Alterar</th>
                        <th>Excluir</th>
                    </tr>
                </thead>
                <tbody>
                    @foreach($schools as $school)
                    <tr>
                        <td>{{ $school->social_name }}</td>
                        <td>
                            <a href="#"><i class="fa fa-file-text-o fa-1_5x"></i></a>
                        </td>
                        <td>
                            <a href="#"><i class="fa fa-pencil-square-o fa-1_5x"></i></a>
                        </td>
                        <td>
                            <a href="#"><i class="fa  fa-trash-o fa-1_5x"></i></a>
                        </td>
                    </tr>
                    @endforeach
                </tbody>
            </table>

            {{ $schools->links() }}

        </div>
    </div>
</div><!-- end col -->

</div>
<!-- end row -->
@endsection
    
asked by anonymous 25.05.2017 / 22:50

1 answer

2

In your index method, you need to pass view to $search , then make the following changes:

public function index(Request $request)
{
    $search = $request->search;
    $query = School::orderBy('social_name', 'ASC');
    if(!empty($search)) {
        $query = School::where('social_name', 'LIKE', '%'.$search.'%');
    }
    $schools = $query->paginate(5);
    return view('/dashboard/school/index')
             ->with('schools', $schools)
             ->with('search', $search)
             ->with('active','escola');
}

and in the View in the% wrapper% change so to include the search in your {{ $schools->links() }} with the url :

{{ $schools->appends(['search' => isset($search) ? $search : ''])->links() }}

With these modifications any search that results in pagination will have the appends the last search done.

29.05.2017 / 01:32