PHPackages                             khalyomede/laravel-odata-adaptable - 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. khalyomede/laravel-odata-adaptable

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

khalyomede/laravel-odata-adaptable
==================================

Adapt your Eloquent model automatically according to the OData query strings.

v0.1.0(6y ago)31.1k1[1 PRs](https://github.com/khalyomede/laravel-odata-adaptable/pulls)MITPHP

Since Dec 8Pushed 5y ago1 watchersCompare

[ Source](https://github.com/khalyomede/laravel-odata-adaptable)[ Packagist](https://packagist.org/packages/khalyomede/laravel-odata-adaptable)[ RSS](/packages/khalyomede-laravel-odata-adaptable/feed)WikiDiscussions master Synced 1w ago

READMEChangelogDependencies (5)Versions (3)Used By (0)

laravel-odata-adaptable
=======================

[](#laravel-odata-adaptable)

Adapt your Eloquent model automatically according to the OData query strings.

[![Packagist Version](https://camo.githubusercontent.com/9f5d3986dfa2f8e9f5dd763a560562beeaab2af59dc441eb33ad613756a64f17/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f6b68616c796f6d6564652f6c61726176656c2d6f646174612d616461707461626c65)](https://packagist.org/packages/khalyomede/laravel-odata-adaptable) [![Packagist](https://camo.githubusercontent.com/ba49aae0e1f9fa820713af2afb00233a0e75f0b5d0364868c2612b626226c55a/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f6c2f6b68616c796f6d6564652f6c61726176656c2d6f646174612d616461707461626c65)](https://packagist.org/packages/khalyomede/laravel-odata-adaptable) [![Build Status](https://camo.githubusercontent.com/44ff2421052b7846f9b47ef040a561bb073522ea16b8d4f0d1f2ca2fac310a61/68747470733a2f2f7472617669732d63692e636f6d2f6b68616c796f6d6564652f6c61726176656c2d6f646174612d616461707461626c652e7376673f6272616e63683d6d6173746572)](https://travis-ci.com/khalyomede/laravel-odata-adaptable) [![Coverage Status](https://camo.githubusercontent.com/1491e872bf76fe9a52bc0ab85f84b0449bb88b82a23f0fc244a929f9ffb1a9fe/68747470733a2f2f636f766572616c6c732e696f2f7265706f732f6769746875622f6b68616c796f6d6564652f6c61726176656c2d6f646174612d616461707461626c652f62616467652e7376673f6272616e63683d6d6173746572)](https://coveralls.io/github/khalyomede/laravel-odata-adaptable?branch=master) [![Infection MSI](https://camo.githubusercontent.com/28ac65e5558deda4bba51cdd749a49af71856db588f2d3ea200a6811e0899efc/68747470733a2f2f62616467652e737472796b65722d6d757461746f722e696f2f6769746875622e636f6d2f6b68616c796f6d6564652f6c61726176656c2d6f646174612d616461707461626c652f6d6173746572)](https://infection.github.io)[![Maintainability](https://camo.githubusercontent.com/12441e3e836abbdd44e950455c28892e6dee673906ed0f534e8d974ece8b522b/68747470733a2f2f6170692e636f6465636c696d6174652e636f6d2f76312f6261646765732f30316334653237633466366237643532356565342f6d61696e7461696e6162696c697479)](https://codeclimate.com/github/khalyomede/laravel-odata-adaptable/maintainability)

Summary
-------

[](#summary)

- [About](#about)
- [Features](#features)
- [Requirements](#requirements)
- [Installation](#installation)
- [Usage](#usage)
- [Examples](#examples)
- [API](#api)
- [Known issues](#known-issues)

About
-----

[](#about)

I needed to customize how my client side application fetches results from my Laravel REST API. Mostly, I just need to filter which fields I want to get from my API. I checked for [POData-Laravel](https://github.com/Algo-Web/POData-Laravel), but I found it too much strict.

I wanted a drop-in solution, that does not change the way I work with my existing Eloquent models, so I decided to create this library.

Features
--------

[](#features)

- use a Trait on your model, which will make them automatically mutate the result according to the OData query strings
- Support for the following OData keywords: `$select`, `$orderBy`, `$top` and `$skip`
- Throw an error if you use `$select` using `$hidden` columns
- Tested with Laravel versions:
    - 5.8.\*

Requirements
------------

[](#requirements)

- PHP &gt;= 7.2.0
- [Composer](https://getcomposer.org)

Installation
------------

[](#installation)

Add the dependency:

```
composer require khalyomede/laravel-odata-adaptable
```

Usage
-----

[](#usage)

Add the `OdataAdaptable` trait to your existing model.

```
// app/Book.php

namespace App;

use Illuminate\Database\Eloquent\Model;
use Khalyomede\OdataAdaptable;

class Book extends Model {
  use OdataAdaptable;
}
```

You should have a route like:

```
// routes/api.php

Route::resource("book", "BookController");
```

In your controller, add the capability to adapt to the OData V4 query strings to your Eloquent model like this:

```
namespace App\Http\Controllers;

use App\Book;
use Illuminate\Http\Request;

class BookController extends Controller {
  public function index(Request $request) {
    return Book::adapt($request)->get();
  }
}
```

Now, if you run this HTTP query

```
GET /api/book

```

You will get

```
[
  {
    "id": 1,
    "authorId": 1,
    "name": "Build an SPA using Gulp & Vue.js"
  }
]

```

But if you use OData query strings, you will be able to do

```
GET /api/book?$select=name

```

And you will get

```
[
  {
    "name": "Build an SPA using Gulp & Vue.js"
  }
]

```

Examples
--------

[](#examples)

- [1. Calling it at the very first](#1-calling-it-at-the-very-first)
- [2. Chaining it after previous call to the Eloquent query builder](#2-chaining-it-after-previous-call-to-the-eloquent-query-builder)

### 1. Calling it at the very first

[](#1-calling-it-at-the-very-first)

In this example, we will call the `adapt` method to adapt our Eloquent ORM result according to the query strings received from the controller's request. The file below is extracted from an hypothetical controller.

```
class BookController extends Controller {
  public function index(Request $request) {
    $books = Book::adapt($request)->get();
  }
}
```

### 2. Chaining it after previous call to the Eloquent query builder

[](#2-chaining-it-after-previous-call-to-the-eloquent-query-builder)

In this example, we will call the `adapt` method right after some methods, to show you how you can still take advantage of this library even after existing changes to the result of your Eloquent query.

```
class VueJsBookController extends Controller {
  public function index(Request $request) {
    $books = Book::where("name", "like", "Vue.js")->adapt($request)->get();
  }
}
```

API
---

[](#api)

```
public function adapt(Illuminate\Http\Request $request): Illuminate\Database\Eloquent\Builder;
```

**parameters**

- `Request $request`: The request from your Controller. It will be used to extract the URL using `$request->fullUrl()`.

**return**

An instance of `Builder` that let you chain other query builder methods.

**exceptions**

See all the exceptions thrown by [OdataQueryParser](https://packagist.org/packages/khalyomede/odata-query-parser).

Known issues
------------

[](#known-issues)

The following OData V4 query strings commands will not work:

- count
- filter

###  Health Score

28

—

LowBetter than 54% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity22

Limited adoption so far

Community10

Small or concentrated contributor base

Maturity48

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 96.2% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Unknown

Total

1

Last Release

2353d ago

### Community

Maintainers

![](https://avatars.githubusercontent.com/u/15908747?v=4)[Anwar](/maintainers/khalyomede)[@khalyomede](https://github.com/khalyomede)

---

Top Contributors

[![khalyomede](https://avatars.githubusercontent.com/u/15908747?v=4)](https://github.com/khalyomede "khalyomede (25 commits)")[![dependabot[bot]](https://avatars.githubusercontent.com/in/29110?v=4)](https://github.com/dependabot[bot] "dependabot[bot] (1 commits)")

---

Tags

eloquentlaravelodataodata-query-parserodatav4query-string

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/khalyomede-laravel-odata-adaptable/health.svg)

```
[![Health](https://phpackages.com/badges/khalyomede-laravel-odata-adaptable/health.svg)](https://phpackages.com/packages/khalyomede-laravel-odata-adaptable)
```

###  Alternatives

[doctrine/orm

Object-Relational-Mapper for PHP

10.2k285.3M6.2k](/packages/doctrine-orm)[jdorn/sql-formatter

a PHP SQL highlighting library

3.9k115.1M102](/packages/jdorn-sql-formatter)[illuminate/database

The Illuminate Database package.

2.8k52.4M9.4k](/packages/illuminate-database)[ramsey/uuid-doctrine

Use ramsey/uuid as a Doctrine field type.

90440.3M211](/packages/ramsey-uuid-doctrine)[reliese/laravel

Reliese Components for Laravel Framework code generation.

1.7k3.4M16](/packages/reliese-laravel)[wildside/userstamps

Laravel Userstamps provides an Eloquent trait which automatically maintains `created\_by` and `updated\_by` columns on your model, populated by the currently authenticated user in your application.

7511.7M13](/packages/wildside-userstamps)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
