{"id":510,"date":"2018-05-20T22:47:51","date_gmt":"2018-05-20T13:47:51","guid":{"rendered":"https:\/\/blog.capilano-fw.com\/?p=510"},"modified":"2018-05-20T22:47:51","modified_gmt":"2018-05-20T13:47:51","slug":"laravel%e3%81%aemany-to-many%e5%a4%9a%e5%af%be%e5%a4%9a%e3%83%86%e3%83%bc%e3%83%96%e3%83%ab%e5%ae%9f%e4%be%8b","status":"publish","type":"post","link":"https:\/\/blog.capilano-fw.com\/?p=510","title":{"rendered":"Laravel\u306eMany-to-Many(\u591a\u5bfe\u591a)\u30c6\u30fc\u30d6\u30eb\u5b9f\u4f8b"},"content":{"rendered":"<p>\u3055\u3066\u3055\u3066\u3001Laravel\u306e\u30c7\u30fc\u30bf\u30d9\u30fc\u30b9\u7ba1\u7406\u306b\u306f\u59cb\u3081\u304b\u3089\u5f37\u529b\u306a\u30ea\u30ec\u30fc\u30b7\u30e7\u30f3\u30b7\u30c3\u30d7\u3092\u69cb\u7bc9\u3059\u308b\u6a5f\u80fd\u304c\u5099\u308f\u3063\u3066\u3044\u307e\u3059\u304c\u3001Many-to-Many(\u591a\u5bfe\u591a)\u306e\u95a2\u4fc2\u3092\u69cb\u7bc9\u3059\u308b\u5834\u5408\u306f\u3001\u3055\u3059\u304c\u306b\u5c11\u3057\u3060\u3051\u8907\u96d1\u306b\u306a\u3063\u3066\u3044\u307e\u3059\u3002<\/p>\n<p>\u79c1\u81ea\u8eab\u3082\u30ea\u30ec\u30fc\u30b7\u30e7\u30f3\u30b7\u30c3\u30d7\u306eDB\u69cb\u9020\u3092\u8003\u3048\u308b\u5834\u5408\u306b\u300c\u3042\u308c\u3001\u3069\u3046\u3060\u3063\u305f\u3063\u3051\uff1f\uff1f\u300d\u3068\u3044\u3046\u3053\u3068\u3082\u305f\u307e\u306b\u3042\u308b\u306e\u3067\u3001\u305b\u3063\u304b\u304f\u306a\u306e\u3067\u3053\u308c\u3092\u6a5f\u306b\u5b9f\u4f8b\u3068\u3057\u3066\u307e\u3068\u3081\u3066\u304a\u304f\u3053\u3068\u306b\u3057\u307e\u3057\u305f\u3002<\/p>\n<p>\u305c\u3072\u304a\u5f79\u306b\u305f\u3066\u305f\u3089\u5b09\u3057\u3044\u3067\u3059\uff01<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-418 aligncenter\" src=\"https:\/\/blog.capilano-fw.com\/wp-content\/uploads\/2018\/04\/35.png\" alt=\"\" width=\"370\" height=\"240\" srcset=\"https:\/\/blog.capilano-fw.com\/wp-content\/uploads\/2018\/04\/35.png 370w, https:\/\/blog.capilano-fw.com\/wp-content\/uploads\/2018\/04\/35-300x195.png 300w\" sizes=\"auto, (max-width: 370px) 100vw, 370px\" \/><\/p>\n<h1>\u30e2\u30c7\u30eb\u3092\u3064\u304f\u308b<\/h1>\n<p>\u307e\u305a\u306f\u3001\u30ea\u30ec\u30fc\u30b7\u30e7\u30f3\u30b7\u30c3\u30d7\u3092\u4f5c\u308a\u305f\u3044\uff12\u3064\u306e\u30e2\u30c7\u30eb\u3092\u4f5c\u308a\u307e\u3057\u3087\u3046\u3002<\/p>\n<p>\u4eca\u56de\u306e\u4f8b\u306f\u3001\u3042\u308b\u751f\u5f92\u30b0\u30eb\u30fc\u30d7\u306e\u8da3\u5473\u3092\u7ba1\u7406\u3059\u308b\u3001<\/p>\n<ol>\n<li><strong>Student\u3000\u30fb\u30fb\u30fb\u3000\u751f\u5f92<\/strong><\/li>\n<li><strong>Hobby\u3000\u30fb\u30fb\u30fb\u3000\u8da3\u5473<\/strong><\/li>\n<\/ol>\n<p>\u306e\u30c6\u30fc\u30d6\u30eb\u3067\u3084\u3063\u3066\u307f\u308b\u3053\u3068\u306b\u3057\u307e\u3059\u3002<\/p>\n<p>\u3067\u306f\u3001\u307e\u305a\u306fLaravel\u306eArtisan\u30b3\u30de\u30f3\u30c9\u3092\u4f7f\u3063\u3066\u30e2\u30c7\u30eb\u3092\u4f5c\u6210\u3057\u307e\u3057\u3087\u3046\u3002<\/p>\n<pre><strong>php artisan make:model Student -m<\/strong><\/pre>\n<pre><strong>php artisan make:model Hobby -m<\/strong><\/pre>\n<p>\u203b\u300c<strong>-m<\/strong>\u300d\u304c\u3064\u3051\u3066\u3044\u308b\u306e\u306f\u3001\u81ea\u52d5\u7684\u306b\u30de\u30a4\u30b0\u30ec\u30fc\u30b7\u30e7\u30f3\u3082\u4f5c\u6210\u3057\u3066\u304f\u308c\u308b\u304b\u3089\u3067\u3059\u3002\u30de\u30a4\u30b0\u30ec\u30fc\u30b7\u30e7\u30f3\u306f\u6b21\u306e\u9805\u76ee\u3067\u8aac\u660e\u3057\u307e\u3059\u3002<\/p>\n<p>\u6b21\u306b\u4f5c\u6210\u3057\u305f\u300cStudent\u300d\u3068\u300cHobby\u300d\u30e2\u30c7\u30eb\u5185\u306b\u30ea\u30ec\u30fc\u30b7\u30e7\u30f3\u30b7\u30c3\u30d7\u3092\u8a2d\u5b9a\u3057\u307e\u3059\u3002<\/p>\n<pre>&lt;?php\r\n\r\nnamespace App;\r\n\r\nuse Illuminate\\Database\\Eloquent\\Model;\r\n\r\nclass <strong>Student<\/strong> extends Model\r\n{\r\n    \/**\r\n     * The roles that belong to the user.\r\n     *\/\r\n<strong>    public function hobbies()<\/strong>\r\n<strong>    {<\/strong>\r\n<strong>        return $this-&gt;belongsToMany('App\\Hobby');<\/strong>\r\n<strong>    }<\/strong>\r\n}<\/pre>\n<pre>&lt;?php\r\n\r\nnamespace App;\r\n\r\nuse Illuminate\\Database\\Eloquent\\Model;\r\n\r\nclass <strong>Hobby<\/strong> extends Model\r\n{\r\n    \/**\r\n     * The roles that belong to the user.\r\n     *\/\r\n<strong>    public function students()<\/strong>\r\n<strong>    {<\/strong>\r\n<strong>        return $this-&gt;belongsToMany('App\\Student');<\/strong>\r\n<strong>    }<\/strong>\r\n}<\/pre>\n<h1>\u30de\u30a4\u30b0\u30ec\u30fc\u30b7\u30e7\u30f3\u3092\u3064\u304f\u308b<\/h1>\n<p>\u3055\u3063\u304d\u30e2\u30c7\u30eb\u3092\u4f5c\u3063\u305f\u3068\u304d\u306b\u5bfe\u5fdc\u3059\u308b\u30de\u30a4\u30b0\u30ec\u30fc\u30b7\u30e7\u30f3\u3082\u4e00\u7dd2\u306b\u4f5c\u6210\u3057\u305f\u306e\u3067\u3001\u65b0\u3057\u304f\u5fc5\u8981\u306a\u306e\u306f\u300cStudent\u300d\u3068\u300cHobby\u300d\u3092\u3064\u306a\u3050Pivot\u30c6\u30fc\u30d6\u30eb\u3060\u3051\u3067\u3059\u3002\u4ee5\u4e0b\u306e\u30b3\u30de\u30f3\u30c9\u3067\u4f5c\u6210\u3057\u307e\u3057\u3087\u3046\u3002<\/p>\n<pre><strong>php artisan make:migration create_student_hobbies_table<\/strong><\/pre>\n<p>\u3067\u306f\u3001\u5168\u90e8\u3067\uff13\u3064\u3042\u308b\u30de\u30a4\u30b0\u30ec\u30fc\u30b7\u30e7\u30f3\u306e\u4e2d\u8eab\u3092\u8a2d\u5b9a\u3057\u3066\u3044\u304d\u307e\u3059\u3002<\/p>\n<p>\u203b\u5197\u9577\u306b\u306a\u308b\u306e\u3067\u3001up()\u30e1\u30bd\u30c3\u30c9\u306e\u4e2d\u8eab\u3060\u3051\u3092\u8868\u793a\u3057\u3066\u3044\u307e\u3059\u3002\u307e\u305f\u3001\u30b5\u30f3\u30d7\u30eb\u306a\u306e\u3067\u8ffd\u52a0\u3057\u3066\u3044\u308b\u30d5\u30a3\u30fc\u30eb\u30c9\u306f\u300cname\u300d\u306e\u307f\u3067\u3059\u3002\u9069\u5b9c\u5fc5\u8981\u306a\u30d5\u30a3\u30fc\u30eb\u30c9\u3092\u8ffd\u52a0\u3057\u3066\u304f\u3060\u3055\u3044\u3002<\/p>\n<p><strong>\uff11\uff0estudents<\/strong><\/p>\n<pre>Schema::create('<strong>students<\/strong>', function (Blueprint $table) {\r\n    $table-&gt;increments('id');\r\n    <strong>$table-&gt;string('name');<\/strong>\r\n    $table-&gt;timestamps();\r\n});<\/pre>\n<p><strong>\uff12\uff0ehobbies<\/strong><\/p>\n<pre>Schema::create('<strong>hobbies<\/strong>', function (Blueprint $table) {\r\n    $table-&gt;increments('id');\r\n    <strong>$table-&gt;string('name');<\/strong>\r\n    $table-&gt;timestamps();\r\n});<\/pre>\n<p><strong>\uff13\uff0ehobby_student\uff08Pivot\u30c6\u30fc\u30d6\u30eb\uff09<\/strong><\/p>\n<pre>Schema::create('<strong>hobby_student<\/strong>', function (Blueprint $table) {\r\n    $table-&gt;unsignedInteger('<strong>student_id<\/strong>')-&gt;index();\r\n    $table-&gt;foreign('<strong>student_id<\/strong>')-&gt;references('id')-&gt;on('students')-&gt;onDelete('cascade');\r\n    $table-&gt;unsignedInteger('<strong>hobby_id<\/strong>')-&gt;index();\r\n    $table-&gt;foreign('<strong>hobby_id<\/strong>')-&gt;references('id')-&gt;on('hobbies')-&gt;onDelete('cascade');\r\n});<\/pre>\n<p>\u3053\u3053\u3067\u6ce8\u610f\u304c\u5fc5\u8981\u306a\u306e\u306f\u3001<strong>onDelete()<\/strong>\u3067\u3059\u3002<br \/>\n\u3053\u308c\u306f\u3001<\/p>\n<ul>\n<li>\u3082\u3057<strong>Student<\/strong>\u30c7\u30fc\u30bf\u304c\u524a\u9664\u3055\u308c\u305f\u3089\u81ea\u52d5\u3067\u8a72\u5f53\u30c7\u30fc\u30bf\u3092\u524a\u9664<\/li>\n<li>\u3082\u3057<strong>Hobby<\/strong>\u30c7\u30fc\u30bf\u304c\u524a\u9664\u3055\u308c\u305f\u3089\u81ea\u52d5\u3067\u8a72\u5f53\u30c7\u30fc\u30bf\u3092\u524a\u9664<\/li>\n<\/ul>\n<p>\u3068\u3044\u3046\u610f\u5473\u3067\u3059\u3002<\/p>\n<p>\u7c21\u5358\u306b\u8a00\u3046\u3068\u3001\u300c\u3042\u3001\u305d\u3063\u3061\u306e\u30c7\u30fc\u30bf\u3092\u524a\u9664\u3059\u308b\u3093\u3060\u3063\u305f\u3089\u3001\u3082\u3046\u3053\u3063\u3061\u3082\u3044\u3089\u306a\u3044\u3088\u306d\u3002\u306f\u3044\u3001\u524a\u9664\u301c\uff01\u300d\u3063\u3066\u3053\u3068\u3067\u3059\u3002<\/p>\n<h1>\u30c6\u30b9\u30c8\u30c7\u30fc\u30bf\u3092\u3064\u304f\u308b<\/h1>\n<p>\u30c6\u30b9\u30c8\u3092\u5b9f\u884c\u3059\u308b\u305f\u3081\u306b\u3001\u300cStudent\u300d\u3068\u300cHobby\u300d\u30c6\u30fc\u30d6\u30eb\u306b\u3042\u3089\u304b\u3058\u3081\u4ee5\u4e0b\u306e\u30c7\u30fc\u30bf\u3092\u8ffd\u52a0\u3057\u3066\u304a\u304d\u307e\u3057\u3087\u3046\u3002<\/p>\n<p><b>\uff08<strong>students\u30c6\u30fc\u30d6\u30eb<\/strong>\uff09<\/b><\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-514\" src=\"https:\/\/blog.capilano-fw.com\/wp-content\/uploads\/2018\/05\/students_table.png\" alt=\"\" width=\"445\" height=\"120\" srcset=\"https:\/\/blog.capilano-fw.com\/wp-content\/uploads\/2018\/05\/students_table.png 445w, https:\/\/blog.capilano-fw.com\/wp-content\/uploads\/2018\/05\/students_table-300x81.png 300w\" sizes=\"auto, (max-width: 445px) 100vw, 445px\" \/><\/p>\n<p><b>\uff08<strong>hobbies\u30c6\u30fc\u30d6\u30eb<\/strong>\uff09<\/b><\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-515\" src=\"https:\/\/blog.capilano-fw.com\/wp-content\/uploads\/2018\/05\/hobies_table.png\" alt=\"\" width=\"433\" height=\"124\" srcset=\"https:\/\/blog.capilano-fw.com\/wp-content\/uploads\/2018\/05\/hobies_table.png 433w, https:\/\/blog.capilano-fw.com\/wp-content\/uploads\/2018\/05\/hobies_table-300x86.png 300w\" sizes=\"auto, (max-width: 433px) 100vw, 433px\" \/><\/p>\n<p>\u203b\u3072\u3068\u3064\u305a\u3064\u30c6\u30fc\u30d6\u30eb\u306b\u8ffd\u52a0\u3057\u3066\u3082\u3044\u3044\u3067\u3059\u3051\u3069\u3001\u3081\u3093\u3069\u3046\u306a\u5834\u5408\u306f<a href=\"https:\/\/laravel.com\/docs\/5.6\/seeding#writing-seeders\" target=\"_blank\" rel=\"noopener\">Seeding\u6a5f\u80fd<\/a>\u3067\u4e00\u6c17\u306b\u8ffd\u52a0\u3057\u3066\u3082\u3044\u3044\u3067\u3057\u3087\u3046\u3002<\/p>\n<h1>\u30c6\u30b9\u30c8\u3057\u3066\u307f\u308b<\/h1>\n<p>\u3067\u306f\u3001\u300c\u6843\u592a\u90ce\u300d\u306e\u8da3\u5473\u304c\u300c\u8aad\u66f8\u300d\uff06\u300c\u6599\u7406\u300d\u306b\u306a\u308b\u3088\u3046\u306b\u30c7\u30fc\u30bf\u3092\u4f5c\u6210\u3057\u3066\u307f\u307e\u3057\u3087\u3046\u3002<\/p>\n<pre>$student = \\App\\Student::find(1);   \/\/ \u6843\u592a\u90ce\r\n<strong>$student-&gt;hobbies()-&gt;sync([1, 2]);<\/strong>  \/\/ \u8aad\u66f8\u3001\u6599\u7406<\/pre>\n<p>\u53d6\u5f97\u3057\u305fStudent\u30c7\u30fc\u30bf\u306b\u300c\u30e2\u30c7\u30eb\u3092\u3064\u304f\u308b\u300d\u3067\u4f5c\u6210\u3057\u305f<strong>hobbies()<\/strong>\u3092\u547c\u3073\u51fa\u3057\u3001\u305d\u3053\u306b<strong>sync()<\/strong>\u3092\u4f7f\u3063\u3066\u30c7\u30fc\u30bf\u3092\u4fdd\u5b58\u3057\u3066\u3044\u307e\u3059\u3002<\/p>\n<p>\u3053\u308c\u3067\u3001\u30c6\u30fc\u30d6\u30eb\u306e\u4e2d\u8eab\u306f\u3053\u3046\u306a\u308a\u307e\u3057\u305f\u3002<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-513\" src=\"https:\/\/blog.capilano-fw.com\/wp-content\/uploads\/2018\/05\/hobby_student_1.png\" alt=\"\" width=\"252\" height=\"93\" \/><\/p>\n<p>\u3082\u3057\u3001\u3053\u306e\u5f8c\u5909\u66f4\u3092\u3057\u305f\u3044\u5834\u5408\u306f\u540c\u3058\u3088\u3046\u306b\u3059\u308c\u3070OK\u3067\u3059\u3002<\/p>\n<pre><strong>$student-&gt;hobbies()-&gt;sync([2, 3]);<\/strong>  \/\/ \u6599\u7406\u3001\u65c5\u884c<\/pre>\n<p>\u305d\u306e\u5834\u5408\u306f\u3001\u30c6\u30fc\u30d6\u30eb\u306f\u3053\u3046\u5909\u66f4\u3055\u308c\u307e\u3059\u3002<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-512\" src=\"https:\/\/blog.capilano-fw.com\/wp-content\/uploads\/2018\/05\/hobby_student_2.png\" alt=\"\" width=\"249\" height=\"93\" \/><\/p>\n<p><strong>\u203b\u3064\u307e\u308a\u3001ID\u304c\uff11\u306e\u300c\u8aad\u66f8\u300d\u306f\u524a\u9664\u3055\u308c\u3066\u3001\u300c\u6599\u7406\u300d\u3068\u300c\u65c5\u884c\u300d\u306e\uff12\u3064\u304c\u6843\u592a\u90ce\u306e\u8da3\u5473\u306b\u306a\u308a\u307e\u3059\u3002<\/strong><\/p>\n<p>\u3067\u306f\u3001\u30ea\u30ec\u30fc\u30b7\u30e7\u30f3\u30b7\u30c3\u30d7\u3092\u542b\u3081\u305fStudent\u30c6\u30fc\u30d6\u30eb\u306e\u5168\u4f53\u304c\u4eca\u3069\u3046\u306a\u3063\u3066\u3044\u308b\u304b\u898b\u3066\u307f\u307e\u3057\u3087\u3046\u3002<\/p>\n<pre>$students = \\App\\<strong>Student::with('hobbies')<\/strong>-&gt;get();\r\nprint_r($students-&gt;toArray());<\/pre>\n<p>\u7d50\u679c\u306f\u3053\u3046\u3067\u3059\u3002<\/p>\n<pre>Array\r\n(\r\n    [0] =&gt; Array\r\n        (\r\n            [id] =&gt; 1\r\n            [name] =&gt; \u6843\u592a\u90ce\r\n            [created_at] =&gt; 2018-05-20 19:24:25\r\n            [updated_at] =&gt; 2018-05-20 19:24:25\r\n            <strong>[hobbies] =&gt; Array\r\n                (\r\n                    [0] =&gt; Array\r\n                        (\r\n                            [id] =&gt; 2\r\n                            [name] =&gt; \u6599\u7406\r\n                            [created_at] =&gt; 2018-05-20 19:24:25\r\n                            [updated_at] =&gt; 2018-05-20 19:24:25\r\n                            [pivot] =&gt; Array\r\n                                (\r\n                                    [student_id] =&gt; 1\r\n                                    [hobby_id] =&gt; 2\r\n                                )\r\n\r\n                        )\r\n\r\n                    [1] =&gt; Array\r\n                        (\r\n                            [id] =&gt; 3\r\n                            [name] =&gt; \u65c5\u884c\r\n                            [created_at] =&gt; 2018-05-20 19:24:25\r\n                            [updated_at] =&gt; 2018-05-20 19:24:25\r\n                            [pivot] =&gt; Array\r\n                                (\r\n                                    [student_id] =&gt; 1\r\n                                    [hobby_id] =&gt; 3\r\n                                )\r\n\r\n                        )\r\n\r\n                )<\/strong>\r\n\r\n        )\r\n\r\n    [1] =&gt; Array\r\n        (\r\n            [id] =&gt; 2\r\n            [name] =&gt; \u91d1\u592a\u90ce\r\n            [created_at] =&gt; 2018-05-20 19:24:25\r\n            [updated_at] =&gt; 2018-05-20 19:24:25\r\n            [hobbies] =&gt; Array\r\n                (\r\n                )\r\n\r\n        )\r\n\r\n    [2] =&gt; Array\r\n        (\r\n            [id] =&gt; 3\r\n            [name] =&gt; \u304b\u3050\u3084\u59eb\r\n            [created_at] =&gt; 2018-05-20 19:24:25\r\n            [updated_at] =&gt; 2018-05-20 19:24:25\r\n            [hobbies] =&gt; Array\r\n                (\r\n                )\r\n\r\n        )\r\n\r\n)<\/pre>\n<p>\u6700\u5f8c\u306b\u8a2d\u5b9a\u3057\u305f\u300c\u6599\u7406\u300d\u300c\u65c5\u884c\u300d\u304c\u6843\u592a\u90ce\u306e\u30c7\u30fc\u30bf\u5185\u306b\u5165\u3063\u3066\u3044\u307e\u3059\u306d\u3002<\/p>\n<p>\u3061\u306a\u307f\u306b\u3001\u3053\u308c\u3060\u3051\u3060\u3068One-to-Many\u3068\u307b\u307c\u540c\u3058\u3067\u3059\u304c\u3001Many-to-Many\u306f\u9006\u5074\u3001\u3064\u307e\u308aHobby\u5074\u304b\u3089\u547c\u3073\u51fa\u3057\u3066\u3082\u30ea\u30ec\u30fc\u30b7\u30e7\u30f3\u30b7\u30c3\u30d7\u304c\u53d6\u5f97\u3067\u304d\u307e\u3059\u3002<\/p>\n<p>\u6b21\u306e\u3088\u3046\u306b\u3057\u3066\u3001Hobby\u30c6\u30fc\u30d6\u30eb\u5074\u304b\u3089\u3082\u540c\u3058\u3088\u3046\u306b\u898b\u3066\u307f\u307e\u3057\u3087\u3046\u3002<\/p>\n<pre>$hobbies = <strong>\\App\\Hobby::with('students')<\/strong>-&gt;get();\r\nprint_r($hobbies-&gt;toArray());<\/pre>\n<p>\u4e2d\u8eab\u306f\u3053\u308c\u3067\u3059\u3002<\/p>\n<pre>Array\r\n(\r\n    [0] =&gt; Array\r\n        (\r\n            [id] =&gt; 1\r\n            [name] =&gt; \u8aad\u66f8\r\n            [created_at] =&gt; 2018-05-20 19:24:25\r\n            [updated_at] =&gt; 2018-05-20 19:24:25\r\n            [students] =&gt; Array\r\n                (\r\n                )\r\n\r\n        )\r\n\r\n    [1] =&gt; Array\r\n        (\r\n            [id] =&gt; 2\r\n            [name] =&gt; \u6599\u7406\r\n            [created_at] =&gt; 2018-05-20 19:24:25\r\n            [updated_at] =&gt; 2018-05-20 19:24:25\r\n            <strong>[students] =&gt; Array\r\n                (\r\n                    [0] =&gt; Array\r\n                        (\r\n                            [id] =&gt; 1\r\n                            [name] =&gt; \u6843\u592a\u90ce\r\n                            [created_at] =&gt; 2018-05-20 19:24:25\r\n                            [updated_at] =&gt; 2018-05-20 19:24:25\r\n                            [pivot] =&gt; Array\r\n                                (\r\n                                    [hobby_id] =&gt; 2\r\n                                    [student_id] =&gt; 1\r\n                                )\r\n\r\n                        )\r\n\r\n                )\r\n<\/strong>\r\n        )\r\n\r\n    [2] =&gt; Array\r\n        (\r\n            [id] =&gt; 3\r\n            [name] =&gt; \u65c5\u884c\r\n            [created_at] =&gt; 2018-05-20 19:24:25\r\n            [updated_at] =&gt; 2018-05-20 19:24:25\r\n            <strong>[students] =&gt; Array\r\n                (\r\n                    [0] =&gt; Array\r\n                        (\r\n                            [id] =&gt; 1\r\n                            [name] =&gt; \u6843\u592a\u90ce\r\n                            [created_at] =&gt; 2018-05-20 19:24:25\r\n                            [updated_at] =&gt; 2018-05-20 19:24:25\r\n                            [pivot] =&gt; Array\r\n                                (\r\n                                    [hobby_id] =&gt; 3\r\n                                    [student_id] =&gt; 1\r\n                                )\r\n\r\n                        )\r\n\r\n                )<\/strong>\r\n\r\n        )\r\n\r\n)<\/pre>\n<p>\u4eca\u5ea6\u306f\u3001Hobby\u5074\u304b\u3089\u898b\u305f\u30c7\u30fc\u30bf\u306a\u306e\u3067\u3001\u4e2d\u8eab\u306f\u300c\u6599\u7406\u300d\u306e\u4e2d\u306b\u6843\u592a\u90ce\u3001\u305d\u3057\u3066\u300c\u65c5\u884c\u300d\u306e\u4e2d\u306b\u3082\u6843\u592a\u90ce\u306e\u30c7\u30fc\u30bf\u304c\u5b58\u5728\u3057\u3066\u3044\u307e\u3059\u3002<\/p>\n<p>\u3082\u3061\u308d\u3093\u3001\u30c7\u30fc\u30bf\u8ffd\u52a0\u3082Hobby\u5074\u304b\u3089\u3067\u304d\u307e\u3059\u3002\u3084\u308a\u65b9\u306f\u3001\u3055\u3063\u304d\u898b\u305fStudent\u5074\u306e\u300c\u9006\u30d0\u30fc\u30b8\u30e7\u30f3\u300d\u3067\u3059\u3002<\/p>\n<pre>$hobby = \\App\\<strong>Hobby<\/strong>::find(1);       \/\/ \u8aad\u66f8\r\n<strong>$hobby-&gt;students()-&gt;sync([2, 3]);<\/strong>   \/\/ \u91d1\u592a\u90ce\u3001\u304b\u3050\u3084\u59eb<\/pre>\n<p>\u73fe\u5728\u306e\u30c7\u30fc\u30bf\u3092\u56f3\u306b\u3057\u3066\u307f\u308b\u3068\u3001\u3053\u3046\u306a\u308a\u307e\u3059\u3002<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-517\" src=\"https:\/\/blog.capilano-fw.com\/wp-content\/uploads\/2018\/05\/many_to_many_table.png\" alt=\"\" width=\"479\" height=\"246\" srcset=\"https:\/\/blog.capilano-fw.com\/wp-content\/uploads\/2018\/05\/many_to_many_table.png 479w, https:\/\/blog.capilano-fw.com\/wp-content\/uploads\/2018\/05\/many_to_many_table-300x154.png 300w\" sizes=\"auto, (max-width: 479px) 100vw, 479px\" \/><\/p>\n<h1>\u304a\u308f\u308a\u306b<\/h1>\n<p>\u3053\u3046\u3084\u3063\u3066Many-toMany\u3067\u30ea\u30ec\u30fc\u30b7\u30e7\u30f3\u30b7\u30c3\u30d7\u3092\u4f5c\u3063\u3066\u304a\u304f\u3068\u3001\u3069\u3061\u3089\u5074\u304b\u3089\u3067\u3082\u30c7\u30fc\u30bf\u3092\u547c\u3073\u51fa\u305b\u3066\u4fbf\u5229\u3067\u3059\u306d\u3002<\/p>\n<p>\u3068\u3044\u3046\u3053\u3068\u3067\u3001\u4eca\u56de\u306fMany-to-Many\u306e\u30ea\u30ec\u30fc\u30b7\u30e7\u30f3\u30b7\u30c3\u30d7\u3092\u7d39\u4ecb\u3057\u307e\u3057\u305f\u3002<\/p>\n<p>\u305c\u3072\u958b\u767a\u306b\u3054\u6d3b\u7528\u304f\u3060\u3055\u3044\u306d\uff01<\/p>\n<p>\u3067\u306f\u3067\u306f\u301c\u3002<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-519 aligncenter\" src=\"https:\/\/blog.capilano-fw.com\/wp-content\/uploads\/2018\/05\/21.png\" alt=\"\" width=\"192\" height=\"320\" srcset=\"https:\/\/blog.capilano-fw.com\/wp-content\/uploads\/2018\/05\/21.png 192w, https:\/\/blog.capilano-fw.com\/wp-content\/uploads\/2018\/05\/21-180x300.png 180w\" sizes=\"auto, (max-width: 192px) 100vw, 192px\" \/><\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u3055\u3066\u3055\u3066\u3001Laravel\u306e\u30c7\u30fc\u30bf\u30d9\u30fc\u30b9\u7ba1\u7406\u306b\u306f\u59cb\u3081\u304b\u3089\u5f37\u529b\u306a\u30ea\u30ec\u30fc\u30b7\u30e7\u30f3\u30b7\u30c3\u30d7\u3092\u69cb\u7bc9\u3059\u308b\u6a5f\u80fd\u304c\u5099\u308f\u3063\u3066\u3044\u307e\u3059\u304c\u3001Many-to-Many(\u591a\u5bfe\u591a)\u306e\u95a2\u4fc2\u3092\u69cb\u7bc9\u3059 &hellip; <\/p>\n<p class=\"link-more\"><a href=\"https:\/\/blog.capilano-fw.com\/?p=510\" class=\"more-link\"><span class=\"screen-reader-text\">&#8220;Laravel\u306eMany-to-Many(\u591a\u5bfe\u591a)\u30c6\u30fc\u30d6\u30eb\u5b9f\u4f8b&#8221; \u306e<\/span>\u7d9a\u304d\u3092\u8aad\u3080<\/a><\/p>\n","protected":false},"author":1,"featured_media":521,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3],"tags":[67],"class_list":["post-510","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-laravel","tag-67"],"_links":{"self":[{"href":"https:\/\/blog.capilano-fw.com\/index.php?rest_route=\/wp\/v2\/posts\/510","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.capilano-fw.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.capilano-fw.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.capilano-fw.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.capilano-fw.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=510"}],"version-history":[{"count":5,"href":"https:\/\/blog.capilano-fw.com\/index.php?rest_route=\/wp\/v2\/posts\/510\/revisions"}],"predecessor-version":[{"id":522,"href":"https:\/\/blog.capilano-fw.com\/index.php?rest_route=\/wp\/v2\/posts\/510\/revisions\/522"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/blog.capilano-fw.com\/index.php?rest_route=\/wp\/v2\/media\/521"}],"wp:attachment":[{"href":"https:\/\/blog.capilano-fw.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=510"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.capilano-fw.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=510"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.capilano-fw.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=510"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}