rajesha's avatar

Laravel pagination with search function

Hi, any one please help me out, how we can pagination with search function.

0 likes
6 replies
zainudinnoori's avatar

In your view where you wanna add search feild do like this:

<form action="/search" method="get">
    {{ csrf_field() }}
    <input  name="search_text" type="text"/>
    <input type="submit"/>
</form>

Make a route:

Route::get('/search','UserController@search');

Make your contoller as UserController

    public function search()
    {
        $query=request('search_text');
        $users = User::where('name', 'LIKE', '%' . $query . '%')->paginate(10);;
        return view('searchresult',compact('users'));
    }

Finally make anther view to display the result. as searchresult.blade.php

@foreach($users as $user)
    {{$user->name}}
@endforeach
{{ $users->links() }}

Hope you can use it. it cant be as simple as this

2 likes
rajesha's avatar

Hi I'm using jquery Datatables for oagination and search function. I'm declaring Datatables class but still it is showing class does not found.

Code:

use Datatables;

public function get_datatable() { //echo 'yes'; return Datatables::eloquent(Master::query())->make(true); }

Output: Class not found Fatal error

zainudinnoori's avatar

Did you include its CDN links? I think you did not: Css : <link rel="stylesheet" href="//cdn.datatables.net/1.10.16/css/jquery.dataTables.min.css"> Js: <script src="//cdn.datatables.net/1.10.16/js/jquery.dataTables.min.js"></script>

rajesha's avatar

Yes I've in included. again i got some error which are listed below.

(1/1) ErrorException

Invalid argument supplied for foreach() in Datatables.php (line 57) at HandleExceptions->handleError(2, 'Invalid argument supplied for foreach()', 'C:\xampp\htdocs\hecbom\vendor\yajra\laravel-datatables-oracle\src\Datatables.php', 57, array('source' => object(Builder), 'datatables' => object(Datatables), 'config' => object(Repository), 'engines' => null, 'builders' => null))in Datatables.php (line 57)

Controller.php

return Datatables::of(Master::query())->make(true);

zainudinnoori's avatar

It looks you are sending an invalid variable to foreach, post your controller and view code.

charlygirola's avatar

On the second page of the navigation, I get all the results, not the filtered ones. Any idea on how to solve this?

Please or to participate in this conversation.