Simple and Advanced Search in Laravel​ 8

Simple and Advanced Search in Laravel​ 8

Laravel Search Feature (Simple & Advanced)

Features

  • Simple Search: Search people by name.

  • Advanced Search: Filter by:

    • Name

    • Address

    • Age Range

1. Install Laravel

composer create-project --prefer-dist laravel/laravel larasearch cd larasearch php artisan serve

Visit: http://localhost:8000

2. Configure .env

DB_CONNECTION=mysql DB_HOST=127.0.0.1 DB_PORT=3306 DB_DATABASE=searchdb DB_USERNAME=root DB_PASSWORD=

3. Model & Migration

php artisan make:model People -m

Migration File:

public function up() { Schema::create('people', function (Blueprint $table) { $table->increments('id'); $table->string('name'); $table->string('address'); $table->integer('age'); $table->timestamps(); }); }

In App\Providers\AppServiceProvider.php:

use Illuminate\Support\Facades\Schema; public function boot() { Schema::defaultStringLength(191); }

Then run:

php artisan migrate

In App\Models\People.php:

protected $fillable = ['name', 'address', 'age'];

4. Add Dummy Data

php artisan make:factory PeopleFactory

In database/factories/PeopleFactory.php:

use Faker\Generator as Faker; $factory->define(App\People::class, function (Faker $faker) { return [ 'name' => $faker->name, 'address' => $faker->address, 'age' => mt_rand(18, 150), ]; });

Seed with:

php artisan tinker >>> factory(App\People::class, 50)->create();

5. Create Controller

php artisan make:controller SearchController

SearchController.php:

namespace App\Http\Controllers; use Illuminate\Http\Request; class SearchController extends Controller { public function index() { $data = \DB::table('people')->paginate(10); return view('search', compact('data')); } public function simple(Request $request) { $data = \DB::table('people'); if ($request->search) { $data->where('name', 'LIKE', "%{$request->search}%"); } $data = $data->paginate(10); return view('search', compact('data')); } public function advance(Request $request) { $data = \DB::table('people'); if ($request->name) { $data->where('name', 'LIKE', "%{$request->name}%"); } if ($request->address) { $data->where('address', 'LIKE', "%{$request->address}%"); } if ($request->min_age && $request->max_age) { $data->whereBetween('age', [$request->min_age, $request->max_age]); } $data = $data->paginate(10); return view('search', compact('data')); } }

6. Define Routes

In routes/web.php:

use App\Http\Controllers\SearchController; Route::get('/people', [SearchController::class, 'index']); Route::get('/people/simple', [SearchController::class, 'simple'])->name('simple_search'); Route::get('/people/advance', [SearchController::class, 'advance'])->name('advance_search');

7. Create Blade View

resources/views/search.blade.php:

<!DOCTYPE html> <html> <head> <title>Laravel Search</title> <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css"> </head> <body> <div class="container mt-4"> <div class="row"> <div class="col-md-4"> <h4>Simple Search</h4> <form action="{{ route('simple_search') }}" method="GET"> <div class="input-group mb-3"> <input type="text" name="search" class="form-control" placeholder="Type name"> <div class="input-group-append"> <button class="btn btn-secondary">Search</button> </div> </div> </form> <h4>Advanced Search</h4> <form action="{{ route('advance_search') }}" method="GET"> <input type="text" name="name" class="form-control mb-2" placeholder="Name"> <input type="text" name="address" class="form-control mb-2" placeholder="Address"> <label>Age Range</label> <div class="input-group mb-2"> <input type="text" name="min_age" class="form-control" placeholder="Min Age"> <input type="text" name="max_age" class="form-control" placeholder="Max Age"> </div> <button class="btn btn-secondary">Search</button> </form> </div> <div class="col-md-8"> <h4>People List</h4> <table class="table table-bordered"> <tr> <th>ID</th><th>Name</th><th>Address</th><th>Age</th> </tr> @foreach($data as $person) <tr> <td>{{ $person->id }}</td> <td>{{ $person->name }}</td> <td>{{ $person->address }}</td> <td>{{ $person->age }}</td> </tr> @endforeach </table> {{ $data->appends(request()->except('page'))->links() }} </div> </div> </div> </body> </html>

Done!

You’ve now got:

  • A paginated list of people

  • A simple search by name

  • An advanced search with multiple filters

Soeng Souy

Soeng Souy

Website that learns and reads, PHP, Framework Laravel, How to and download Admin template sample source code free.

4 Comments

CAN FEEDBACK
  1. merltovkh
    merltovkh
    Test
  2. merltovkh
    merltovkh
    ok
  3. Omi Cool Services
    Omi Cool Services
    dosent work
  4. Anonymous
    Anonymous
    i got error Call to undefined method stdClass::appends(), can you help me
close