Problem with search and paging in Laravel?

Asked

Viewed 940 times

2

The code below returns all bank records and also searches for the term entered in input search with pagination.

The problem is that by clicking for example on page 2 it already refreshes the page by returning all the records. How to stay with the search result only and display all results only when you have 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

1 answer

2


In his method index, needs to move to the view the variable $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 stretch {{ $schools->links() }} so change to include the search in your url with the method appends:

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

With these modifications any search that results in paging will have us links the last survey conducted.

Reference:

Browser other questions tagged

You are not signed in. Login or sign up in order to post.