Search data from database in Laravel | Filter data in laravel
Today i will show you How to Search data from database in laravel with pagination
it's simple and very easy Let's Go start
Create a Controller
StudentController.php
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Models\Student;
class StudentController extends Controller
{
public function index()
{
$students=Student::all();
return view('student.view',compact('students'));
}
public function Search(Request $request)
{
if(empty($request))
{
$students=Student::paginate(2);
return view('student.view',compact('students'));
}else{
$students=Student::where('name','like','%'.$request->search.'%')->orWhere('content','like','%'.$request->search.'%')->paginate(2);
$students->appends($request->all());
return view('student.view',compact('students'));
}
}
}
Create view/ blade file
view.blade.php
<!DOCTYPE html>
<html>
<head>
<title>View</title>
</head>
<body>
<h2>View Student</h2>
<div>
<form action="/ViewStudent" method="GET">
{{csrf_field()}}
<input type="text" name="search" placeholder="Key word">
<input type="submit" value="Serach">
</form>
</div>
<br>
<table border="1">
<thead>
<tr>
<th>SL</th>
<th>Name</th>
<th>Content</th>
</tr>
</thead>
<tbody>
@php $i=$students->perPage()*($students->currentPage()-1); @endphp
@foreach($students as $key=>$data)
<tr>
<td>{{++$i}}</td>
<td>{{$data->name}}</td>
<td>{{$data->content}}</td>
</tr>
@endforeach
</tbody>
</table>
{{$students->links()}}
</body>
</html>
Create route
web.php
Route::get('/ViewStudent',[App\Http\Controllers\StudentController::class, 'Search']);
Thanks for visit
visit my other site: Basabaribd.com
No comments