πŸš€ DevOps & SRE Certification Program πŸ“… Starting: 1st of Every Month 🀝 +91 8409492687 πŸ” Contact@DevOpsSchool.com

Upgrade & Secure Your Future with DevOps, SRE, DevSecOps, MLOps!

We spend hours on Instagram and YouTube and waste money on coffee and fast food, but won’t spend 30 minutes a day learning skills to boost our careers.
Master in DevOps, SRE, DevSecOps & MLOps!

Learn from Guru Rajesh Kumar and double your salary in just one year.


Get Started Now!

How to use Search Functionality in Laravel?

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 databaseWrite 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!");
}
});
view raw web.php hosted with ❀ by GitHub

Run the Project

Result

Thanks

Certification Courses

DevOpsSchool has introduced a series of professional certification courses designed to enhance your skills and expertise in cutting-edge technologies and methodologies. Whether you are aiming to excel in development, security, or operations, these certifications provide a comprehensive learning experience. Explore the following programs:

DevOps Certification, SRE Certification, and DevSecOps Certification by DevOpsSchool

Explore our DevOps Certification, SRE Certification, and DevSecOps Certification programs at DevOpsSchool. Gain the expertise needed to excel in your career with hands-on training and globally recognized certifications.