{"id":19353599,"url":"https://github.com/telkins/laravel-dag-manager","last_synced_at":"2025-10-07T19:49:53.665Z","repository":{"id":27503600,"uuid":"114165854","full_name":"telkins/laravel-dag-manager","owner":"telkins","description":"A SQL-based Directed Acyclic Graph (DAG) solution for Laravel.","archived":false,"fork":false,"pushed_at":"2025-04-12T21:32:29.000Z","size":120,"stargazers_count":32,"open_issues_count":4,"forks_count":9,"subscribers_count":2,"default_branch":"main","last_synced_at":"2025-10-05T02:29:14.922Z","etag":null,"topics":["dag","directed-acyclic-graph","hacktoberfest","laravel","sql"],"latest_commit_sha":null,"homepage":"","language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/telkins.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":"CONTRIBUTING.md","funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null}},"created_at":"2017-12-13T20:30:35.000Z","updated_at":"2025-04-12T21:30:48.000Z","dependencies_parsed_at":"2023-01-14T06:54:09.371Z","dependency_job_id":"613cfc26-287e-4a11-a125-c20792400e8b","html_url":"https://github.com/telkins/laravel-dag-manager","commit_stats":{"total_commits":83,"total_committers":4,"mean_commits":20.75,"dds":"0.27710843373493976","last_synced_commit":"f79b5844d251bceb82bdfee0e02c13ecbad74a18"},"previous_names":[],"tags_count":27,"template":false,"template_full_name":null,"purl":"pkg:github/telkins/laravel-dag-manager","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/telkins%2Flaravel-dag-manager","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/telkins%2Flaravel-dag-manager/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/telkins%2Flaravel-dag-manager/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/telkins%2Flaravel-dag-manager/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/telkins","download_url":"https://codeload.github.com/telkins/laravel-dag-manager/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/telkins%2Flaravel-dag-manager/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":278838019,"owners_count":26054720,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","status":"online","status_checked_at":"2025-10-07T02:00:06.786Z","response_time":59,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["dag","directed-acyclic-graph","hacktoberfest","laravel","sql"],"created_at":"2024-11-10T04:43:24.449Z","updated_at":"2025-10-07T19:49:53.659Z","avatar_url":"https://github.com/telkins.png","language":"PHP","funding_links":[],"categories":[],"sub_categories":[],"readme":"# A SQL-based Directed Acyclic Graph (DAG) solution for Laravel\n\n[![Latest Stable Version](https://poser.pugx.org/telkins/laravel-dag-manager/v/stable)](https://packagist.org/packages/telkins/laravel-dag-manager)\n![run tests](https://github.com/telkins/laravel-dag-manager/workflows/run%20tests/badge.svg)\n[![Total Downloads](https://poser.pugx.org/telkins/laravel-dag-manager/downloads)](https://packagist.org/packages/telkins/laravel-dag-manager)\n[![License](https://poser.pugx.org/telkins/laravel-dag-manager/license)](https://packagist.org/packages/telkins/laravel-dag-manager)\n\nThis package allows you to create, persist, and remove directed acyclic graphs.\n\n* [Basic Usage](#basic-usage)\n* [Installation](#installation)\n* [Usage](#usage)\n* [Warning](#warning)\n* [Testing](#testing)\n* [Additional Notes](#additional-notes)\n* [Changelog](#changelog)\n* [Contributing](#contributing)\n* [Security](#security)\n* [Credits](#credits)\n* [License](#license)\n\n## Basic Usage\n\nCreating a direct edge:\n\n```php\n$newEdges = dag()-\u003ecreateEdge($startVertex, $endVertex, $source);\n// $newEdges contains all new edges, including the specified direct edge, that were created as a result of the request.\n```\n\nDeleting a direct edge:\n\n```php\n$deleted = dag()-\u003edeleteEdge($startVertex, $endVertex, $source);\n// $deleted is true if any edges were deleted as a result of the request, false otherwise.\n```\n\n## Installation\n\nThe latest version of this package requires PHP 8.2 or higher as well as Laravel 12.0 or higher.\n\nYou can install the package via composer:\n\n```bash\ncomposer require telkins/laravel-dag-manager\n```\n\nThe package will automatically register itself.\n\nYou can publish the migration with:\n\n```bash\nphp artisan vendor:publish --provider=\"Telkins\\Dag\\Providers\\DagServiceProvider\" --tag=\"migrations\"\n```\n\nNote: The default migration assumes you are using integers for your DAG edge IDs.\n\nYou can optionally publish the config file with:\n```bash\nphp artisan vendor:publish --provider=\"Telkins\\Dag\\Providers\\DagServiceProvider\" --tag=\"config\"\n```\n\nThis is the contents of the published config file:\n```php\nreturn [\n    /**\n     *-------------------------------------------------------------------------\n     * Max Hops\n     *-------------------------------------------------------------------------\n     *\n     * This value represents the maximum number of hops that are allowed where\n     * hops \"[i]ndicates how many vertex hops are necessary for the path; it is\n     * zero for direct edges\".\n     *\n     * The more hops that are allowed (and used), then the more DAG edges will\n     * be created.  This will have an increasing impact on performance, space,\n     * and memory.  Whether or not it's negligible, noticeable, or impactful\n     * depends on a variety of factors.\n     */\n    'max_hops' =\u003e 5,\n\n    /**\n     *-------------------------------------------------------------------------\n     * Default Database Connection Name\n     *-------------------------------------------------------------------------\n     *\n     * This is the name of the database connection where the dag table\n     * can be found.\n     *\n     * Set to `null` to use the default connection.\n     */\n    'default_database_connection_name' =\u003e null,\n\n    /**\n     *-------------------------------------------------------------------------\n     * Table Name\n     *-------------------------------------------------------------------------\n     *\n     * This is the name of the table where the dag structure\n     * will be stored.\n     */\n    'table_name' =\u003e 'dag_edges',\n];\n```\n\n## Warning\n\nFrom Kemal Erdogan's article, [\"A Model to Represent Directed Acyclic Graphs (DAG) on SQL Databases\"](https://www.codeproject.com/Articles/22824/A-Model-to-Represent-Directed-Acyclic-Graphs-DAG-o):\n\n\u003eIn theory, the size of the transitive closure set of a fair DAG can be very large with this model, well beyond the millions. The maximum number of edges for a given DAG itself is a research topic in Graph Theory, but my practical tests show that there exist DAGs with 100 vertices and 300 edges whose transitive closure would create well beyond 20,000,000 rows with this algorithm.\n\nPlease be mindful of this when creating \"excessively\" large and/or complex graphs.\n\n## Usage\n\nFor Eloquent models that are \"DAG managed\", you can add the `Telkins\\Models\\Traits\\IsDagManaged` trait:\n```php\nuse Illuminate\\Database\\Eloquent\\Model;\nuse Telkins\\Models\\Traits\\IsDagManaged;\n\nclass MyModel extends Model\n{\n    use IsDagManaged;\n\n    // ...\n}\n```\n\nThis will allow you to easily access certain functionality from your model class.\n\nTo apply a scope that only includes models that are descendants of the specified model ID:\n```php\n$descendants = MyModel::dagDescendantsOf($myModel-\u003eid, 'my-source')-\u003eget();\n```\n\nAn ID and source must be provided.\n\nLikewise, to apply a scope that only includes models that are ancestors of the specified model ID:\n```php\n$ancestors = MyModel::dagAncestorsOf($myModel-\u003eid, 'my-source')-\u003eget();\n```\n\nAgain, an ID and source must be provided.\n\nFinally, one can apply a scope that will get both ancestors and descendants:\n```php\n$ancestors = MyModel::dagRelationsOf($myModel-\u003eid, 'my-source')-\u003eget();\n```\n\nEach of the aforementioned methods also allow the caller to constrain the results based on the number of hops.  So, if you want to get the immediate children of the specified model ID, then you could do the following:\n```php\n$descendants = MyModel::dagDescendantsOf($myModel-\u003eid, 'my-source', 0)-\u003eget();\n```\n\nAnd, of course, in order to get the parents and grandparents of the specified model ID, you could do the following:\n```php\n$ancestors = MyModel::dagAncestorsOf($myModel-\u003eid, 'my-source', 1)-\u003eget();\n```\n\nNot providing the `$maxHops` parameter means that all descendants, ancestors, or relations will be returned.\n\n### Custom DAG edge model\n\nYou can use your own model class if you need to customise the behavior of the DAG edge model.\n\nYour custom model class must extend the `Telkins\\Models\\DagEdge` class:\n\n```php\nnamespace App\\Models;\n\nuse Telkins\\Models\\DagEdge as BaseModel;\n\nclass MyDagEdge extends BaseModel\n{\n...\n```\n\nYou can then specify the fully qualified class name of your custom model in the package config file.\n\n```php\n// config/laravel-dag-manager.php\n...\n'edge_model' =\u003e \\App\\Models\\MyDagEdge::class,\n...\n```\n\n## Testing\n\n```bash\ncomposer test\n```\n\n## Additional Notes\n\nContributors may want to consider leveraging any of the following:\n* [relaxedws/lca](https://github.com/relaxedws/lca): A PHP Library to find Lowest Common ancestor from a Directed Acyclic Graph.\n* [clue/graph](https://github.com/clue/graph): A mathematical graph/network library written in PHP.\n* [graphp/algorithms](https://github.com/graphp/algorithms): Graph algorithms in PHP, a collection of common (and not so common) ones.\n\n## Changelog\n\nPlease see [CHANGELOG](CHANGELOG.md) for more information what has changed recently.\n\n## Contributing\n\nPlease see [CONTRIBUTING](CONTRIBUTING.md) for details.\n\n## Security\n\ntbd\n\n## Credits\n\n- [Travis Elkins](https://github.com/telkins)\n- [All contributors](../../contributors)\n\nAdditionally:\n- Kemal Erdogan and his article entitled, [\"A Model to Represent Directed Acyclic Graphs (DAG) on SQL Databases\"](https://www.codeproject.com/Articles/22824/A-Model-to-Represent-Directed-Acyclic-Graphs-DAG-o).\n- [xib](https://github.com/xib) and his MySQL stored procedures port (which is not currently used, but may be in a future version): [xib/DAG_MySQL.sql](https://gist.github.com/xib/21786eeaa970911f0693)\n\n## License\n\nThe MIT License (MIT). Please see [License File](LICENSE) for more information.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftelkins%2Flaravel-dag-manager","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Ftelkins%2Flaravel-dag-manager","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftelkins%2Flaravel-dag-manager/lists"}