PHP code example of firevel / includes

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

    

firevel / includes example snippets


use Firevel\Includes\IncludesParser;

$with = app(IncludesParser::class)
    ->parseIncludes($request->input('.
        return function ($relationship) use ($parameters) {
            if (isset($parameters['limit'])) {
                $relationship->limit((int) $parameters['limit']);
            }

            return $relationship;
        };
        // ...or return null to load the relationship "open" (no constraint).
    });

$posts = Post::with($with)->paginate();

use Firevel\Includes\HasIncludes;

class Post extends Model
{
    use HasIncludes;

    // Optional allowlist. Omit it to accept any real relationship
    // (validated against the model). Mirrors Fractal's $availableIncludes.
    protected $availableIncludes = ['comments', 'comments.replies', 'author'];

    // Optional policy. Default: every relationship is loaded "open".
    // Override to apply your own per-relationship constraints.
    protected function  {
                $relationship->where($filters);
            }
            if ($limit !== null) {
                $relationship->limit((int) $limit);   // per-parent on Laravel 11+
            }

            return $relationship;
        };
    }
}

$posts = Post::query()
    ->withIncludes($request->input('  ->parseIncludes(Post::

protected function ontext + [null];

    return fn (array $parameters) => function ($relationship) use ($parameters, $user) {
        $sort  = $parameters['sort']  ?? null;
        $limit = $parameters['limit'] ?? null;

        // Everything else is a filter. A comma value means "any of these",
        // which firevel/filterable expresses through its `in` operator.
        $filters = [];
        foreach (\Illuminate\Support\Arr::except($parameters, ['sort', 'limit']) as $column => $value) {
            $filters[$column] = str_contains((string) $value, ',') ? ['in' => $value] : $value;
        }

        $relationship->visibleBy($user);                          // authorization
        if ($filters)        $relationship->filter($filters);     // firevel/filterable
        if ($sort)           $relationship->sort($sort);          // firevel/sortable (comma string)
        if ($limit !== null) $relationship->limit((int) $limit);  // per-parent on Laravel 11+

        return $relationship;
    };
}

->generateWith(fn (array $parameters) => function ($relationship) use ($parameters) {
    foreach ($parameters as $column => $value) {
        $relationship->whereIn($column, explode(',', (string) $value));
    }

    return $relationship;
});

->generateWith(fn (array $parameters) =>
    $parameters === [] ? null : fn ($relationship) => $relationship->where($parameters)
);

->generateWith(fn (array $parameters) => function ($relationship) use ($parameters) {
    $related = $relationship->getRelated();

    if ($related instanceof \App\Models\Facility) {
        return $relationship;                          // open
    }

    return $relationship->where($parameters);          // constrained
});

return $relationship->visibleBy($user)->filter($parameters);

->generateWith(fn (array $parameters) => function ($relationship) use ($parameters) {
    if ($relationship instanceof \Illuminate\Database\Eloquent\Relations\MorphTo) {
        return $relationship->constrain([
            \App\Models\Post::class  => fn ($q) => $q->where($parameters),
            \App\Models\Video::class => fn ($q) => $q->where($parameters),
        ]);
    }

    return $relationship->where($parameters);
});

$parser->setAllowedIncludes(['comments', 'comments.replies']);

$parser->setModel(Post::class); // instance or class string

return [
    // Maximum dot-notation nesting depth, counted in segments.
    'max_depth' => 5,

    // 'ignore' to silently drop disallowed paths, 'throw' to raise an exception.
    'on_disallowed' => 'ignore',
];

use Firevel\Includes\IncludesParser;

$with = (new IncludesParser(maxDepth: 3, onDisallowed: 'throw'))
    ->setAllowedIncludes(['comments'])
    ->parseIncludes($
bash
php artisan vendor:publish --tag=