1. Go to this page and download the library: Download andreazorzi/search-table 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/ */
andreazorzi / search-table example snippets
namespace App\Http\Controllers;
use SearchTable\Traits\SearchController;
class UserController extends Controller
{
use SearchController;
// Your existing controller methods...
}
namespace App\Models;
use SearchTable\Traits\SearchModel;
use Illuminate\Foundation\Auth\User as Authenticatable;
class User extends Authenticatable
{
use SearchModel;
// Mandatory: Define which fields to display and their behavior
protected static $table_fields = [
"username" => [
"filter" => true,
"sort" => "asc"
],
"name" => [
"filter" => true,
],
"email" => [
"filter" => true,
]
];
// Optional: Define custom actions for each row
public function getTableActions($model_name, $key): array
{
return [
[
"attributes" => 'data-id="'.$key.'" hx-get="'.route($model_name.".show", [$key ?? 0]).'" hx-target="#modal .modal-content"',
"content" => '<i class="table-search-preview fa-solid fa-pen"></i>'
]
];
}
}
// Merged attributes
"custom-filter" => "CONCAT(first_name, ' ', last_name)"
// Manipulated data
"custom-filter" => "CASE WHEN status = 0 THEN 'New' WHEN status = 1 THEN 'In Progress' ELSE 'Finished' END"
// Model relationship
"custom-filter" => "(SELECT d.name FROM departments d WHERE d.id = department_id)"
// Model multiple relationship (Advanced Type: in-array)
"custom-filter" => "(SELECT GROUP_CONCAT(t.name SEPARATOR ', ') FROM tags t join user_tag ut on ut.tag_id = t.id WHERE ut.user_username = users.username)"