Quickstart
This guide walks you through creating a minimal OData service that exposes an Eloquent model in under 5 minutes.
1. Create a model and migration
If you already have a model, skip to step 2.
bash
php artisan make:model Product -mEdit the migration:
php
// database/migrations/xxxx_create_products_table.php
public function up(): void
{
Schema::create('products', function (Blueprint $table) {
$table->id();
$table->string('name');
$table->decimal('price', 10, 2);
$table->boolean('active')->default(true);
$table->timestamps();
});
}Run the migration:
bash
php artisan migrate2. Create an OData service
Create app/OData/ProductService.php:
php
<?php
namespace App\OData;
use App\Models\Product;
use LaravelUi5\OData\ODataService;
use LaravelUi5\OData\Service\Contracts\EdmBuilderInterface;
class ProductService extends ODataService
{
public function serviceUri(): string
{
return '';
}
public function namespace(): string
{
return 'App.Products';
}
protected function configure(EdmBuilderInterface $builder): EdmBuilderInterface
{
$this->discoverModel(Product::class);
return $builder->namespace($this->namespace());
}
}That's it. discoverModel() inspects the products table and automatically maps columns to OData properties, detects the primary key, and creates an entity set named Products.
3. Register the service
Create a service registry at app/OData/ServiceRegistry.php:
php
<?php
namespace App\OData;
use LaravelUi5\OData\Service\Contracts\ODataServiceInterface;
use LaravelUi5\OData\Service\Contracts\ODataServiceRegistryInterface;
class ServiceRegistry implements ODataServiceRegistryInterface
{
public function resolve(string $fullPath): ODataServiceInterface
{
return new ProductService();
}
public function services(): array
{
return [new ProductService()];
}
}Update config/odata.php:
php
'service_registry' => App\OData\ServiceRegistry::class,4. Try it out
Start your server and seed some data:
bash
php artisan tinker
>>> App\Models\Product::create(['name' => 'Widget', 'price' => 9.99]);
>>> App\Models\Product::create(['name' => 'Gadget', 'price' => 24.50, 'active' => false]);
>>> exitNow query your service:
GET /odata → service document
GET /odata/$metadata → CSDL XML schema
GET /odata/Products → all products
GET /odata/Products(1) → product with id=1
GET /odata/Products?$select=name → only the name column
GET /odata/Products?$filter=price gt 10
GET /odata/Products?$orderby=name asc
GET /odata/Products?$top=5&$skip=0
GET /odata/Products?$count=true → includes total count
GET /odata/Products?$search=widget → free-text searchNext steps
- Defining a Service -- full service API reference
- Model Discovery -- conventions, overrides, relationships
- Manual Schema -- building types by hand for full control
- Query Options -- supported
$filteroperators