PHP code example of aw-studio / laravel-model-index

1. Go to this page and download the library: Download aw-studio/laravel-model-index 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/ */

    

aw-studio / laravel-model-index example snippets


return Product::index()
    ->filterable(['name', 'price', 'category_id'])
    ->sortable(['name', 'price', 'created_at'])
    ->searchable(['name', 'sku'])
    ->maxPerPage(100)
    ->get();

// config/model-index.php
return [
    'per_page' => 10,
    'max_per_page' => 100,   // null to disable the ceiling
    'sortable' => [],        // ['*'] to allow every column
    'searchable' => [],      // ['*'] to search every column
];

use AwStudio\ModelIndex\Traits\HasIndexQuery;

class Product extends Model
{
    use HasIndexQuery;
}

public function index()
{
    return Product::index()
        ->filterable(['name', 'price'])
        ->get();
}

Product::index()->useResource(ProductResource::class)->get();

Product::index()->filterable(['name', 'price'])->get();

// equivalent to: where name like %Shirt% or name like %Hoodie%

public function index()
{
    // GET /users?filter[popular]=true
    return User::index()
        ->filter('popular', fn ($query, $value) => $query->popular())
        ->get();
}

// GET /posts?filter[user.name]=John
return Post::index()
    ->filter('user.name', function ($query, $value) {
        $query->whereHas('user', fn ($q) => $q->where('name', $value));
    })
    ->get();

Post::index()->filterable(['user.name', 'user.email'])->get();

class Product extends Model
{
    use HasIndexQuery;

    public function filterable(): array
    {
        return ['name', 'price', 'category_id'];
    }

    // or:
    public $filterable = ['name', 'price'];
}

Product::index()->sortable(['name', 'price', 'created_at'])->get();

Product::index()
    ->sort('popularity', fn ($query, $direction) => $query->orderBy('sales_count', $direction))
    ->get();

User::index()->searchable(['name', 'email'])->get();

Post::index()->searchable(['title', 'user.name'])->get();

Product::index()
    ->search('sku_prefix', fn ($query, $term) => $query->orWhere('sku', 'like', "{$term}%"))
    ->get();

Product::index()->pageName('p')->get();   // GET /products?p=2

Product::index()->maxPerPage(250)->paginate();

Product::index()->sortable(['created_at'])->cursorPaginate();

Product::index()->with('category')->where('active', true)->get();

Model::index()
    ->filterable(['name', 'status'])
    ->sortable(['name', 'created_at'])
    ->searchable(['name', 'sku'])
    ->paginate();
sh
php artisan vendor:publish --tag=model-index-config
sh
GET /products?filter[price][$between]=10,99
GET /orders?filter[created_at][$between][0]=2026-01-01&filter[created_at][$between][1]=2026-12-31