PHP code example of novabytes / laravel-odata

1. Go to this page and download the library: Download novabytes/laravel-odata 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/ */

    

novabytes / laravel-odata example snippets


use NovaBytes\OData\Laravel\ODataQueryBuilder;

class ProductController extends Controller
{
    public function index(Request $request)
    {
        return ODataQueryBuilder::for(Product::class, $request)
            ->allowedFilters('name', 'price', 'is_active', 'category_id')
            ->allowedSorts('name', 'price', 'created_at')
            ->allowedExpands('category', 'reviews')
            ->allowedSelects('id', 'name', 'price', 'description', 'category_id')
            ->get();
    }
}

// config/odata.php

'entity_sets' => [
    \App\Models\Product::class => [
        'entitySet'       => 'Products',
        'operations'      => ['read', 'create', 'update', 'delete'],
        'allowedFilters'  => ['name', 'price'],
        'allowedSorts'    => ['name', 'price'],
        'allowedExpands'  => ['category'],
        'allowedSelects'  => ['id', 'name', 'price', 'description'],
        'allowedCreates'  => ['name', 'price', 'description', 'category_id'],
        'allowedUpdates'  => ['name', 'price', 'description'],
    ],
],

'crud' => [
    'enabled'            => true,
    'route_prefix'       => 'api',
    'middleware'          => ['api'],
    'default_operations' => ['read'],
],

// config/odata.php

'entity_sets' => [
    \App\Models\Product::class => [
        'entitySet'       => 'Products',           // optional, auto-generated from table name
        'operations'      => ['read', 'create', 'update', 'delete'],
        'allowedFilters'  => ['name', 'price', 'is_active'],
        'allowedSorts'    => ['name', 'price', 'created_at'],
        'allowedExpands'  => ['category', 'reviews'],
        'allowedSelects'  => ['id', 'name', 'price', 'description'],
        'allowedCreates'  => ['name', 'price', 'description', 'category_id'],
        'allowedUpdates'  => ['name', 'price', 'description'],
    ],
    \App\Models\Category::class => [
        'operations'      => ['read'],
        'allowedFilters'  => ['name'],
        'allowedExpands'  => ['products'],
    ],
],

'namespace' => 'Default',

'metadata' => [
    'enabled' => true,
    'route_prefix' => 'odata',
    'openapi' => [
        'title' => 'My OData API',
        'version' => '1.0.0',
        'description' => '',
    ],
],

// Allowlists are loaded from config automatically
ODataQueryBuilder::for(Product::class, $request)->get();

// Explicit calls still override config when you need to restrict further
ODataQueryBuilder::for(Product::class, $request)
    ->allowedFilters('name')  // overrides config for this endpoint
    ->get();

// config/odata.php

return [
    'response_format'  => 'laravel',  // 'laravel' or 'odata'
    'max_expand_depth' => 3,          // Max $expand nesting depth
    'max_top'          => 1000,       // Max $top value (null = unlimited)
    'default_top'      => null,       // Default $top when not specified (null = no limit)
    'throw_on_invalid' => true,       // true = 400 on invalid ops, false = silently ignore
    'entity_sets'      => [],         // Model registrations (see above)
    'namespace'        => 'Default',  // CSDL schema namespace
    'metadata'         => [           // Metadata endpoint config
        'enabled'      => false,
        'route_prefix' => 'odata',
        'openapi'      => ['title' => 'OData API', 'version' => '1.0.0', 'description' => ''],
    ],
    'crud'             => [           // CRUD endpoint config
        'enabled'            => false,
        'route_prefix'       => 'api',
        'middleware'          => ['api'],
        'default_operations' => ['read'],
    ],
];

$builder = ODataQueryBuilder::for(Product::class, $request)
    ->allowedFilters('price')
    ->toBuilder();

$results = $builder->where('is_active', true)->get();

$query = Product::where('is_active', true);

$results = ODataQueryBuilder::for($query, $request)
    ->allowedFilters('name', 'price')
    ->get();
bash
php artisan vendor:publish --tag=odata-config