{"id":20972023,"url":"https://github.com/kamva/moloquent","last_synced_at":"2025-05-14T11:34:01.710Z","repository":{"id":57004285,"uuid":"73504547","full_name":"Kamva/moloquent","owner":"Kamva","description":"An extended jenssegers/laravel-mongodb library","archived":false,"fork":false,"pushed_at":"2018-06-23T10:06:56.000Z","size":23,"stargazers_count":5,"open_issues_count":0,"forks_count":1,"subscribers_count":3,"default_branch":"master","last_synced_at":"2025-04-02T17:51:22.142Z","etag":null,"topics":["laravel-mongodb","mongodb","mongodb-collections","objectid","relations"],"latest_commit_sha":null,"homepage":"http://kamva.ir","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/Kamva.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2016-11-11T19:16:27.000Z","updated_at":"2019-10-26T15:13:17.000Z","dependencies_parsed_at":"2022-08-21T12:10:51.456Z","dependency_job_id":null,"html_url":"https://github.com/Kamva/moloquent","commit_stats":null,"previous_names":[],"tags_count":8,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Kamva%2Fmoloquent","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Kamva%2Fmoloquent/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Kamva%2Fmoloquent/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Kamva%2Fmoloquent/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/Kamva","download_url":"https://codeload.github.com/Kamva/moloquent/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":254131949,"owners_count":22020052,"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":["laravel-mongodb","mongodb","mongodb-collections","objectid","relations"],"created_at":"2024-11-19T04:06:17.066Z","updated_at":"2025-05-14T11:34:01.680Z","avatar_url":"https://github.com/Kamva.png","language":"PHP","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Moloquent\nAn extended [jenssegers/laravel-mongodb][1] library\n\n## Notes and Laravel Compatibility\n\n* This package is a wrapper around `jenssegers/mongodb`. Usages and\nconfigurations are the same as base package, except new features and changed behaviours.\nThey have covered in the following sections.\n* This packages uses `jenssegers/mongodb` version `3.0` and it is compatible\nwith laravel `5.1` and `5.2`.\n\n## What has Added and What has Changed\n\n[jenssegers/laravel-mongodb][1] is a great package but it does not support DBRefs\n([read more][2]). The problem was that related model ID stored as\nstring *(value of related document ObjectID)*, and not MongoDB ObjectID itself.\nThis is ok and work well until you want to join related collection by using `lookup`\naggregation ([read more][3]). In this package we change the way that the related document\nID stores.\n\nIn MongoDB, relations are different than SQL RDBMS. For example a `One-to-Many` relation\ncan be categorized into three type; `One-to-Few`, `One-to-Many`, `One-to-Squillions`. And\neach of them store related IDs with different strategy. Other factors like whether the\nrelated entity is weak or strong entity are effective in selecting the best strategy.\nIn this package we provided new model relations for covering these type of relations.\n\nYou can read more about this topic in these links.\n* [6 Rules of Thumb for MongoDB Schema Design: Part 1][4]\n* [6 Rules of Thumb for MongoDB Schema Design: Part 2][5]\n* [6 Rules of Thumb for MongoDB Schema Design: Part 3][6]\n\n## Installation\n\nAdd moloquent package to your `compoer.json`:\n\n```json\n\"require\": {\n  \"kamva/moloquent\": \"~1.0\"\n}\n```\n\nAnd then do a `composer update`. \n\nOr run the following command on your console:\n\n```\ncomposer require kamva/moloquent ~1.0\n```\n\n### Registering the package\nRegister the service provider within the providers array found in config/app.php:\n\n```php\n'providers' =\u003e [\n    // ...\n    Kamva\\Moloquent\\MoloquentServiceProvider::class\n]\n```\n\n### Configuration\n\nChange your default database connection name in config/database.php:\n\n```php\n'default' =\u003e env('DB_CONNECTION', 'mongodb'),\n```\nAnd add a new mongodb connection:\n\n```php\n'mongodb' =\u003e [\n    'driver'   =\u003e 'mongodb',\n    'host'     =\u003e env('DB_HOST', 'localhost'),\n    'port'     =\u003e env('DB_PORT', 27017),\n    'database' =\u003e env('DB_DATABASE'),\n    'username' =\u003e env('DB_USERNAME'),\n    'password' =\u003e env('DB_PASSWORD'),\n    'options' =\u003e [\n        'database' =\u003e 'admin' // sets the authentication database required by mongo 3\n    ]\n],\n```\nYou can connect to multiple servers or replica sets with the following configuration:\n\n```php\n'mongodb' =\u003e [\n    'driver'   =\u003e 'mongodb',\n    'host'     =\u003e env('DB_HOST', 'localhost'),\n    'port'     =\u003e env('DB_PORT', 27017),\n    'database' =\u003e env('DB_DATABASE', 'local'),\n    'username' =\u003e env('DB_USERNAME'),\n    'password' =\u003e env('DB_PASSWORD'),\n    'options'  =\u003e []\n],\n```\n\n## Usage\n\n### Models\n\nExtend your models from `Kamva/Moloquent/Moloquent`, and your done.\n\n```php\nuse Kamva/Moloquent/Moloquent;\n\nclass User extend Moloquent {}\n```\n\n### Relations\n\nSupported relations are:\n\n- hasOne\n- hasMany\n- belongsTo\n- belongsToMany\n- embedsOne\n- embedsMany\n- ContainsOne\n- ContainsFew\n- IncludedIn\n\nThe first 6 relations are the same as base package, with a little different that IDs store\nas ObjectID instead of value of ObjectID (string)\n\n#### ContainsOne\n\nIt is `One-to-One`'s sister. The difference is that the child ID will store in parent\ndocument. Here is an example:\n\n```php\nuse Kamva/Moloquent/Moloquent;\n\nclass User extend Moloquent {\n\n    public function avatar() {\n        return $this-\u003econtainsOne('Image');\n    }\n\n}\n```\n\nAnd the document on mongodb users collection would be like this:\n\n```bson\n{\n    \"_id\" : ObjectId(\"57bbca61551dfe007c67427e\"),\n    \"email\" : \"sample@mail.com\",\n    \"password\" : \"[hashed passowrd]\",\n    \"image_id\" : ObjectId(\"57d2b82e1284c5008032521f\")\n}\n```\n\n#### ContainsFew\n\nIt is a `One-to-Many` relation that children are few, and children are strong entity\n(or there's a need to access them directly). So we want to save the children IDs in parent.\nHere is an example:\n\n```php\nuse Kamva/Moloquent/Moloquent;\n\nclass User extend Moloquent {\n\n    public function phones() {\n        return $this-\u003econtainsFew('Phone');\n    }\n\n}\n```\n\nAnd an example document would be like this:\n\n```bson\n{\n    \"_id\" : ObjectId(\"57bbca61551dfe007c67427e\"),\n    \"email\" : \"sample@mail.com\",\n    \"password\" : \"[hashed passowrd]\",\n    \"phone_ids\" : [\n        ObjectId(\"57d2ba501284c500826dfbd5\"),\n        ObjectId(\"57ba1e7d551dfe007a7e8cc1\")\n    ]\n}\n```\n\n#### IncludedIn\n\nIt is the opposite relation of `ContainsOne` and `ContainsFew` when the child (current instance) is included in\none parent.\n\n```php\nuse Kamva/Moloquent/Moloquent;\n\nclass Phone extend Moloquent {\n\n    public function owner() {\n        return $this-\u003eIncludedIn('User');\n    }\n\n}\n```\n\n#### IncludedInMany\n\nThere are situations that the child is included in many instances of a model, for these situation we use\n`IncludedInMany` relation. \n\n```php\nuse Kamva/Moloquent/Moloquent;\n\nclass Address extend Moloquent {\n\n    public function order() {\n        return $this-\u003eIncludedInMany('Order');\n    }\n\n}\n```\n\n[فروشگاه ساز][7]\n\n\n[1]: https://github.com/jenssegers/laravel-mongodb\n[2]: https://docs.mongodb.com/manual/reference/database-references/\n[3]: https://docs.mongodb.com/master/reference/operator/aggregation/lookup/\n[4]: http://blog.mongodb.org/post/87200945828/6-rules-of-thumb-for-mongodb-schema-design-part-1\n[5]: http://blog.mongodb.org/post/87892923503/6-rules-of-thumb-for-mongodb-schema-design-part-2\n[6]: http://blog.mongodb.org/post/88473035333/6-rules-of-thumb-for-mongodb-schema-design-part-3\n[7]: http://kamva.ir\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fkamva%2Fmoloquent","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fkamva%2Fmoloquent","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fkamva%2Fmoloquent/lists"}