{"id":13631365,"url":"https://github.com/Genaker/laragento","last_synced_at":"2025-04-17T21:32:48.217Z","repository":{"id":62509693,"uuid":"381246068","full_name":"Genaker/laragento","owner":"Genaker","description":"Laravel/Eloquent ORM for Magento that allows you to get data directly from a Magento 2 database tables bypassing slow M2 core code.","archived":false,"fork":false,"pushed_at":"2024-04-03T00:21:29.000Z","size":343,"stargazers_count":100,"open_issues_count":8,"forks_count":24,"subscribers_count":7,"default_branch":"main","last_synced_at":"2025-04-13T10:51:09.468Z","etag":null,"topics":["eloquent","laravel","magento","magento2","php"],"latest_commit_sha":null,"homepage":"","language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"gpl-3.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/Genaker.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE.md","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}},"created_at":"2021-06-29T05:10:42.000Z","updated_at":"2025-04-11T13:22:35.000Z","dependencies_parsed_at":"2024-04-03T01:40:55.181Z","dependency_job_id":null,"html_url":"https://github.com/Genaker/laragento","commit_stats":{"total_commits":55,"total_committers":1,"mean_commits":55.0,"dds":0.0,"last_synced_commit":"c30d00fe9447585ec3c4a16681e58f6322869bf5"},"previous_names":[],"tags_count":2,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Genaker%2Flaragento","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Genaker%2Flaragento/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Genaker%2Flaragento/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Genaker%2Flaragento/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/Genaker","download_url":"https://codeload.github.com/Genaker/laragento/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":249381007,"owners_count":21261227,"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","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":["eloquent","laravel","magento","magento2","php"],"created_at":"2024-08-01T22:02:22.636Z","updated_at":"2025-04-17T21:32:48.199Z","avatar_url":"https://github.com/Genaker.png","language":"PHP","readme":"# Laragento Laravel Magento Microservices\n\nMagento 2 has legacy code based on abandoned **Zend Framework 1** with really ugly ORM on top of outdated **Zend_DB**. Zend_DB is not even in the latest version. \n\nThis repo is a collection of Model classes that allows you to get data directly from a Magento 2 database using modern **ORM**.\n\n\u003cimg src=\"https://raw.githubusercontent.com/Genaker/laragento/main/LaragentoMgentoLaravel.png\" alt=\"LaraGento\"\u003e\n\nLaragento is a collection of PHP classes built on top of **Eloquent ORM** (from **Laravel** framework), that provides a fluent interface to connect and get data directly from a Magento database.\n\nYou can use legacy Magento 2 as the backend (administration panel), and any other PHP app  (Symfony/Laravel/Lumen/Vanilla.PHP etc.) and query that data (as a Model layer). It's easier to use Laragento with Laravel, but you're free to use it with any PHP project using Composer.\n\n# History \n\nAfter the releases of the VueStorefron and Hyva Theme (A brand-new frontend for Magento 2 without reusing Magento UI and using Laravel ecosystem as a base Alpine.JS / Tailwind CSS) everybody understands that the only way to build a good eCommerce Website is not to use M2 broken legacy frontend/backend functionality. I have been using this library for the last 3 years and see only benefits. Even if you need reimplement functionality from scratch it is much faster than reusing Adobe Commerce broken core framework bloatware.\n\n**Why Laravel and Eloquent?**\n\nLaravel is the most popular PHP framework. I have used different ORMs and Eloquent is the best.\n\n# Magento ORM Concept\n\nData is the essence of any web application. Models are the essence of the ORM. A model is an abstraction that represents a magento table in the database. In Larafento, it is a class that extends base Eloquent/Model.\n\nThe model tells Eloquent several things about the entity it represents, such as the name of the table in the database and optionally which columns it has (and their data types).\n\nA model has a name. This name does not have to be the same name of the Magento table it represents in the database. catalog_product_entity -\u003e CatalogProductEntity. \n\n# Extand Base Magento Model\n\nCustomizations involve creating new Eloquent models that extend Magento base models. By extending a model, you inherit the full functionality of the parent model, while retaining the ability to add customization, scopes, event listeners, etc. Also, a Repository pattern can be used but it is another story. A base model represents a Datase table operation only.  \n\n```\nclass ProductSimple extends CatalogProductEntity\n{\n   public static function boot()\n    {\n        parent::boot();\n\n        static::addGlobalScope(function ($query) {\n            $query-\u003ewhere('type_id', 'simple');\n        });\n    }\n    \n   public function getBySku(string $sku) {\n        $this-\u003ewhere('sku', $sku);\n   }\n}\n```\n\n# Documentation \nThis solution doesn't require documentation because it reuses a widely used software development tool vs Magento 2 in-house built framework. \n\n**Official Eloquent Documentation:** https://laravel.com/docs/8.x/eloquent \n\nIf you have any issues and Enterprise (Adobe Commerce) Version support create a ticket or drop me an email at: yegorshytikov@gmail.com\n\n# Install Laragento \n\nYou need to use Composer to install Laragento into your project:\n\n```\ncomposer config repositories.foo '{\"type\": \"vcs\", \"url\": \"https://github.com/Genaker/laragento\", \"trunk-path\": \"master\"}'\ncomposer require Genaker/laragento\n```\nThe installation via Composer is not recommended. Please use the Other PHP Framework installation by adding an additional autoload file instead of the Composer version. We are not able to provide compatibility to all Magento versions anymore. Also, Magento uses outdated packages and Eloquent replaces those dependencies with new ones and that produces the issues. But, you can try, there is a way to fix. Read -\u003e **Integration with Magento 2 / 1 Framework**\n\n#Install without Eloquent. Minimal \n```\ncomposer require illuminate/database\n```\n\n# Other PHP Framework (not Laravel and Magento) Setup\n\nHere you have to configure the database to work with Magento. First, you should include the Composer autoload file if not already loaded:\n```\nrequire __DIR__ . '/vendor/autoload.php';\n```\nNow you must set your Magento2 database params:\n```\n$params = array(\n    'database'  =\u003e 'database_name',\n    'username'  =\u003e 'username',\n    'password'  =\u003e 'pa$$word',\n    'prefix'    =\u003e '' \n);\nLaragento\\DB::connect($params);\n```\nYou can specify all Eloquent params, but some are default (but you can override them).\n```\n'driver'    =\u003e 'mysql',\n'host'      =\u003e 'localhost',\n'charset'   =\u003e 'utf8',\n'collation' =\u003e 'utf8_unicode_ci',\n'prefix'    =\u003e '', \n```\nMagento automated DB connection script:\n```\n\t$conf = include(BP . \"/app/etc/env.php\");\n\t$db = $conf[\"db\"][\"connection\"][\"default\"]; \n\t$host = $db[\"host\"];\n\t$user = $db[\"username\"];\n\t$password = $db[\"password\"];\n\t$dbname = $db[\"dbname\"];\n\n\t$params = array(\n\t\t'host' =\u003e $db[\"host\"],\n\t\t'database' =\u003e $dbname,\n\t\t'username' =\u003e $user,\n\t\t'password' =\u003e $password,\n\t\t'prefix' =\u003e '' \n\t);\n   \n   Laragento\\DB::connect($params);\n```\n\nHowever it will be better to use it as a separate Magento-les microservice entry point in the separate directory together with Laravel or Lumen.\n\nTo make it simple, we configuration to json and use it and read config.json from the original config\n\n```\nphp -r '$x = include(\"app/etc/env.php\"); echo json_encode($x);' \u003e config.json\n```\n\n# LAravel Magento with Tinker (REPL)\n\nLaravel Tinker is a powerful REPL for the Laravel framework, powered by the PsySH package.\n\n# Tinker Installation\n\nAll Laravel applications include Tinker by default. However, you may install Tinker using Composer if you have previously removed it from your application:\n\n```\ncomposer create-project laravel/laravel laragento \ncd laragento\ncomposer require laravel/tinker\ncomposer config repositories.foo '{\"type\": \"vcs\", \"url\": \"https://github.com/Genaker/laragento\", \"trunk-path\": \"master\"}'\ncomposer require Genaker/laragento:dev-main\n```\n\nSet proper DB to connect in the **.env**  file:\n\n```\nDB_CONNECTION=mysql\nDB_HOST=127.0.0.1\nDB_PORT=3306\nDB_DATABASE=laravel\nDB_USERNAME=root\nDB_PASSWORD=\n```\n\n# Tinker Usage\n\nTinker allows you to interact with your entire Laravel application on the command line, including your Eloquent models (Laragento), jobs, events, and more. To enter the Tinker environment, run the tinker Artisan command:\n```\nphp artisan tinker\n\n$products = Laragento\\Models\\CatalogProductEntity::first();\n\n$products = Laragento\\Models\\CatalogProductEntity::where('sku', '21157');\n\n$products = Laragento\\Models\\CatalogProductEntity::where('sku', '21157')-\u003ewith('catalog_product_entity_varchars')-\u003eget();\n\n$orders = Laragento\\Models\\SalesOrder::limit(10)-\u003ewith('sales_order_items', 'sales_invoices', 'sales_order_payments', 'sales_creditmemos', 'sales_order_addresses', 'sales_shipments', 'customer_entity')-\u003eget(); \n$orders-\u003etoJson();\n\n$creditMemo = Laragento\\Models\\SalesCreditmemo::limit(1)-\u003ewith('sales_order','sales_creditmemo_items','sales_creditmemo_comments', 'sales_creditmemo_items')-\u003eget(); $creditMemo-\u003etoArray()\n\n$invoices = Laragento\\Models\\SalesInvoice::limit(1)-\u003ewith('sales_order','sales_invoice_items','sales_invoice_comments')-\u003eget(); $invoices-\u003etoJson();\n\n```\n# Example of the Magento Order to ERM implementation \n\n```\nrequire __DIR__ . '/vendor/autoload.php';\n\n$orders = Laragento\\Models\\SalesOrder::whereNull('sync_status')-\u003ewith('sales_order_items', 'sales_invoices', 'sales_order_payments', 'sales_creditmemos', 'sales_order_addresses', 'sales_shipments', 'customer_entity', 'sales_payment_transactions')-\u003eget();\n \nforeach ($oreders as $order) {\n    $response = $erp-\u003eorderAPI($order-\u003etoJson());\n    if ($response-\u003eresponseCode === 200) {\n        $order-\u003esync_status = 'sent_to_erp';\n        $order-\u003esave();\n    } else {\n        echo \"Error\";\n    }\n}\n\n```\nNo ObjectManager, DI, and other stuff is required. \n\n# Laravel/Eloquent and static::methods\n\nSome Magento 2 bloatware lovers hate static methods. For those guys Laravel has a solution...\n\nStatic Example: \n```\n$user = User::find(1);\nvar_dump($user-\u003ename);\n```\n\nLaravel isn't using a static method, you are. Another way to do this which you are probably looking for is to use dependency injection, which Laravel makes very easy because it can be done automatically. So in whatever class you are using your User model in, you should be setting up something like this in the constructor...\n\n```\npublic function __construct(User $user)\n{\n    $this-\u003euser = $user;\n}\n```\n\nAnd then you can modify your code to not use the static bindings.\n\n```\n$user = $this-\u003euser-\u003efind(1);\nvar_dump($user-\u003ename);\n```\n\nMore about Eloquent you can watch here:\n\nhttps://www.youtube.com/watch?v=uyQH90okBNQ\u0026list=PL8nVHL94VZ18EGJX9iSR01Xx7vgzX6Uar\n\n# Integration with Magento 2 / 1 Framework \n\nInstall this ORM to the separate folder \n```\nmkdir orm\ncd orm\ncomposer require genaker/laragento @dev\n```\n\nRun this command. Magento 2 uses outdated Monolog version and has conflict in the composers: \n\n```\nfind ./orm/vendor/ -type f -exec sed -i -e 's/Monolog/Monolog2/g' {} \\;\nmv ./orm/vendor/monolog/monolog/src/Monolog/ vendor/monolog/monolog/src/Monolog2/\n```\n\nadd to the **app/bootstrap.php** additional autoloader \n```\n# After This\nrequire_once __DIR__ . '/autoload.php';\n# Insert This\nrequire_once __DIR__ . '/../orm/vendor/autoload.php';\n\n```\nOverhead of loading LAravel with 100 product in magento is : 0.0051751136779785 seconds \n10 products: 0.0026481151580811 seconds.\nJust autoload:  0.00015616416931152 seconds.\nSo, there is no overhead of adding this life hack to Magento. Magento 2 framework is an instinct dinosaur. \n\n# Magento GraphQL for Laravel/Eloquent\n\nLighthouse is a GraphQL framework that integrates with MAgento Eloquent Laravel application. It takes the best ideas of both and combines them to solve common tasks with ease and offer flexibility when you need it.\n\nMore information:\n\nhttps://lighthouse-php.com/\n\nIt is really Cool Magento developers can enjoy building modern software and not just debug and troubleshoot broken MAgento 2 \n\n# Debug Queries using the Laravel Query Log\nLaravel query log that collects all queries within a request. You can enable this log, run your query and dump the output.\n\n```\nDB::enableQueryLog();\n\nApp\\User::query()\n    -\u003ewhere('created_at', '\u003c', now()-\u003esubYear())\n    -\u003ewith('assignedApps', 'courses')\n    -\u003eorderBy('email', 'asc')\n    -\u003elimit(5)\n    -\u003eget();\n\ndump(DB::getQueryLog());\n```\n\n# Listening For Query Events\n\nIf you would like to receive each SQL query executed by your application, you may use the listen method. This method is useful for logging queries or debugging. You may register your query listener in a service provider:\n\napp/Providers/AppServiceProvider.php\n\n```\n\u003c?php\n\nnamespace App\\Providers;\n\nuse Illuminate\\Support\\Facades\\DB;\nuse Illuminate\\Support\\ServiceProvider;\n\nclass AppServiceProvider extends ServiceProvider\n{\n    /**\n     * Bootstrap any application services.\n     *\n     * @return void\n     */\n    public function boot()\n    {\n        DB::listen(function ($query) {\n           var_dump(\n                $query-\u003esql,\n                $query-\u003ebindings,\n                $query-\u003etime\n            );\n        });\n    }\n\n    /**\n     * Register the service provider.\n     *\n     * @return void\n     */\n    public function register()\n    {\n        //\n    }\n}\n```\n\n## Seed Magento table data\n\nUsing Laragento together with https://github.com/orangehill/iseed we can generate a new seed file based on data from the existing Magento database table.\n\n\n\n\n\n\n","funding_links":[],"categories":["PHP"],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2FGenaker%2Flaragento","html_url":"https://awesome.ecosyste.ms/projects/github.com%2FGenaker%2Flaragento","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2FGenaker%2Flaragento/lists"}