{"id":25478836,"url":"https://github.com/atiqurcode/eloquent-many-to-many","last_synced_at":"2026-04-30T14:31:47.687Z","repository":{"id":113437885,"uuid":"480670964","full_name":"AtiqurCode/Eloquent-Many-to-Many","owner":"AtiqurCode","description":"Eloquent Many To Many relationship","archived":false,"fork":false,"pushed_at":"2022-04-12T05:53:04.000Z","size":77,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-05-18T06:07:23.634Z","etag":null,"topics":["eloquent","laravel","many-to-many","model","orm","relationships"],"latest_commit_sha":null,"homepage":"","language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/AtiqurCode.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"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":"2022-04-12T05:47:40.000Z","updated_at":"2022-04-12T05:52:03.000Z","dependencies_parsed_at":"2026-03-08T13:00:53.004Z","dependency_job_id":null,"html_url":"https://github.com/AtiqurCode/Eloquent-Many-to-Many","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/AtiqurCode/Eloquent-Many-to-Many","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/AtiqurCode%2FEloquent-Many-to-Many","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/AtiqurCode%2FEloquent-Many-to-Many/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/AtiqurCode%2FEloquent-Many-to-Many/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/AtiqurCode%2FEloquent-Many-to-Many/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/AtiqurCode","download_url":"https://codeload.github.com/AtiqurCode/Eloquent-Many-to-Many/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/AtiqurCode%2FEloquent-Many-to-Many/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":32468009,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-04-30T13:12:12.517Z","status":"ssl_error","status_checked_at":"2026-04-30T13:12:06.837Z","response_time":57,"last_error":"SSL_read: unexpected eof while reading","robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":false,"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":["eloquent","laravel","many-to-many","model","orm","relationships"],"created_at":"2025-02-18T14:53:47.147Z","updated_at":"2026-04-30T14:31:47.679Z","avatar_url":"https://github.com/AtiqurCode.png","language":"PHP","funding_links":[],"categories":[],"sub_categories":[],"readme":"## Read Many-to-Many basics \u0026 try with this code as you want\n\nGo throw **laravel documentation** to read about it \u0026 many more blog\n\nFirst you need to clone this and save **.env.example as .env** and setup your environment or just change the database configure\n\n```sh\nDB_CONNECTION=mysql\nDB_HOST=127.0.0.1\nDB_PORT=3306\nDB_DATABASE=your-database-name\nDB_USERNAME=your-database-user-name\nDB_PASSWORD=your-database-password(if have)\n```\n\nNow just run some command on terminal one by one\n\n```sh\ncomposer install\nphp artisan migrate\nphp artisan serve\n```\n\nfor dummy or fake data you can run\n```sh\nphp artisan migrate:refresh --seed\n```\n\nAs I have said this is Many to Many relationship. // Imagine part\n- Example1: A Product can have many category\n- Example2: A category can have many product\n- Example3: A Company can have many projects\n- Example4: A projects can be assigned many Company\n- Example5: A Book can be write by many Writters\n- Example6: A Writter can write many Books\n\nSo I have just created Two model\n- **Product**\n- **Category**\n\nAnd three migration file **products, categories \u0026 category_product**\n\nProduct Model are relation with hasOne\n\n```sh\n    public function categories()\n    {\n        return $this-\u003ebelongsToMany(Category::class);\n    }\n```\n\nCategory Model are relation with belongsTo\n\n```sh\n    public function products()\n    {\n        return $this-\u003ebelongsToMany(Product::class);\n    }\n\n    // You can save return $this-\u003ebelongsToMany(Product::class, 'category_product');\n    // $this-\u003ebelongsToMany(Product::class, 'category_product');\n    // relation pivot table name if you didn't follow the naming convention \n```\n\nAnd controller code are depend's on condition what I need. So you can go throw the controllers method.\n\nif you have **Postman**/any software like that please import One-to-Many.postman_collection.json file either call\n```sh\n- {url-of-your-app}/api/products                 --method : get      // get all product with categories list\n- {url-of-your-app}/api/products/{product}       --method : get      // get single product with categories list\n- {url-of-your-app}/api/products                 --method : post     // insert product details \u0026 category\n- {url-of-your-app}/api/products/{product}       --method : put      // update product details \u0026 category\n- {url-of-your-app}/api/products/{product}       --method : delete   // delete product and remove category relations \n- {url-of-your-app}/api/categories               --method : get      // get all category \n- {url-of-your-app}/api/categories/{category}    --method : get      // get all product under the category\n- {url-of-your-app}/api/categories/              --method : post     // create category\n- {url-of-your-app}/api/categories/{category}    --method : put      // update category by category/id\n- {url-of-your-app}/api/categories/{category}    --method : delete   // delete category by category/id\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fatiqurcode%2Feloquent-many-to-many","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fatiqurcode%2Feloquent-many-to-many","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fatiqurcode%2Feloquent-many-to-many/lists"}