{"id":4748,"date":"2019-10-16T05:09:19","date_gmt":"2019-10-15T20:09:19","guid":{"rendered":"https:\/\/blog.capilano-fw.com\/?p=4748"},"modified":"2019-10-17T01:01:44","modified_gmt":"2019-10-16T16:01:44","slug":"laravel%e3%81%a7%e8%a4%87%e9%9b%91%e3%81%aa%e3%83%aa%e3%83%ac%e3%83%bc%e3%82%b7%e3%83%a7%e3%83%b3%e3%82%b7%e3%83%83%e3%83%97%e3%81%ae%e6%a4%9c%e7%b4%a2%e3%82%92%e3%82%b7%e3%83%b3%e3%83%97%e3%83%ab","status":"publish","type":"post","link":"https:\/\/blog.capilano-fw.com\/?p=4748","title":{"rendered":"Laravel\u3067\u8907\u96d1\u306a\u30ea\u30ec\u30fc\u30b7\u30e7\u30f3\u30b7\u30c3\u30d7\u306e\u691c\u7d22\u3092\u30b7\u30f3\u30d7\u30eb\u306b\u3059\u308b"},"content":{"rendered":"<p>\u3055\u3066\u3055\u3066\u3001\u79c1\u306f<code>Laravel<\/code>\u3092\u4f7f\u3063\u305f\u30a6\u30a7\u30d6\u958b\u767a\u3092\u306f\u3058\u3081\u3066\u304b\u3089\uff15\u5e74\u4ee5\u4e0a\u306e\u6642\u9593\u304c\u7d4c\u3063\u3066\u3044\u307e\u3059\u3002<\/p>\n<p>\u305d\u3057\u3066\u3001\u3042\u308a\u304c\u305f\u3044\u3053\u3068\u306b\u57fa\u672c\u7684\u306b\u4f55\u304b\u65b0\u3057\u3044\u6a5f\u80fd\u3092\u958b\u767a\u3059\u308b\u5834\u5408\u3067\u3082\u300c\u3046\u30fc\u3093\u3001\u30b3\u30ec\u306f\u3069\u3046\u3084\u3063\u305f\u3089\u5b9f\u73fe\u3067\u304d\u308b\u306e\u304b\u308f\u304b\u3089\u306a\u3044\u305e\u30fb\u30fb\u30fb\ud83d\udca6\u3068\u306f\u300d\u307f\u305f\u3044\u306a\u3053\u3068\u3063\u3066\u5c11\u306a\u304f\u306a\u3063\u3066\u304d\u307e\u3057\u305f\uff08\u30db\u30f3\u30c8\u3001<code>Stack Overflow<\/code>\u3084\u5148\u4eba\u306e\u516c\u958b\u3057\u305f\u60c5\u5831\u306e\u304a\u304b\u3052\u3067\u3059\u3002\u611f\u8b1d\u3067\u3059\ud83d\ude0a\u2728\uff09<\/p>\n<p>\u305f\u3060\u3001\u305d\u3093\u306a\u72b6\u6cc1\u3067\u3042\u3063\u3066\u3082\u3053\u306e\u9593\u3001\u300c\u3046\u30fc\u30fc\u30fc\u30fc\u30fc\u3093\u30fb\u30fb\u30fb\u300d\u304c\u767a\u751f\u3057\u305f\u3053\u3068\u304c\u3042\u308a\u307e\u3057\u305f\u3002<\/p>\n<p>\u305d\u308c\u306f\u3001\u4f8b\u3048\u3070\u3001\u4ee5\u4e0b\u306e\u3088\u3046\u306a\u30c7\u30fc\u30bf\u30d9\u30fc\u30b9\u306e\u691c\u7d22\u3067\u3059\u3002<\/p>\n<ul>\n<li>hasMany\u3067\u7d50\u5408\u3057\u305f\u5b50\u30c6\u30fc\u30d6\u30eb\u306b\u306f\u30c7\u30fc\u30bf\u304c\u5165\u3063\u3066\u3044\u306a\u3044\u3082\u306e\uff08\u305f\u3060\u3057\u3001\u30d0\u30ca\u30ca\u306f\u30ab\u30a6\u30f3\u30c8\u304b\u3089\u9664\u5916\u3059\u308b\uff09\u3092\u53d6\u5f97\u3059\u308b<\/li>\n<li>hasMany\u3057\u305f\u5b50\u30c6\u30fc\u30d6\u30eb\u306e\u300cname\u300d\u304c\u300c\u30a4\u30c1\u30b4\u300d\u3060\u3051\u306e\u3082\u306e\uff08\u305f\u3060\u3057\u3001\u30d1\u30a4\u30ca\u30c3\u30d7\u30eb\u306f\u30ab\u30a6\u30f3\u30c8\u304b\u3089\u9664\u5916\u3059\u308b\uff09\u3092\u53d6\u5f97\u3059\u308b<\/li>\n<\/ul>\n<p>\u5f53\u521d\u306f\u3001<code>whereHas('\u30ea\u30ec\u30fc\u30b7\u30e7\u30f3\u30b7\u30c3\u30d7\u540d', function(){ })<\/code>\u3092\u4f7f\u3063\u3066\u30ea\u30ec\u30fc\u30b7\u30e7\u30f3\u30b7\u30c3\u30d7\u5148\u306e\u691c\u7d22\u3092\u8a66\u3057\u3066\u307f\u307e\u3057\u305f\u304c\u3001\u6700\u7d42\u7684\u306b\u3046\u307e\u304f\u3044\u304d\u307e\u305b\u3093\u3067\u3057\u305f\u3002\uff08\u3082\u3057\u304b\u3057\u305f\u3089\u9054\u4eba\u306b\u304b\u304b\u308c\u3070\u30b5\u30af\u30c3\u3068\u81ea\u524d\u306eSQL\u3092\u4f5c\u3063\u3061\u3083\u3046\u304b\u3082\u3067\u3059\u304c\u30fb\u30fb\u30fb\uff09<\/p>\n<p>\u3055\u3089\u306b\u691c\u7d22\u306a\u306e\u3067\u3001\u3067\u304d\u308b\u3060\u3051\u9ad8\u901f\u306b<code>SQL<\/code>\u304c\u5b9f\u884c\u3067\u304d\u308b\u3088\u3046\u306b\u3057\u305f\u3044\u3053\u3068\u3082\u3042\u3063\u3066<code>whereHas<\/code>\u306f\u3042\u307e\u308a\u73fe\u5b9f\u7684\u3067\u306f\u306a\u3044\u3088\u3046\u306a\u6c17\u306b\u306a\u3063\u3066\u3044\u307e\u3057\u305f\u30fb\u30fb\u30fb<\/p>\n<p>\u305d\u3053\u3067\uff01<\/p>\n<p>\u4eca\u56de\u306f\u3053\u306e\u3088\u3046\u306a\u30ea\u30ec\u30fc\u30b7\u30e7\u30f3\u30b7\u30c3\u30d7\u5148\u306e\u8907\u96d1\u306a\u6761\u4ef6\u3092\u3069\u306e\u3088\u3046\u306b\u5b9f\u73fe\u3057\u305f\u304b\u3092\u3054\u7d39\u4ecb\u3057\u305f\u3044\u3068\u601d\u3044\u307e\u3059\u3002<\/p>\n<p>\u305c\u3072\u7686\u3055\u3093\u306e\u304a\u5f79\u306b\u7acb\u3066\u308b\u3068\u5b09\u3057\u3044\u3067\u3059\ud83d\ude0a\u2728<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-3566 aligncenter\" src=\"https:\/\/blog.capilano-fw.com\/wp-content\/uploads\/2019\/04\/no_1.png\" alt=\"\" width=\"306\" height=\"320\" srcset=\"https:\/\/blog.capilano-fw.com\/wp-content\/uploads\/2019\/04\/no_1.png 306w, https:\/\/blog.capilano-fw.com\/wp-content\/uploads\/2019\/04\/no_1-287x300.png 287w\" sizes=\"auto, (max-width: 306px) 100vw, 306px\" \/><\/p>\n<p><strong>\u958b\u767a\u74b0\u5883\uff1a<\/strong> Laravel 6.x<\/p>\n<h1>\u3069\u306e\u3088\u3046\u306b\u3053\u306e\u554f\u984c\u3092\u89e3\u6c7a\u3057\u305f\u304b\uff1f<\/h1>\n<h2>SQL\u3060\u3051\u306e\u3084\u308a\u65b9\u306f\u3042\u304d\u3089\u3081\u305f<\/h2>\n<p>\u307e\u305a\u5148\u306b\u884c\u3063\u3066\u3057\u307e\u3046\u3068\u3001SQL\u3060\u3051\u3067\u306e\u5b9f\u73fe\u306f\u8ae6\u3081\u307e\u3057\u305f\u3002<\/p>\n<p>\u305d\u3057\u3066\u3001\u5225\u306e\u30a2\u30d7\u30ed\u30fc\u30c1\u306e\u65b9\u6cd5\u3092\u8003\u3048\u3066\u3044\u305f\u306e\u3067\u3059\u304c\u3001\u305d\u306e\u6642\u306b\u5f79\u306b\u7acb\u3063\u305f\u306e\u304c<strong>\u300c\u6642\u9593\u304c\u304b\u304b\u308b\u3053\u3068\u306f\u5148\u306b\u3084\u3063\u3068\u3053\u3046\u300d<\/strong>\u7cbe\u795e\u3067\u3057\u305f\u3002<\/p>\n<p>\u3064\u307e\u308a\u3001\u8907\u96d1\u306a\u691c\u7d22\u90e8\u5206\u306f\u5148\u306b\u96c6\u8a08\u3057\u3066\u304a\u3044\u3066\u4e8b\u524d\u306b\u89aa\u30c6\u30fc\u30d6\u30eb\u306b<code>status<\/code>\u3068\u3057\u3066\u4fdd\u5b58\u3057\u3066\u304a\u304f\u3053\u3068\u306b\u3057\u305f\u306e\u3067\u3059\u3002<\/p>\n<p>\u4f8b\u3048\u3070\u3001\u7d75\u3092\u4f7f\u3063\u3066\u898b\u3066\u307f\u307e\u3057\u3087\u3046\u3002<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-4749\" src=\"https:\/\/blog.capilano-fw.com\/wp-content\/uploads\/2019\/10\/complicated_has_many_1.png\" alt=\"\" width=\"640\" height=\"234\" srcset=\"https:\/\/blog.capilano-fw.com\/wp-content\/uploads\/2019\/10\/complicated_has_many_1.png 640w, https:\/\/blog.capilano-fw.com\/wp-content\/uploads\/2019\/10\/complicated_has_many_1-300x110.png 300w\" sizes=\"auto, (max-width: 640px) 100vw, 640px\" \/><\/p>\n<p>\u203b \u3053\u306e\u4f8b\u3067\u306f\u3001\u7bb1\u304c\u89aa\u30c6\u30fc\u30d6\u30eb\u3067\u3001\u305d\u306e\u4e0b\u306e\u30d5\u30eb\u30fc\u30c4\u304c<code>hasMany<\/code>\u3057\u305f\u5b50\u30c6\u30fc\u30d6\u30eb\u3067\u3059\u3002<\/p>\n<p>\u3053\u306e\u5834\u5408\u3001ID\u304c<code>1<\/code>\u306e\u7bb1\u306f\u3001\u30d0\u30ca\u30ca\u3057\u304b\u5165\u3063\u3066\u3044\u306a\u3044\u306e\u3067\u5192\u982d\u3067\u7d39\u4ecb\u3057\u305f\u6761\u4ef6<strong>\u300c\u30c7\u30fc\u30bf\u304c\u5165\u3063\u3066\u3044\u306a\u3044\u3082\u306e\uff08\u305f\u3060\u3057\u3001\u30d0\u30ca\u30ca\u306f\u30ab\u30a6\u30f3\u30c8\u304b\u3089\u9664\u5916\u3059\u308b\uff09\u300d<\/strong>\u306b\u5f53\u3066\u306f\u307e\u308a\u307e\u3059\u3002<\/p>\n<p>\u3053\u308c\u3092\u30d7\u30ed\u30b0\u30e9\u30e0\u3092\u4f7f\u3063\u3066\u4e8b\u524d\u306b\u30c1\u30a7\u30c3\u30af\u3057\u3066\u304a\u3044\u3066\u3001\u4f8b\u3048\u3070<code>no_data<\/code>\u306e\u3088\u3046\u306a\u30b9\u30c6\u30fc\u30bf\u30b9\u3092\u89aa\u30c6\u30fc\u30d6\u30eb\u306b\u4fdd\u5b58\u3057\u3066\u304a\u3051\u3070\u3001\uff11\u3064\u306e\u30c6\u30fc\u30d6\u30eb\u3092\u30c1\u30a7\u30c3\u30af\u3059\u308b\u3060\u3051\u3067\u8907\u96d1\u306a\u691c\u7d22\u304c\u5b9f\u73fe\u3067\u304d\u307e\u3059\u3057\u3001\u3055\u3089\u306b\u3001\u3088\u308a\u9ad8\u901f\u306a\u691c\u7d22\u3092\u5b9f\u73fe\u3059\u308b\u3053\u3068\u304c\u3067\u304d\u308b\u3068\u3044\u3046\u4ed5\u7d44\u307f\u3067\u3059\u3002<\/p>\n<h2>\u3069\u306e\u30bf\u30a4\u30df\u30f3\u30b0\u3067\u5b50\u30c6\u30fc\u30d6\u30eb\u3092\u96c6\u8a08\u3059\u308b\u306e\u304b\uff1f<\/h2>\n<p>\u305d\u3057\u3066\u3001\u6b21\u306b\u8003\u3048\u305f\u306e\u306f\u3001\u300c\u305d\u306e\u5b50\u30c6\u30fc\u30d6\u30eb\u306e\u96c6\u8a08\u300d\u3092\u3044\u3064\u3059\u308c\u3070\u3044\u3044\u306e\u304b\u3068\u3044\u3046\u3053\u3068\u3067\u3057\u305f\u3002<\/p>\n<p>\u7b54\u3048\u306f\u3001\u30ba\u30d0\u30ea<strong>\u300c\u5b50\u30c6\u30fc\u30d6\u30eb\u306b\u5909\u66f4\u304c\u3042\u3063\u305f\u3068\u304d\u300d<\/strong>\u3067\u3059\u3002<\/p>\n<p>\u4f8b\u3048\u3070\u3001\u3044\u307e\u65b0\u3057\u304f\u30a4\u30c1\u30b4\ud83c\udf53\u306e\u30c7\u30fc\u30bf\u3092\u8ffd\u52a0\u3059\u308b\u3068\u3057\u307e\u3059\u3002<\/p>\n<p>\u5b50\u30c6\u30fc\u30d6\u30eb\u306f<code>hasMany<\/code>\u3067\u7d50\u5408\u3055\u308c\u308b\u306e\u3067\u5fc5\u305a\u89aa\u30c6\u30fc\u30d6\u30eb\u306eID\u3092\u6301\u3063\u3066\u3044\u308b\u306f\u305a\u3067\u3059\u3002\u305d\u3057\u3066\u3001\u3053\u306e<strong>\u89aaID\u3092\u6301\u3063\u3066\u3044\u308b\u5b50\u30c6\u30fc\u30d6\u30eb\u306e\u30c7\u30fc\u30bf\u5168\u3066<\/strong>\u3092\u53d6\u308a\u51fa\u3057\u3066\u3001\u305d\u306e\u72b6\u614b\u304c\u3069\u3046\u306a\u3063\u3066\u3044\u308b\u304b\u3092\u30c1\u30a7\u30c3\u30af\u3057\u3066\u304a\u3051\u3070\u3044\u3044\u308f\u3051\u3067\u3059\u3002<\/p>\n<p>\u2193\u2193\u2193\u4f8b\u306f\u3053\u3093\u306a\u611f\u3058\u3067\u3059\u3002<\/p>\n<p>\ud83c\udf53\u3000\ud83c\udf4d\u3000\ud83c\udf53\u3000\ud83c\udf4d\u3000\ud83c\udf4d\u3000\uff1d\u3000<code>only_strawberry<\/code><\/p>\n<p>\u203b \u30d1\u30a4\u30ca\u30c3\u30d7\u30eb\u306f\u9664\u5916\u3059\u308b\u3068\u3044\u3046\u6761\u4ef6\u306a\u306e\u3067\u30a4\u30c1\u30b4\u3060\u3051\u306b\u306a\u3063\u3066\u3044\u307e\u3059\u3002<\/p>\n<p>\u3053\u308c\u304c\u3067\u304d\u308c\u3070\u3001\u5f8c\u306f\u4ee5\u4e0b\u306e\u3088\u3046\u306b<code>where()<\/code>\u3084<code>whereIn()<\/code>\u3092\u4f7f\u3063\u3066\u30b7\u30f3\u30d7\u30eb\u306b\u691c\u7d22\u304c\u3067\u304d\u308b\u3088\u3046\u306b\u306a\u308a\u307e\u3059\u3002<\/p>\n<pre>$box = \\App\\Box::<strong>where('status', 'only_strawberry')<\/strong>-&gt;get();<\/pre>\n<h1>Larave\u3067\u306f\u3069\u3046\u3059\u308c\u3070\u3044\u3044\u304b<\/h1>\n<p>\u3067\u306f\u3001\u3053\u306e\u300c\u5148\u306b\u3084\u3063\u3068\u3053\u3046\u300d\u4f5c\u6226\u3092\u5b9f\u969b\u306b<code>Laravel<\/code>\u3067\u5b9f\u73fe\u3059\u308b\u306b\u306f\u3069\u3046\u3059\u308c\u3070\u3044\u3044\u304b\u3092\u9806\u3092\u8ffd\u3063\u3066\u307f\u3066\u3044\u304d\u307e\u3057\u3087\u3046\u3002<\/p>\n<h2>\u30c6\u30fc\u30d6\u30eb\u3068\u30c6\u30b9\u30c8\u30c7\u30fc\u30bf\u3092\u7528\u610f\u3059\u308b<\/h2>\n<p>\u5148\u306b\u30b3\u30fc\u30c9\u3092\u5b9f\u884c\u3059\u308b\u305f\u3081\u306e\u30c6\u30fc\u30d6\u30eb\u3092\u30c6\u30b9\u30c8\u30c7\u30fc\u30bf\u3092DB\u306b\u7528\u610f\u3057\u307e\u3059\u3002\u305f\u3060\u3057\u3001\u3082\u3057\u3059\u3067\u306b\u3053\u306e\u69cb\u9020\u3092\u4f5c\u3063\u3066\u3044\u308b\u65b9\u306f\u6b21\u306e\u9805\u76ee\u307e\u3067\u8aad\u307f\u98db\u3070\u3057\u3066\u304f\u3060\u3055\u3044\u3002<\/p>\n<p>\u307e\u305a\u3001\u4ee5\u4e0b\u306e\u30b3\u30de\u30f3\u30c9\u3067\u30e2\u30c7\u30eb\u3068\u30de\u30a4\u30b0\u30ec\u30fc\u30b7\u30e7\u30f3\u3092\u4f5c\u6210\u3057\u307e\u3059\u3002<\/p>\n<pre><strong>php artisan make:model Box -m<\/strong>\n<strong>php artisan make:model BoxDetail -m<\/strong><\/pre>\n<p>\u305d\u3057\u3066\u3001\u4f5c\u6210\u3055\u308c\u305f\u5404\u30de\u30a4\u30b0\u30ec\u30fc\u30b7\u30e7\u30f3\u306e<code>up()<\/code>\u3092\u6b21\u306e\u3088\u3046\u306b\u3057\u307e\u3059\u3002<\/p>\n<p><strong>\uff08database\/migrations\/****_**_**_******_create_boxes_table.php\uff09<\/strong><\/p>\n<pre>public function up()\n{\n    <strong>Schema::create('boxes', function (Blueprint $table) {<\/strong>\n<strong>        $table-&gt;bigIncrements('id');<\/strong>\n<strong>        $table-&gt;string('name');\n        $table-&gt;string('status')-&gt;default('no_data'); \/\/ \u3053\u3053\u306b\u96c6\u8a08\u3057\u305f\u7d50\u679c\u304c\u306f\u3044\u308b<\/strong>\n<strong>        $table-&gt;timestamps();<\/strong>\n<strong>    });<\/strong>\n}<\/pre>\n<p><strong>\uff08database\/migrations\/****_**_**_******_create_box_details_table.php\uff09<\/strong><\/p>\n<pre>public function up()\n{\n    <strong>Schema::create('box_details', function (Blueprint $table) {<\/strong>\n<strong>        $table-&gt;bigIncrements('id');<\/strong>\n<strong>        $table-&gt;unsignedBigInteger('box_id');<\/strong>\n<strong>        $table-&gt;string('name');<\/strong>\n<strong>        $table-&gt;timestamps();<\/strong>\n\n<strong>        $table-&gt;foreign('box_id')-&gt;references('id')-&gt;on('boxes');<\/strong>\n<strong>    });<\/strong>\n}<\/pre>\n<p>\u6b21\u306b\u30c6\u30b9\u30c8\u30c7\u30fc\u30bf\u306e\u305f\u3081\u306e<code>Seeder<\/code>\u3092\u4f5c\u308a\u307e\u3059\u3002<br \/>\n\u4ee5\u4e0b\u306e\u30b3\u30de\u30f3\u30c9\u3092\u5b9f\u884c\u3057\u3066\u304f\u3060\u3055\u3044\u3002\uff08<code>Seeder<\/code>\u306f\uff11\u3064\u3060\u3051\u3067\u5b9f\u88c5\u3057\u307e\u3059\uff09<\/p>\n<pre><strong>php artisan make:seed BoxesTableSeeder<\/strong><\/pre>\n<p>\u305d\u3057\u3066\u3001<code>database\/seeds\/BoxesTableSeeder.php<\/code>\u3092\u958b\u3044\u3066\u4ee5\u4e0b\u306e\u3088\u3046\u306b\u5909\u66f4\u3057\u307e\u3059\u3002<\/p>\n<pre>public function run()\n{\n    <strong>for($i = 0 ; $i &lt; 10 ; $i++) {<\/strong>\n\n<strong>        $box = new \\App\\Box();<\/strong>\n<strong>        $box-&gt;name = '\u7bb1 - ' . $i;<\/strong>\n<strong>        $box-&gt;save();<\/strong>\n\n<strong>        for($j = 0 ; $j &lt; 10 ; $j++) {<\/strong>\n\n<strong>            $box_detail = new \\App\\BoxDetail();<\/strong>\n<strong>            $box_detail-&gt;box_id = $box-&gt;id;<\/strong>\n<strong>            $box_detail-&gt;name = Arr::random(['\u30d0\u30ca\u30ca', '\u30a4\u30c1\u30b4', '\u30ea\u30f3\u30b4', '\u3082\u3082', '\u3076\u3069\u3046']);<\/strong>\n<strong>            $box_detail-&gt;save();<\/strong>\n\n<strong>        }<\/strong>\n\n<strong>    }<\/strong>\n}<\/pre>\n<p>\u305d\u3057\u3066\u3001\u3053\u306e<code>Seeder<\/code>\u3092<code>database\/seeds\/DatabaseSeeder.php<\/code>\u306b\u767b\u9332\u3057\u3066\u5b9f\u884c\u3059\u308c\u3070\u5b8c\u4e86\u3067\u3059\u3002<\/p>\n<pre>public function run()\n{\n     \/\/$this-&gt;call(UsersTableSeeder::class);\n     <strong>$this-&gt;call(BoxesTableSeeder::class);<\/strong>\n}<\/pre>\n<p>\uff08\u30b3\u30de\u30f3\u30c9\u3092\u5b9f\u884c\uff09<\/p>\n<pre><strong>php artisan migrate:fresh --seed<\/strong><\/pre>\n<p>\u3053\u308c\u306710\u4ef6\u306e\u7bb1\u30c7\u30fc\u30bf\u3068\u3001100\u4ef6\u306e\u8a73\u7d30\u30c7\u30fc\u30bf\u304c\u4f5c\u6210\u3055\u308c\u307e\u3057\u305f\u3002<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-4754\" style=\"border: 3px solid #000;\" src=\"https:\/\/blog.capilano-fw.com\/wp-content\/uploads\/2019\/10\/complicated_has_many_2.png\" alt=\"\" width=\"520\" height=\"375\" srcset=\"https:\/\/blog.capilano-fw.com\/wp-content\/uploads\/2019\/10\/complicated_has_many_2.png 520w, https:\/\/blog.capilano-fw.com\/wp-content\/uploads\/2019\/10\/complicated_has_many_2-300x216.png 300w\" sizes=\"auto, (max-width: 520px) 100vw, 520px\" \/><\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-4753\" style=\"border: 3px solid #000;\" src=\"https:\/\/blog.capilano-fw.com\/wp-content\/uploads\/2019\/10\/complicated_has_many_3.png\" alt=\"\" width=\"530\" height=\"496\" srcset=\"https:\/\/blog.capilano-fw.com\/wp-content\/uploads\/2019\/10\/complicated_has_many_3.png 530w, https:\/\/blog.capilano-fw.com\/wp-content\/uploads\/2019\/10\/complicated_has_many_3-300x281.png 300w\" sizes=\"auto, (max-width: 530px) 100vw, 530px\" \/><\/p>\n<h2>Saved\u30a4\u30d9\u30f3\u30c8\u3092\u3064\u304f\u308b<\/h2>\n<p>\u5148\u307b\u3069\u306e\u300c\u3069\u306e\u30bf\u30a4\u30df\u30f3\u30b0\u3067\u5b50\u30c6\u30fc\u30d6\u30eb\u3092\u96c6\u8a08\u3059\u308b\u306e\u304b\uff1f\u300d\u3067\u8aac\u660e\u3057\u305f\u3068\u304a\u308a\u3001\u5b50\u30c6\u30fc\u30d6\u30eb\u306e\u96c6\u8a08\u306f\u5b50\u30c6\u30fc\u30d6\u30eb\u306b\u4f55\u3089\u304b\u306e\u5909\u66f4\u304c\u3042\u3063\u305f\uff08\u3082\u3063\u3068\u8a00\u3046\u3068<code>name<\/code>\u306b\u5909\u66f4\u304c\u3042\u3063\u305f\uff09\u3068\u304d\u306b\u306a\u308a\u307e\u3059\u3002<\/p>\n<p>\u3053\u308c\u3092\u5b9f\u73fe\u3059\u308b\u305f\u3081\u306bDB\u306b\u30c7\u30fc\u30bf\u304c\u4fdd\u5b58\u3055\u308c\u305f\u3089\u5b9f\u884c\u3055\u308c\u308b<code>Saved<\/code>\u30a4\u30d9\u30f3\u30c8\u3092<code>BoxDetail<\/code>\u30e2\u30c7\u30eb\u306b\u3064\u3051\u3066\u304a\u304d\u307e\u3057\u3087\u3046\u3002<\/p>\n<p>\u307e\u305a\u306f\u4ee5\u4e0b\u306e\u30b3\u30de\u30f3\u30c9\u3092\u5b9f\u884c\u3057\u3066\u304f\u3060\u3055\u3044\u3002<\/p>\n<pre><strong>php artisan make:event BoxDetailsSaved<\/strong><\/pre>\n<p>\u305d\u3057\u3066\u3001<code>app\/Events\/BoxDetailsSaved.php<\/code>\u3092\u4ee5\u4e0b\u306e\u3088\u3046\u306b\u5909\u66f4\u3057\u307e\u3059\u3002<\/p>\n<pre>&lt;?php\n\nnamespace App\\Events;\n\nuse App\\BoxDetail;\nuse Illuminate\\Broadcasting\\Channel;\nuse Illuminate\\Broadcasting\\InteractsWithSockets;\nuse Illuminate\\Broadcasting\\PresenceChannel;\nuse Illuminate\\Broadcasting\\PrivateChannel;\nuse Illuminate\\Contracts\\Broadcasting\\ShouldBroadcast;\nuse Illuminate\\Foundation\\Events\\Dispatchable;\nuse Illuminate\\Queue\\SerializesModels;\n\nclass BoxDetailsSaved\n{\n    use Dispatchable, InteractsWithSockets, SerializesModels;\n\n    \/**\n     * Create a new event instance.\n     *\n     * @return void\n     *\/\n    <strong>public function __construct(BoxDetail $current_detail)<\/strong>\n<strong>    {<\/strong>\n<strong>        \/\/ \u89aa\u30c7\u30fc\u30bf<\/strong>\n<strong>        $box = \\App\\Box::find($current_detail-&gt;box_id);<\/strong>\n\n<strong>        \/\/ \u4fdd\u5b58\u3057\u305f\u30c7\u30fc\u30bf\u306e\u89aaID\u3092\u3082\u3064\u30c7\u30fc\u30bf\u3092\u5168\u3066\u53d6\u5f97<\/strong>\n<strong>        $details = \\App\\BoxDetail::where('box_id', $current_detail-&gt;box_id)-&gt;pluck('name');<\/strong>\n\n<strong>        \/\/ \u91cd\u8907\u3059\u308b\u30c7\u30fc\u30bf\u3092\u524a\u9664<\/strong>\n<strong>        $unique_details = $details-&gt;unique();<\/strong>\n\n<strong>        $status = 'no_data';<\/strong>\n\n<strong>        if($unique_details-&gt;count() === 1 &amp;&amp; $unique_details-&gt;first() === '\u30d0\u30ca\u30ca') {  \/\/ \u30d0\u30ca\u30ca\u3060\u3051\u306e\u5834\u5408<\/strong>\n\n<strong>            \/\/ \u30d0\u30ca\u30ca\u306f\u9664\u5916\u3059\u308b\u306e\u3067\u30c7\u30fc\u30bf\u306a\u3057\u3068\u3057\u307e\u3059<\/strong>\n<strong>            $status = 'no_data';<\/strong>\n\n<strong>        }<\/strong>\n\n<strong>        \/\/ \u7701\u7565\uff08\u305d\u306e\u4ed6\u3082 \"else if\" \u3067\u30b9\u30c6\u30fc\u30bf\u30b9\u3092\u5909\u66f4\uff09<\/strong>\n\n<strong>        \/\/ \u89aa\u30c7\u30fc\u30bf\u306e\u30b9\u30c6\u30fc\u30bf\u30b9\u3092\u4fdd\u5b58<\/strong>\n<strong>        $box-&gt;status = $status;<\/strong>\n<strong>        $box-&gt;save();<\/strong>\n\n<strong>    }<\/strong>\n\n    \/**\n     * Get the channels the event should broadcast on.\n     *\n     * @return \\Illuminate\\Broadcasting\\Channel|array\n     *\/\n    public function broadcastOn()\n    {\n        return new PrivateChannel('channel-name');\n    }\n}<\/pre>\n<p>\u307e\u305f\u3001\u30a4\u30d9\u30f3\u30c8\u306f\u4f5c\u6210\u3059\u308b\u3060\u3051\u3067\u306f\u5b9f\u884c\u3055\u308c\u307e\u305b\u3093\u306e\u3067<code>BoxDetail<\/code>\u30e2\u30c7\u30eb\u306b\u4ee5\u4e0b\u306e\u3088\u3046\u306b\u767b\u9332\u3057\u3066\u304a\u304d\u307e\u3059\u3002<\/p>\n<pre>&lt;?php\n\nnamespace App;\n\nuse App\\Events\\BoxDetailsSaved;\nuse Illuminate\\Database\\Eloquent\\Model;\n\nclass BoxDetail extends Model\n{\n    <strong>protected $dispatchesEvents = [<\/strong>\n<strong>        'saved' =&gt; BoxDetailsSaved::class<\/strong>\n<strong>    ];<\/strong>\n}<\/pre>\n<p>\u203b \u6ce8\u610f\uff1a Laravel 5.4\u307e\u3067\u306f<code>$dispatchesEvents<\/code>\u306f\u3001<code>$events<\/code>\u3068\u3044\u3046\u540d\u524d\u3067\u3057\u305f\u3002<\/p>\n<p>\u3053\u308c\u3067\u3001\u5b50\u30c6\u30fc\u30d6\u30eb\u304c\u4fdd\u5b58\u3055\u308c\u305f\u3089\u81ea\u52d5\u7684\u306b\u5185\u5bb9\u3092\u96c6\u8a08\u3057\u3066\u3001\u89aa\u30c7\u30fc\u30bf\u306e\u30b9\u30c6\u30fc\u30bf\u30b9\u3092\u66f4\u65b0\u3057\u3066\u304f\u308c\u308b\u3088\u3046\u306b\u306a\u308a\u307e\u3057\u305f\u3002<\/p>\n<p>\u304a\u75b2\u308c\u69d8\u3067\u3057\u305f\ud83d\ude0a\u2728<\/p>\n<h1>\u3061\u306a\u307f\u306b<\/h1>\n<p>\u30a4\u30d9\u30f3\u30c8\u3092\u3064\u3051\u305f\u3068\u304d\u306b\u305f\u307e\u306b\u554f\u984c\u306b\u306a\u3063\u3066\u304f\u308b\u306e\u3067\u3059\u304c\u3001\u30a4\u30d9\u30f3\u30c8\u306e\u4e2d\u3067\u4fdd\u5b58\u3059\u308b\u3053\u3068\u306b\u3088\u3063\u3066\u3001\u9023\u9396\u7684\u306b\u3055\u3089\u306b\u5225\u306e\uff08\u3082\u3057\u304f\u306f\u540c\u3058\uff09\u30a4\u30d9\u30f3\u30c8\u304c\u5b9f\u884c\u3055\u308c\u3066\u3057\u307e\u3063\u305f\u308a\u3059\u308b\u30b1\u30fc\u30b9\u304c\u3042\u308a\u307e\u3059\u3002<\/p>\n<p>\u3082\u3057\u3053\u306e\u3088\u3046\u306a\u3053\u3068\u3092\u9632\u3050\u305f\u3081\u306b\u306f<code>withoutEvents()<\/code>\u3092\u4f7f\u3063\u3066\u4e00\u6642\u7684\u306b\u30a4\u30d9\u30f3\u30c8\u3092\u5916\u3057\u3066\u5b9f\u884c\u3059\u308b\u3068\u3044\u3044\u3067\u3057\u3087\u3046\u3002<\/p>\n<pre><strong>\\App\\User::withoutEvents(function(){<\/strong>\n\n<strong>    $user = new \\App\\User();<\/strong>\n<strong>    \/\/ \u7701\u7565<\/strong>\n<strong>    $user-&gt;save();<\/strong>\n\n<strong>});<\/strong><\/pre>\n<p>\u203b \u305f\u3060\u3057\u3001<code>withoutEvents()<\/code>\u306f\u30d0\u30fc\u30b8\u30e7\u30f3<code>5.7.26<\/code>\u304b\u3089\u8ffd\u52a0\u3055\u308c\u305f\u3082\u306e\u3067\u3001\u305d\u308c\u4ee5\u524d\u306f\u4ee5\u4e0b\u306e\u3088\u3046\u306b<code>unsetEventDispatcher()<\/code>\u3092\u4f7f\u3063\u3066\u30a4\u30d9\u30f3\u30c8\u3092\u56de\u907f\u3057\u3066\u3044\u307e\u3057\u305f\u3002<\/p>\n<pre><strong>\\App\\User::unsetEventDispatcher();<\/strong>\n\n$user = new \\App\\User();\n\/\/ \u7701\u7565\n$user-&gt;save();<\/pre>\n<p>\u307e\u305f\u3001\u72ec\u81ea<code>artisan<\/code>\u30b3\u30de\u30f3\u30c9\u3067\u30c7\u30fc\u30bf\u4fdd\u5b58\uff06<code>saved<\/code>\u30a4\u30d9\u30f3\u30c8\u5b9f\u884c\u306e\u3068\u304d\u306b\u3001\u30b0\u30ed\u30fc\u30d0\u30eb\u30b9\u30b3\u30fc\u30d7\u3092\u4f7f\u3063\u3066\u3044\u3066\u3046\u307e\u304f\u3044\u304b\u306a\u3044\u3068\u3044\u3046\u3053\u3068\u304c\u3042\u308a\u307e\u3059\u3002\uff08\u30b3\u30de\u30f3\u30c9\u3067\u5b9f\u884c\u3059\u308b\u3068\u30ed\u30b0\u30a4\u30f3\u3057\u3066\u306a\u3044\u306e\u3067\u30e6\u30fc\u30b6\u30fcID\u304c\u3068\u308c\u306a\u304b\u3063\u305f\u308a\u3001\u305d\u3082\u305d\u3082\u30e6\u30fc\u30b6\u30fcID\u9650\u5b9a\u306b\u306f\u3057\u306a\u3044\u76ee\u7684\u3060\u3063\u305f\u308a\u3059\u308b\u308f\u3051\u3067\u3059\u306d\ud83d\ude02\uff09<\/p>\n<p>\u305d\u3093\u306a\u5834\u5408\u306f\u3001\u4ee5\u4e0b\u306e\u3088\u3046\u306b\u3057\u3066\u30b0\u30ed\u30fc\u30d0\u30eb\u30b9\u30b3\u30fc\u30d7\u3082\u4f7f\u308f\u306a\u3044\u3088\u3046\u306b\u3059\u308b\u3068\u3044\u3044\u3067\u3057\u3087\u3046\u3002<\/p>\n<pre>\\App\\User::<strong>withoutGlobalScope(LoginUserScope::class)<\/strong>-&gt;get();<\/pre>\n<h1>\u304a\u308f\u308a\u306b<\/h1>\n<p>\u3068\u3044\u3046\u3053\u3068\u3067\u3001\u4eca\u56de\u306f\u3044\u3064\u3082\u306e\u57fa\u672c\u7684\u306a\u5185\u5bb9\u3067\u306f\u306a\u304f\u57fa\u672c\u30c6\u30af\u30cb\u30c3\u30af\u3092\u5bc4\u305b\u96c6\u3081\u305f\u5fdc\u7528\u7684\u306a\u5185\u5bb9\u3092\u304a\u5c4a\u3051\u3057\u307e\u3057\u305f\u3002<\/p>\n<p>\u3084\u306f\u308a\u57fa\u672c\u304c\u3057\u3063\u304b\u308a\u3057\u3066\u3044\u306a\u3044\u3068\u5fdc\u7528\u306f\u3067\u304d\u306a\u3044\u3068\u3044\u3046\u3053\u3068\u3092\u518d\u8a8d\u8b58\u3055\u305b\u3089\u308c\u307e\u3057\u305f\u3002\u3053\u308c\u304b\u3089\u3082\u57fa\u672c\u3092\u5927\u4e8b\u306b\u3057\u3066\u300c\u6b63\u3057\u3044\u52aa\u529b\u300d\u3092\u7d9a\u3051\u3066\u3044\u304d\u305f\u3044\u3068\u601d\u3044\u307e\u3059\ud83d\ude0a<\/p>\n<p>\u305f\u3060\u3001\u305d\u308c\u306b\u3057\u3066\u3082\u958b\u767a\u8005\u3068\u3057\u3066\u6c17\u306b\u306a\u308b\u306e\u306f\u300c\u4eca\u56de\u306e\u8907\u96d1\u306a\u6761\u4ef6\u3092SQL\u3060\u3051\u3067\u5b9f\u73fe\u3067\u304d\u308b\u306e\u304b\u3069\u3046\u304b\uff1f\uff1f\u300d\u3067\u3059\u3002<\/p>\n<p>\u3082\u3057\u3001SQL\u4e00\u767a\u3067\u5b9f\u884c\u53ef\u80fd\u306a\u65b9\u6cd5\u3092\u77e5\u3063\u3066\u3044\u305f\u3089\u3001\u305c\u3072<a href=\"https:\/\/docs.google.com\/forms\/d\/e\/1FAIpQLSdqMmc1sGcm4BaHTInlMTn-8tFf4pV1k4JV0GovSfTqUddnrg\/viewform\" target=\"_blank\" rel=\"noopener noreferrer\">\u304a\u554f\u3044\u5408\u308f\u305b\u30d5\u30a9\u30fc\u30e0<\/a>\u304b\u3089\u6559\u3048\u3066\u304f\u3060\u3055\u3044\uff01\u3053\u3053\u306e\u8a18\u4e8b\u3067\u8ffd\u8a18\u3068\u3057\u3066\u304a\u540d\u524d\uff06\u30ea\u30f3\u30af\u3068\u4e00\u7dd2\u306b\u7d39\u4ecb\u3055\u305b\u3066\u3044\u305f\u3060\u304d\u307e\u3059\u3002<\/p>\n<p>\u304a\u5f85\u3061\u3057\u3066\u3044\u307e\u3059\u3002<\/p>\n<p>\u3067\u306f\u3067\u306f\u301c\uff01<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-4445 aligncenter\" src=\"https:\/\/blog.capilano-fw.com\/wp-content\/uploads\/2019\/08\/no_2.png\" alt=\"\" width=\"370\" height=\"310\" srcset=\"https:\/\/blog.capilano-fw.com\/wp-content\/uploads\/2019\/08\/no_2.png 370w, https:\/\/blog.capilano-fw.com\/wp-content\/uploads\/2019\/08\/no_2-300x251.png 300w\" sizes=\"auto, (max-width: 370px) 100vw, 370px\" \/><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u3055\u3066\u3055\u3066\u3001\u79c1\u306fLaravel\u3092\u4f7f\u3063\u305f\u30a6\u30a7\u30d6\u958b\u767a\u3092\u306f\u3058\u3081\u3066\u304b\u3089\uff15\u5e74\u4ee5\u4e0a\u306e\u6642\u9593\u304c\u7d4c\u3063\u3066\u3044\u307e\u3059\u3002 \u305d\u3057\u3066\u3001\u3042\u308a\u304c\u305f\u3044\u3053\u3068\u306b\u57fa\u672c\u7684\u306b\u4f55\u304b\u65b0\u3057\u3044\u6a5f\u80fd\u3092\u958b\u767a\u3059\u308b\u5834\u5408\u3067\u3082\u300c\u3046 &hellip; <\/p>\n<p class=\"link-more\"><a href=\"https:\/\/blog.capilano-fw.com\/?p=4748\" class=\"more-link\"><span class=\"screen-reader-text\">&#8220;Laravel\u3067\u8907\u96d1\u306a\u30ea\u30ec\u30fc\u30b7\u30e7\u30f3\u30b7\u30c3\u30d7\u306e\u691c\u7d22\u3092\u30b7\u30f3\u30d7\u30eb\u306b\u3059\u308b&#8221; \u306e<\/span>\u7d9a\u304d\u3092\u8aad\u3080<\/a><\/p>\n","protected":false},"author":1,"featured_media":4757,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3],"tags":[175,128,60],"class_list":["post-4748","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-laravel","tag-sql","tag-128","tag-60"],"_links":{"self":[{"href":"https:\/\/blog.capilano-fw.com\/index.php?rest_route=\/wp\/v2\/posts\/4748","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=4748"}],"version-history":[{"count":6,"href":"https:\/\/blog.capilano-fw.com\/index.php?rest_route=\/wp\/v2\/posts\/4748\/revisions"}],"predecessor-version":[{"id":4758,"href":"https:\/\/blog.capilano-fw.com\/index.php?rest_route=\/wp\/v2\/posts\/4748\/revisions\/4758"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/blog.capilano-fw.com\/index.php?rest_route=\/wp\/v2\/media\/4757"}],"wp:attachment":[{"href":"https:\/\/blog.capilano-fw.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=4748"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.capilano-fw.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=4748"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.capilano-fw.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=4748"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}