PHP code example of laravelwakeup / filter-sort

1. Go to this page and download the library: Download laravelwakeup/filter-sort 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/ */

    

laravelwakeup / filter-sort example snippets


return [
    // Example: status_op=eq, status_sort=desc
    'prefix' => '_op',
    'sort_field_suffix' => '_sort'
];

use LaravelWakeUp\FilterSort\Traits\FilterTrait;
use LaravelWakeUp\FilterSort\Traits\SortTrait;

class Post extends Model
{
    use FilterTrait, SortTrait;
    
    // Optional: Restrict which fields can be filtered
    protected array $allowedFilters = ['title', 'created_at', 'status', 'deleted_at'];

    // Optional: Restrict which fields can be sorted
    protected array $allowedSorts = ['id', 'created_at'];
}

$posts = Post::query()
    ->filter(request())
    ->sort(request())
    ->get();

// Sort by created_at DESC, then by id ASC
/posts?created_at_sort=desc&id_sort=asc

// Sort by status DESC, created_at DESC, and id ASC
/posts?status_sort=desc&created_at_sort=desc&id_sort=asc

protected array $allowedSorts = ['id', 'created_at', 'title', 'status'];

// config/laravel-filter-sort.php
return [
    'sort_field_suffix' => '_sort'  // Change this to your preferred suffix
];

   protected array $multiColumnSearch = [
       'search_field' => 'search_txt', // The request parameter to use for the search term
       'fields' => [
           'username' => 'like',
           'server' => 'eq',
           // Add more fields as needed
       ],
   ];