PHPackages                             niiknow/laratt - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Database &amp; ORM](/categories/database)
4. /
5. niiknow/laratt

ActiveLibrary[Database &amp; ORM](/categories/database)

niiknow/laratt
==============

A Laravel package to support table multitenancy

2.2.0(2y ago)0304↓50%MITPHPPHP &gt;=7.4

Since Nov 3Pushed 2y ago1 watchersCompare

[ Source](https://github.com/niiknow/laratt)[ Packagist](https://packagist.org/packages/niiknow/laratt)[ Docs](https://github.com/niiknow/laratt)[ RSS](/packages/niiknow-laratt/feed)WikiDiscussions master Synced 1mo ago

READMEChangelog (10)Dependencies (10)Versions (46)Used By (0)

Laravel Table Tenancy (laratt)
==============================

[](#laravel-table-tenancy-laratt)

> Allow for multi-tenancy by using table prefix

[![Build Status](https://camo.githubusercontent.com/c2836d79ae5344025f985231334f51db77edc16213ab14ce99a93041d4905274/68747470733a2f2f7472617669732d63692e6f72672f6e69696b6e6f772f6c61726174742e7376673f6272616e63683d6d6173746572)](https://travis-ci.org/niiknow/laratt)[![Software License](https://camo.githubusercontent.com/55c0218c8f8009f06ad4ddae837ddd05301481fcf0dff8e0ed9dadda8780713e/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f6c6963656e73652d4d49542d627269676874677265656e2e7376673f7374796c653d666c61742d737175617265)](LICENSE.md)

Also see [laratt-api](https://github.com/niiknow/laratt-api) where this project was originally built and refactored.

**Install**:

```
composer require niiknow/laratt

```

**Config**:

```
php artisan vendor:publish --provider="Niiknow\Laratt\LarattServiceProvider"

```

Features
--------

[](#features)

- Use special character `$` for tenant and table separator. Most database allow for this character.
- Dynamic table as `tenant$table_name`
- Tenant resolution use `x-tenant` header/input by default; though, it is customizable by providing a static function for `resolver` config.
- A generic Controller Trait that provide simple and flexible CRUD (create, retrieve, update, delete) REST endpoint.
- Simple query and bulk delete `/query` REST endpoint.
- jQuery DataTables as `/data` endpoint with [laravel-datatables](https://github.com/yajra/laravel-datatables)
- Pre-defined structured schema for `ProfileModel`
- Schedulable and ecommerce schema type for `TableModel`
- Being able to include and exclude table from auditable - so you don't have to audit things like when you're using it for logging, caching, or when client doesn't need it for some particular reason.

**CONS** It doesn't support table relationship.

API Schema
----------

[](#api-schema)

The image below is from our Swagger documentation of the [laratt-api](https://github.com/niiknow/laratt-api) project. [![](https://raw.githubusercontent.com/niiknow/laratt/master/api.png?raw=true)](https://raw.githubusercontent.com/niiknow/laratt/master/api.png?raw=true)

[Table Schema](https://github.com/niiknow/laratt/blob/master/src/Models/TableModel.php#L77)

Special multi-tables endpoint @ `/api/v1/tables/{table}`; where `{table}` is the table name you want to create. `{table}` must be all lower cased alphanumeric and underscore with mininum of 3 characters to 30 max. Example, let say `x-tenant: clienta` and `{table} = product`, then the resulting table will be `clienta$product`.

Also note that there are two ids: `id` and `uid`. `id` is internal to **laratt**. You should be using `uid` for all operations. `uid` is an auto-generated guid, if none is provide during `insert`.

Providing a `uid` allow the API `update` to effectively act as an `merge/upsert` operation. This mean that, if you call update with a `uid`, it will `update` if the record is found, otherwise `insert` a new record.

- `/query` endpoint is use for query and bulk `DELETE`, see: [Query Syntax](#query-syntax)
- `/data` endpoint is use for returning jQuery DataTables format using [laravel-datatables](https://github.com/yajra/laravel-datatables).
- `/import` bulk import is csv to allow for bigger import. Up to 10000 records instead of some small number like 100 for Azure Table Storage (also see admin config to adjust). This allow for efficiency of smaller file and quicker file transfer/upload.
- `/truncate` truncate all data from table.
- `/drop` drop a table. Why not? Now you can do all kind of crazy stuff with table.

What about your own/custom schema? See example of our [Profile Schema](https://github.com/niiknow/laratt/blob/master/src/Models/ProfileModel.php#L78)

Query-Syntax
------------

[](#query-syntax)

This library provide simple query endpoint for search and bulk delete: `api/v1/profile/query` or `api/v1/tables/{table}/query`

### Limiting

[](#limiting)

To limit the number of returned resources to a specific amount with keyword `limit` or `per_page`:

```
/query?limit=10
/query?limit=20

```

### Sorting

[](#sorting)

To sort the resources by a column in ascending or descending order:

```
/query?sort[]=column|asc
/query?sort[]=column|desc

```

You could also have multiple sort queries:

```
/query?sort[]=column1|asc&sort[]=column2|desc

```

### Filtering

[](#filtering)

The basic format to filter the resources:

```
/query?filter[]=column:operator:value

```

**Note:** The `value`s are `rawurldecode()`d.

#### Filtering Options

[](#filtering-options)

OperatorDescriptionExampleeqEqual to`/query?filter[]=column1:eq:123`neqNot equal to`/query?filter[]=column1:neq:123`gtGreater than`/query?filter[]=column1:gt:123`gteGreater than or equal to`/query?filter[]=column1:gte:123`ltLess than`/query?filter[]=column1:lt:123`lteLess than or equal to`/query?filter[]=column1:lte:123`ctContains text`/query?filter[]=column1:ct:some%20text`nctDoes not contains text`/query?filter[]=column1:nct:some%20text`swStarts with text`/query?filter[]=column1:sw:some%20text`nswDoes not start with text`/query?filter[]=column1:nsw:some%20text`ewEnds with text`/query?filter[]=column1:ew:some%20text`newDoes not end with text`/query?filter[]=column1:new:some%20text`btBetween two values`/query?filter[]=column1:bt:123|321`nbtNot between two values`/query?filter[]=column1:nbt:123|321`inIn array`/query?filter[]=column1:in:123|321|231`ninNot in array`/query?filter[]=column1:nin:123|321|231`nlIs null`/query?filter[]=column1:nl`nnlIs not null`/query?filter[]=column1:nnl`You can also do `OR` and `AND` clauses. For `OR` clauses, use commas inside the same `filter[]` query:

```
/query?filter[]=column1:operator:value1,column2:operator:value2

```

For `AND` clauses, use another `filter[]` query.

```
/query?filter[]=column1:operator:value1&filter[]=column2:operator:value2

```

RequestQueryBuilder server-side usage
=====================================

[](#requestquerybuilder-server-side-usage)

Below demo ficticious server-side DonationController that provide Laravel Paginate json data for some client-side ui.

```
