PHP code example of nahid-ferdous / laravel-searchable
1. Go to this page and download the library: Download nahid-ferdous/laravel-searchable library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
nahid-ferdous / laravel-searchable example snippets
namespace App\Models;
use Illuminate\Foundation\Auth\User as Authenticatable;
use NahidFerdous\Searchable\Searchable;
class User extends Authenticatable
{
use Searchable;
// Define your relationships as usual
public function country()
{
return $this->belongsTo(Country::class);
}
public function posts()
{
return $this->hasMany(Post::class);
}
}
public function index(Request $request)
{
$users = User::query()
// Text search across multiple fields
->search($request->search_query, [
'%first_name',
'%last_name',
'%first_name+last_name',
'%email',
'country|%name',
])
// Filter by status
->search($request->status, ['status'])
// Filter by role
->search($request->role, ['role'])
// Date range filter
->searchDate($request->date_range, ['created_at'], '><')
// Pagination
->paginate(20);
return view('users.index', compact('users'));
}