1. Go to this page and download the library: Download hanieas/larasearch 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/ */
hanieas / larasearch example snippets
namespace LaraSearch;
interface Searchable
{
/**
* @return array
*/
public function getColumnsForExactMatch(): array;
/**
* @return array
*/
public function getColumnsForLikeMatch(): array;
/**
* @return array
*/
public function getColumnsForBooleanMatch(): array;
/**
* @return array
*/
public function getColumnsForPeriodMatch(): array;
}
use LaraSearch\Searchable;
class User extends Model implements Searchable
{
protected $fillable = [
'username',
'mobile',
'is_active',
'logged_in',
];
/**
* @return array
*/
public function getColumnsForExactMatch(): array {
return [
'mobile'
];
}
/**
* @return array
*/
public function getColumnsForLikeMatch(): array {
return [
'username'
];
}
/**
* @return array
*/
public function getColumnsForBooleanMatch(): array {
return [
'is_active'
];
}
/**
* @return array
*/
public function getColumnsForPeriodMatch(): array
{
return [
'logged_in'
];
}
}
class UserController extends Controller
{
/**
* @return JsonResponse
*/
public function index(): JsonResponse
{
$users = new Search(Request()->toArray(), User::query())->get();
return response()->json([
'users' => $users
]);
}
}