How to use Search Functionality in Laravel?
Step 1. Create a new Project in Laravel, so open git bash. Write down the following command:-
$ composer create-project --prefer-dist laravel/laravel pagination_search "5.8.*"

Step 2. Now, Move to project directory on git Bash, so write down the following command:-
$ cd pagination_search

Mysql Database connection Laravel
Step 3. So, Create a Database for this and Go to XAMPP server->phpMyAdmin->Click New Database->searchdata.

Step 4. Now, Go to .env file to set the project path and give the project APP_URL, DB_DATABASE name and DB_USERNAME name.


Step 5. So, Create the user authentication scaffolding and write down this command:
php artisan make:auth

Step 6. Now, Create a Customer model, so write down the following command:-
php artisan make:model -m Customer

Step 7. Go to database/migration/customers_table file and Add name and email. Write down the following code:-
public function up() | |
{ | |
Schema::create('customers', function (Blueprint $table) { | |
$table->bigIncrements('id'); | |
$table->string('name'); | |
$table->string('email')->unique(); | |
$table->timestamps(); | |
}); | |
} |

Step 8. Now, Migrate the Customer table into the MySQL database. Write the following command within git Base.
$ php artisan migrate

Step 9. Insert data into Customer table.

Step 10. Go to resoucres/views/welcome.blade.php file and write down this follwing code.
<body> | |
<br/> | |
<div class="container"> | |
<form action="{{url('/search')}}" method="POST" role="search"> | |
{{csrf_field()}} | |
<div class="input-group"> | |
<input type="text" class="form-control" name="q" placeholder="Search for"><span class="input-group-btn"> | |
<button type="submit" class="btn btn-info"> | |
<i class="fas fa-search fa-sm"></i> Search | |
</button> | |
</span> | |
</div> | |
</form> | |
</div> <br/><br/> <br/><br/> <br/><br/> | |
<div class="container"> | |
@if(isset($data)) | |
<table class="table table-striped "> | |
<thead class="thead-dark"> | |
<tr> | |
<th>Name</th> | |
<th>Email</th> | |
</tr> | |
</thead> | |
<tbody> | |
@foreach($data as $user) | |
<tr> | |
<td>{{$user->name}}</td> | |
<td>{{$user->email}}</td> | |
</tr> | |
@endforeach | |
</tbody> | |
</table> | |
{!! $data->render() !!} | |
@else{{$message}} | |
@endif | |
</div> | |
</body> |
Step 11. Go to app/Coustomer.php model file and write down this follwing code.
class Customer extends Model
{
protected $table ="customers";
}

Step 12. Go to routes/web.php file and define the routes.
<?php | |
use Illuminate\Support\Facades\Input; | |
use App\Customer; | |
Route::get('/', function () { | |
$data = Customer::paginate(9); | |
return view('welcome')->withData($data); | |
}); | |
Route::any('/search',function(){ | |
$q = (Input::get('q')); | |
if($q != ''){ | |
$data =Customer::where('name','like','%'.$q.'%')->orWhere('email','like','%'.$q.'%')->paginate(5)->setpath(''); | |
$data->appends(array( | |
'q' => Input::get('q'), | |
)); | |
if(count($data)>0){ | |
return view('welcome')->withData($data); | |
} | |
return view('welcome')->withMessage("No Results Found!"); | |
} | |
}); |

Run the Project

Result

Thanks



With MotoShare.in, you can book a bike instantly, enjoy doorstep delivery, and ride without worries. Perfect for travelers, professionals, and adventure enthusiasts looking for a seamless mobility solution.