PHP code example of andreazorzi / search-table

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)"

protected static $table_fields = [
    "id" => [
        "sort" => "desc",
        "hidden" => true
    ],
    "full_name" => [
        "filter" => true,
        "custom-label" => "Full Name",
        "custom-value" => "getFullName",
        "custom-filter" => "CONCAT(first_name, ' ', last_name)"
    ],
    "email" => [
        "filter" => true,
    ],
    "created_at" => [
        "filter" => true,
        "advanced-type" => "date-range",
        "custom-label" => "Registration Date"
    ],
    "department" => [
        "filter" => true,
        "custom-value" => "getDepartmentName",
        "custom-filter" => "(SELECT d.name FROM departments d WHERE d.id = department_id)"
    ],
    "enabled" => [
        "filter" => true,
        "custom-label" => "Status"
        "custom-value" => "getStatus",
    ]
];

// Custom value methods
public function getFullName(){
    return $this->first_name . ' ' . $this->last_name;
}

public function getDepartmentName(){
    return $this->department->name;
}

public function getStatus(){
    return $this->enabled ? "Enabled" : "Disabled";
}

public function getTableActions($model_name, $key): array{
    return [
        [
            "attributes" => 'hx-get="'.route($model_name.".edit", $key).'" hx-target="#modal .modal-content"',
            "content" => '<i class="fa-solid fa-pen"></i> Edit'
        ],
        [
            "attributes" => 'hx-delete="'.route($model_name.".destroy", $key).'" hx-confirm="Are you sure?"',
            "content" => '<i class="fa-solid fa-trash text-danger"></i> Delete'
        ]
    ];
}

// Model
protected static $table_fields = [
    "name" => ["filter" => true, "sort" => "asc"],
    "email" => ["filter" => true],
    "created_at" => ["custom-label" => "Joined"]
];

// Blade
<x-search-table::table :model="new App\Models\User()"/>

// Model
protected static $table_fields = [
    "name" => ["filter" => true, "sort" => "asc"],
    "category" => [
        "filter" => true,
        "custom-value" => "getCategoryName",
        "custom-filter" => "(SELECT c.name FROM categories c WHERE c.id = category_id)"
    ],
    "price" => ["sort" => "desc"],
    "tags" => [
        "filter" => true,
        "custom-value" => "getTagsText",
        "advanced-type" => "in-array"
    ]
];

// Blade with advanced filters
<x-search-table::table 
    :model="new App\Models\Product()" 
    :showadvancefilters="true"
    :disableaddbutton="false"
/>