{"id":28677518,"url":"https://github.com/ymigval/laravel-model-datatable-ssp","last_synced_at":"2025-06-14T00:06:42.177Z","repository":{"id":193607930,"uuid":"689154342","full_name":"ymigval/laravel-model-datatable-ssp","owner":"ymigval","description":"An extension designed to seamlessly integrate Laravel models with server-side DataTables.","archived":false,"fork":false,"pushed_at":"2024-01-26T18:36:05.000Z","size":83,"stargazers_count":9,"open_issues_count":0,"forks_count":0,"subscribers_count":2,"default_branch":"main","last_synced_at":"2025-05-07T06:17:44.229Z","etag":null,"topics":["api","datatables","eloquent","laravel","model","query","ssp"],"latest_commit_sha":null,"homepage":"","language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/ymigval.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null}},"created_at":"2023-09-09T00:09:28.000Z","updated_at":"2024-08-31T09:54:52.000Z","dependencies_parsed_at":null,"dependency_job_id":"ab1e71d0-ec4f-4bfc-9d14-71f57bc90ce4","html_url":"https://github.com/ymigval/laravel-model-datatable-ssp","commit_stats":null,"previous_names":["ymigval/laravel-model-datatable-ssp"],"tags_count":6,"template":false,"template_full_name":null,"purl":"pkg:github/ymigval/laravel-model-datatable-ssp","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ymigval%2Flaravel-model-datatable-ssp","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ymigval%2Flaravel-model-datatable-ssp/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ymigval%2Flaravel-model-datatable-ssp/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ymigval%2Flaravel-model-datatable-ssp/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/ymigval","download_url":"https://codeload.github.com/ymigval/laravel-model-datatable-ssp/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ymigval%2Flaravel-model-datatable-ssp/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":259645964,"owners_count":22889709,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["api","datatables","eloquent","laravel","model","query","ssp"],"created_at":"2025-06-14T00:06:39.758Z","updated_at":"2025-06-14T00:06:42.146Z","avatar_url":"https://github.com/ymigval.png","language":"PHP","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Processing Laravel Models with DataTables\n\nExtension designed to seamlessly integrate Laravel models with [server-side DataTables](https://datatables.net/examples/server_side/simple.html). It provides a convenient and efficient way to fetch, transform, and display data from your Laravel models in DataTables.\n\n## Installation\n\nTo get started, install the package via Composer:\n```bash\ncomposer require ymigval/laravel-model-datatable-ssp\n```\n\n## Usage with Eloquent Models\n\nTo use DataTables with an Eloquent model, you can create an instance or query your model and call the `datatable()` method with column mappings.\n\n```php\nuse App\\Models\\Customer;\n\nreturn (new Customer())-\u003edatatable([\n    'first_name', 'last_name', 'phone'\n]);\n```\n\nAlternatively, you can call the static `datatable()` method:\n\n```php\nuse App\\Models\\Customer;\n\nreturn Customer::datatable([\n    'first_name', 'last_name', 'phone'\n]);\n```\n\n- Replace `Customer` with your Eloquent model.\n- `['first_name' =\u003e 'first_name', 'last_name' =\u003e 'last_name', 'phone' =\u003e 'phone']`: Define the mappings of your model's fields.\n\n### Customizing Columns\n\nYou can customize field values by providing closures in your column mappings.\n\n```php\nuse App\\Models.Customer;\n\nreturn Customer::where('type', 'male')-\u003edatatable([\n    'first_name',\n    'last_name',\n    'active' =\u003e function ($field, $row) {\n        return ($field) ? 'Yes' : 'No';\n    }\n]);\n```\n\n- $field: Contains the value of the field in that mapping.\n- $row: Contains the values of the fields in the context of the current row.\n\n### Adding Additional Columns\n\nYou can add additional columns by using closures:\n\n```php\nuse App\\Models\\Customer;\n\nreturn Customer::datatable([\n    'first_name',\n    'last_name',\n    function () {\n        return 'additional column #1';\n    },\n    function () {\n        return 'additional column #2';\n    }\n]);\n```\n\n### Fields in Context\n\nDefine model fields in context to access related data or perform custom formatting.\n\n```php\nuse App\\Models\\Customer;\n\nreturn Customer::datatable([\n    'first_name' =\u003e function ($field, $row) {\n        return $field . ' ' . $row-\u003elast_name;\n    }\n], ['last_name']);\n```\n\nBy default, fields added to the context cannot be searched and sorted. You can configure this behavior by adding options to the field:\n\n```php\nuse App\\Models\\Customer;\n\nreturn Customer::datatable([\n    'first_name' =\u003e function ($field, $row) {\n        return $field . ' ' . $row-\u003elast_name;\n    }\n], ['last_name' =\u003e ['orderable' =\u003e true, 'searchable' =\u003e true]]);\n```\n\n## Usage with Query Builder\n\nYou can use DataTable with Query Builder by calling `dataTable()` on a query builder instance.\n\n```php\nuse Illuminate\\Support\\Facades\\DB;\n\nreturn DB::table('customers')\n    -\u003edatatable([\n        'first_name',\n        'last_name',\n        'phone'\n    ]);\n```\n\n## Transforming Output Data\n\nYou can transform the datatable return into various formats such as `response`, `array`, or `json` by specifying it as the third parameter.\n\nBy default, a response is returned.\n\n```php\nuse App\\Models\\Customer;\n\n(new Customer())-\u003edatatable(\n    ['first_name', 'last_name', 'phone'],\n    [],\n    'array'\n);\n```\n\n```php\nuse Illuminate\\Support\\Facades\\DB;\n\nDB::table('customers')-\u003edatatable(\n    ['first_name', 'last_name', 'phone'],\n    [],\n    'json'\n);\n```\n\n## Advanced Usage\n\n### Using Callbacks for Column Mappings\n\nYou can use a callback to define columns dynamically.\n\n```php\nuse App\\Models\\Customer;\n\nreturn (new Customer())-\u003edatatable(\n    function () {\n        return ['first_name', 'last_name', 'phone'];\n    }\n);\n```\n\n### Union Queries\n\nPerform union queries with DataTable.\n\n```php\nuse App\\Models\\Customer;\n\nreturn Customer::join('business', 'business.id_customer', '=', 'customers.id')\n    -\u003edatatable(\n        function () {\n            return ['customers.first_name', 'customers.last_name', 'business.name'];\n        }\n    );\n```\n\nYou can also add aliases to the fields in the column mapping or fields in context:\n\n```php\nuse App\\Models\\Customer;\n\nreturn Customer::join('business', 'business.id_customer', '=', 'customers.id')\n    -\u003edatatable(\n        [\n            'customers.first_name AS f_name',\n            'customers.last_name AS l_name',\n            'business.name AS aaa',\n        ],\n        ['customers.phone AS contact' =\u003e ['orderable' =\u003e false, 'searchable' =\u003e true]]\n    );\n```\n\n## Using Eloquent Relationships\n\n#### Note on Using Relations\n\nWhen using relations, there are some limitations:\n\n- Avoid using related fields as column values without a closure.\n- To utilize the value of a related field, it should be accessed through a formatting closure. Use the second parameter of the closure to access the value. Remember that the second parameter contains the values of the fields in the context of the current row.\n- Related fields cannot be sorted or searched.\n\nPlease make sure to add the local key used in the relation to your column mappings or fields in context.\n\n```php\nuse App\\Models\\Customer;\n\nreturn Customer::with('business')\n    -\u003edatatable(\n        [\n            'first_name',\n            'last_name',\n            function ($field, $row) {\n                return $row-\u003ebusiness-\u003ename;\n            },\n        ],\n        ['id'] // 'id' is the localKey field specified in the relation with 'business'\n    );\n```\n\nFor more usage examples, refer to the test cases.\n\n## Installing DataTables in Your Application\n\nIn the official DataTables documentation: https://datatables.net/, you will find the steps to install the library in your application.\n\nCheck out the server-side processing examples: https://datatables.net/examples/server_side/simple.html\n\n## Changelog\nPlease refer to the [CHANGELOG](CHANGELOG.md) for more information about recent changes.\n\n## License\nThe MIT License (MIT). For more information, please see the [License File](LICENSE).\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fymigval%2Flaravel-model-datatable-ssp","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fymigval%2Flaravel-model-datatable-ssp","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fymigval%2Flaravel-model-datatable-ssp/lists"}