{"id":8019,"date":"2020-09-28T15:00:56","date_gmt":"2020-09-28T06:00:56","guid":{"rendered":"https:\/\/blog.capilano-fw.com\/?p=8019"},"modified":"2020-09-25T00:36:55","modified_gmt":"2020-09-24T15:36:55","slug":"%e3%80%90laravel%e3%80%91%e3%82%b9%e3%82%b3%e3%83%bc%e3%83%97%e3%82%92%e4%bd%bf%e3%81%88%e3%81%b0%e3%80%81%e3%80%8c%e3%81%86%e3%81%a3%e3%81%8b%e3%82%8a%e3%80%8d%e3%81%8c%e6%b8%9b%e3%82%8a%e3%80%81","status":"publish","type":"post","link":"https:\/\/blog.capilano-fw.com\/?p=8019","title":{"rendered":"\u3010Laravel\u3011\u30b9\u30b3\u30fc\u30d7\u3092\u4f7f\u3048\u3070\u300c\u3046\u3063\u304b\u308a\u300d\u304c\u6e1b\u3063\u3066\u300c\u30e9\u30af\u300d\u3067\u304d\u308b"},"content":{"rendered":"<p>\u3055\u3066\u3055\u3066\u3001\u3053\u306e\u9593\u30ea\u30ea\u30fc\u30b9\u3055\u308c\u305f<code>Laravel<\/code>\u306e\u65b0\u30d0\u30fc\u30b8\u30e7\u30f3<code>8.x<\/code>\u306b\u3064\u3044\u3066\u306f\u79c1\u306e\u4e2d\u3067\u4e00\u65e6\u843d\u3061\u7740\u304d\u307e\u3057\u305f\u306e\u3067\u3001\u4eca\u56de\u306f\u65b0\u6a5f\u80fd\u3067\u306f\u306a\u304f\u3001\u5143\u3005\u3042\u308b\u4fbf\u5229\u306a\u6a5f\u80fd\u306b\u7684\u3092\u7d5e\u3063\u3066\u3054\u7d39\u4ecb\u3057\u3066\u3044\u304d\u305f\u3044\u3068\u601d\u3044\u307e\u3059\u3002<\/p>\n<p>\u30ba\u30d0\u30ea\u3001\u305d\u306e\u7d39\u4ecb\u3057\u305f\u3044\u6a5f\u80fd\u306f\u30fb\u30fb\u30fb\u30fb\u30fb\u30fb<\/p>\n<p><strong style=\"font-size: 35px;\">\u30b9\u30b3\u30fc\u30d7<\/strong><\/p>\n<p>\u3067\u3059\u3002<\/p>\n<p>\u3053\u3053\u3067\u8a00\u3046\u30b9\u30b3\u30fc\u30d7\u3068\u3044\u3046\u306e\u306f\u3001<code>JavaScript<\/code>\u306e\u30b9\u30b3\u30fc\u30d7\u3068\u306f\u5c11\u3057\u9055\u3063\u3066\u3044\u3066\u3001\u4e3b\u306bDB\u30c6\u30fc\u30d6\u30eb\u306e\u7d5e\u308a\u8fbc\u307f\u3092\u3059\u308b<code>where\u53e5<\/code>\u306b\u95a2\u9023\u3059\u308b\u6a5f\u80fd\u306e\u3053\u3068\u3067\u3059\u3002<\/p>\n<p>\u305d\u3057\u3066\u3001\u4e00\u8a00\u3067\u30b9\u30b3\u30fc\u30d7\u3068\u3044\u3063\u3066\u3082<code>Laravel<\/code>\u306b\u306f\u4ee5\u4e0b\uff12\u3064\u306e\u30b9\u30b3\u30fc\u30d7\u304c\u3042\u3063\u3066\u3001\u3069\u3061\u3089\u3092\u4f7f\u3046\u3079\u304d\u304b\u306f\u72b6\u6cc1\u306b\u3088\u3063\u3066\u5909\u308f\u3063\u3066\u304d\u307e\u3059\u3002<\/p>\n<ul>\n<li>\u30b0\u30ed\u30fc\u30d0\u30eb\u30fb\u30b9\u30b3\u30fc\u30d7<\/li>\n<li>\u30ed\u30fc\u30ab\u30eb\u30fb\u30b9\u30b3\u30fc\u30d7<\/li>\n<\/ul>\n<p>\u305d\u3053\u3067\u2757<\/p>\n<p>\u4eca\u56de\u306f\u3001\u3053\u306e\uff12\u3064\u306e\u30b9\u30b3\u30fc\u30d7\u3092\u3054\u7d39\u4ecb\u3057\u3066\u3044\u304d\u305f\u3044\u3068\u601d\u3044\u307e\u3059\u3002<br \/>\n\u305c\u3072\u7686\u3055\u3093\u306e\u304a\u5f79\u306b\u7acb\u3066\u307e\u3057\u305f\u3089\u5b09\u3057\u3044\u3067\u3059\ud83d\ude0a\u2728<\/p>\n<p style=\"text-align: center;\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-6071 aligncenter\" src=\"https:\/\/blog.capilano-fw.com\/wp-content\/uploads\/2020\/03\/no_8.png\" alt=\"\" width=\"216\" height=\"320\" srcset=\"https:\/\/blog.capilano-fw.com\/wp-content\/uploads\/2020\/03\/no_8.png 216w, https:\/\/blog.capilano-fw.com\/wp-content\/uploads\/2020\/03\/no_8-203x300.png 203w\" sizes=\"auto, (max-width: 216px) 100vw, 216px\" \/>\u300c\uff15\uff05\u3060\u3068\u601d\u3063\u305f\u3089\u3001\u3057\u3063\u304b\u308a<br \/>\n\u30b9\u30c8\u30ed\u30f3\u30b0\u306e\uff19\uff05\u3067\u30d8\u30ed\u30d8\u30ed\u3067\u3059\ud83d\ude02\u300d<\/p>\n<p><strong>\u958b\u767a\u74b0\u5883\uff1a<\/strong> Laravel 8.x\uff08\u30b9\u30b3\u30fc\u30d7\u306f\u904e\u53bb\u30d0\u30fc\u30b8\u30e7\u30f3\u3067\u3082\u4f7f\u3048\u307e\u3059\uff09<\/p>\n<h1>\u300c\u3046\u3063\u304b\u308a\u300d\u3092\u6e1b\u3089\u305b\u308b\u30b0\u30ed\u30fc\u30d0\u30eb\u30fb\u30b9\u30b3\u30fc\u30d7<\/h1>\n<h2>\u30b0\u30ed\u30fc\u30d0\u30eb\u30fb\u30b9\u30b3\u30fc\u30d7\u3068\u306f<\/h2>\n<p>\u30b0\u30ed\u30fc\u30d0\u30eb\u30fb\u30b9\u30b3\u30fc\u30d7\u306f\u3001\u7c21\u5358\u306b\u8a00\u3046\u3068\u3001\u300c<strong>\u3044\u3064\u3067\u3082\u7d5e\u308a\u8fbc\u307f\u304c\u6709\u52b9\u306b\u306a\u308b<\/strong>\u300d\u6a5f\u80fd\u3067\u3059\u3002<\/p>\n<p>\u5b9f\u969b\u306e\u4f8b\u3092\u898b\u3066\u307f\u307e\u3057\u3087\u3046\u3002<\/p>\n<p>\u3044\u307e\u3001\u30e6\u30fc\u30b6\u30fc\u304c\u6295\u7a3f\u3057\u305f\u8a18\u4e8b\u3092\u4fdd\u5b58\u3059\u308b<code>posts<\/code>\u3068\u3044\u3046\u30c6\u30fc\u30d6\u30eb\u304c\u3042\u308b\u3068\u3057\u307e\u3059\u3002<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-8020\" style=\"border: 3px solid #000;\" src=\"https:\/\/blog.capilano-fw.com\/wp-content\/uploads\/2020\/09\/laravel_scope_1.png\" alt=\"\" width=\"667\" height=\"461\" srcset=\"https:\/\/blog.capilano-fw.com\/wp-content\/uploads\/2020\/09\/laravel_scope_1.png 667w, https:\/\/blog.capilano-fw.com\/wp-content\/uploads\/2020\/09\/laravel_scope_1-300x207.png 300w\" sizes=\"auto, (max-width: 667px) 100vw, 667px\" \/><\/p>\n<p>\u3082\u3061\u308d\u3093\u8ab0\u3067\u3082\u6295\u7a3f\u3067\u304d\u308b\u306e\u3067\u3001\u3044\u308d\u3093\u306a<code>user_id<\/code>\u304c\u30c7\u30fc\u30bf\u3068\u3057\u3066\u4fdd\u5b58\u3055\u308c\u3066\u3044\u307e\u3059\u3002<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-8021\" style=\"border: 3px solid #000;\" src=\"https:\/\/blog.capilano-fw.com\/wp-content\/uploads\/2020\/09\/laravel_scope_2.png\" alt=\"\" width=\"314\" height=\"449\" srcset=\"https:\/\/blog.capilano-fw.com\/wp-content\/uploads\/2020\/09\/laravel_scope_2.png 314w, https:\/\/blog.capilano-fw.com\/wp-content\/uploads\/2020\/09\/laravel_scope_2-210x300.png 210w\" sizes=\"auto, (max-width: 314px) 100vw, 314px\" \/><\/p>\n<p>\u30fb\u30fb\u30fb\u3068\u3044\u3046\u3053\u3068\u306f\u3001\u30b3\u30fc\u30c9\u3092\u66f8\u304f\u3068\u304d\u306b\u3072\u3068\u3064\u6c17\u3092\u3064\u3051\u306a\u3044\u3068\u3044\u3051\u306a\u3044\u3053\u3068\u304c\u51fa\u3066\u304d\u307e\u3059\u3002<\/p>\n<p>\u305d\u3046\u3067\u3059\u3002\u300c<strong>\u30ed\u30b0\u30a4\u30f3\u3057\u3066\u3044\u308b\u30e6\u30fc\u30b6\u30fc\u306eID\u3067\u7d5e\u308a\u8fbc\u307f\u3092\u3057\u306a\u3044\u3068\u3001\u4ed6\u4eba\u306e\u6295\u7a3f\u307e\u3067\u5909\u66f4\u3084\u524a\u9664\u304c\u3067\u304d\u3066\u3057\u307e\u3046<\/strong>\u300d\u3068\u3044\u3046\u70b9\u3067\u3059\u3002<\/p>\n<p>\u305d\u306e\u305f\u3081\u3001\u901a\u5e38\u3060\u3068\u4ee5\u4e0b\u306e\u3088\u3046\u306b<code>where()<\/code>\u3092\u4f7f\u3063\u3066\u7d5e\u308a\u8fbc\u307f\u3092\u3059\u308b\u3053\u3068\u306b\u306a\u308b\u3068\u601d\u3044\u307e\u3059\u3002<\/p>\n<pre>$user_id = auth()-&gt;id();\r\n$posts = Post::<strong>where('user_id', $user_id)<\/strong>-&gt;get(); \/\/ \ud83d\udc48 <strong>\u672c\u4eba\u306e\u30c7\u30fc\u30bf\u3060\u3051\u53d6\u5f97<\/strong><\/pre>\n<p>\u3082\u3061\u308d\u3093\u3053\u306e\u5f62\u3067\u3082\u9593\u9055\u3044\u3067\u306f\u3042\u308a\u307e\u305b\u3093\u304c\u3001\u3053\u308c\u306b\u4f3c\u305f\u30b3\u30fc\u30c9\u3092\u4f55\u5ea6\u3082\u4f55\u5ea6\u3082\u66f8\u3044\u3066\u3044\u308b\u3068\u3001<strong>\u300c\u3046\u3063\u304b\u308a\u300d\u304c\u767a\u751f\u3057\u3001<code>where( ... )<\/code>\u304c\u629c\u3051\u3066\u3057\u307e\u3046\u3053\u3068\u3082\u8003\u3048\u3089\u308c\u307e\u3059\u3002<\/strong><\/p>\n<p>\u305d\u3093\u306a\u3068\u304d\u300c\u30b0\u30ed\u30fc\u30d0\u30eb\u30fb\u30b9\u30b3\u30fc\u30d7\u300d\u306e\u767b\u5834\u3067\u3059\u2757<\/p>\n<p>\u3064\u307e\u308a\u3001\u30b0\u30ed\u30fc\u30d0\u30eb\u30fb\u30b9\u30b3\u30fc\u30d7\u3092\u8a2d\u5b9a\u3057\u3066\u304a\u304f\u3068\u3001<strong>\u66f8\u3044\u3066\u306a\u304f\u3066\u3082\u5fc5\u305a user_id \u306e\u7d5e\u308a\u8fbc\u307f\u3092\u6709\u52b9\u306b\u3059\u308b\u3053\u3068\u304c\u3067\u304d\u307e\u3059\u3002<\/strong><\/p>\n<pre><strong>$posts = Post::get();<\/strong> \/\/ \ud83d\udc48 <strong>\u30b0\u30ed\u30fc\u30d0\u30eb\u30fb\u30b9\u30b3\u30fc\u30d7\u304c\u3042\u308c\u3070\u3001\u6bce\u56de user_id \u3067\u7d5e\u308a\u8fbc\u307f\u304c\u6709\u52b9\u306b\u306a\u308a\u307e\u3059<\/strong><\/pre>\n<p>\u3053\u308c\u306f\u3001<code>get()<\/code>\u3060\u3051\u3067\u306a\u304f\u3001<code>first()<\/code>\u306a\u3069\u306b\u3082\u6709\u52b9\u306b\u306a\u308a\u307e\u3059\u3002<\/p>\n<h2>\u30b0\u30ed\u30fc\u30d0\u30eb\u30fb\u30b9\u30b3\u30fc\u30d7\u3092\u8a2d\u5b9a\u3059\u308b\u65b9\u6cd5<\/h2>\n<p>\u3067\u306f\u3001\u5b9f\u969b\u306b\u30b0\u30ed\u30fc\u30d0\u30eb\u30fb\u30b9\u30b3\u30fc\u30d7\u3092\u8a2d\u5b9a\u3059\u308b\u65b9\u6cd5\u3092\u3054\u7d39\u4ecb\u3057\u307e\u3057\u3087\u3046\u3002<\/p>\n<p><strong>app\/Models\/Post.php<\/strong><\/p>\n<pre>&lt;?php\r\n\r\nnamespace App\\Models;\r\n\r\nuse Illuminate\\Database\\Eloquent\\Builder;\r\nuse Illuminate\\Database\\Eloquent\\Model;\r\n\r\nclass Post extends Model\r\n{\r\n    <strong>protected static function booted()<\/strong>\r\n<strong>    {<\/strong>\r\n<strong>        static::addGlobalScope('user_id', function (Builder $builder) {<\/strong>\r\n\r\n<strong>            $user_id = auth()-&gt;id();<\/strong>\r\n<strong>            $builder-&gt;where('user_id', $user_id);<\/strong>\r\n\r\n<strong>        });<\/strong>\r\n<strong>    }<\/strong>\r\n}<\/pre>\n<p>\u203b \u8a2d\u5b9a\u306f\u30e2\u30c7\u30eb\u3054\u3068\u306b\u884c\u3044\u307e\u3059\u3002\u7686\u3055\u3093\u306f\u304a\u597d\u304d\u306a\u30e2\u30c7\u30eb\u3067\u3084\u3063\u3066\u307f\u3066\u304f\u3060\u3055\u3044\u3002<\/p>\n<p>\u307e\u305a\u3001<code>addGlobalScope<\/code>\u306e\u3059\u3050\u6a2a\u306b\u3042\u308b<code>user_id<\/code>\u306f\u540d\u524d\u3067\u3001\u3053\u308c\u306f\u597d\u304d\u306a\u6587\u5b57\u5217\u3067OK\u3067\u3059\u3002\uff08\u305f\u3060\u3001\u30b0\u30ed\u30fc\u30d0\u30eb\u30fb\u30b9\u30b3\u30fc\u30d7\u3092\u89e3\u9664\u3059\u308b\u3068\u304d\u306b\u3053\u306e\u540d\u524d\u3092\u4f7f\u3046\u306e\u3067\u899a\u3048\u3084\u3059\u3044\u3082\u306e\u304c\u3044\u3044\u3067\u3057\u3087\u3046\ud83d\ude0a\uff09<\/p>\n<p>\u305d\u3057\u3066\u3001<code>function() { ... }<\/code>\u5185\u304c\u30b0\u30ed\u30fc\u30d0\u30eb\u30fb\u30b9\u30b3\u30fc\u30d7\u3068\u3057\u3066\u3044\u3064\u3067\u3082\u5b9f\u884c\u3055\u308c\u308b<code>where\u53e5<\/code>\u306b\u306a\u308a\u307e\u3059\u3002<\/p>\n<p>\u3067\u306f\u3001\u8a2d\u5b9a\u524d\u3068\u5f8c\u3067\u3001\u4ee5\u4e0b\u306e\u30b3\u30fc\u30c9\u304c\u3069\u3046\u9055\u3063\u3066\u304f\u308b\u304b\u898b\u3066\u307f\u307e\u3057\u3087\u3046\u3002<\/p>\n<pre><strong>$posts = Post::get();\r\ndd($posts-&gt;toArray());\r\n<\/strong><\/pre>\n<p><strong>\uff08\u8a2d\u5b9a\u3059\u308b\u524d\uff1a\u5168\u3066\u306e user_id \u304c\u30c7\u30fc\u30bf\u3068\u3057\u3066\u53d6\u5f97\u3055\u308c\u307e\u3059\uff09<\/strong><\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-8024\" src=\"https:\/\/blog.capilano-fw.com\/wp-content\/uploads\/2020\/09\/laravel_scope_3.png\" alt=\"\" width=\"318\" height=\"517\" srcset=\"https:\/\/blog.capilano-fw.com\/wp-content\/uploads\/2020\/09\/laravel_scope_3.png 318w, https:\/\/blog.capilano-fw.com\/wp-content\/uploads\/2020\/09\/laravel_scope_3-185x300.png 185w\" sizes=\"auto, (max-width: 318px) 100vw, 318px\" \/><\/p>\n<p><strong>\uff08\u8a2d\u5b9a\u3057\u305f\u5f8c\uff1a\u8a72\u5f53\u3059\u308b<code>user_id<\/code>\u3060\u3051\u304c\u53d6\u5f97\u3055\u308c\u307e\u3059\uff09<\/strong><\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-8023\" src=\"https:\/\/blog.capilano-fw.com\/wp-content\/uploads\/2020\/09\/laravel_scope_4.png\" alt=\"\" width=\"324\" height=\"518\" srcset=\"https:\/\/blog.capilano-fw.com\/wp-content\/uploads\/2020\/09\/laravel_scope_4.png 324w, https:\/\/blog.capilano-fw.com\/wp-content\/uploads\/2020\/09\/laravel_scope_4-188x300.png 188w\" sizes=\"auto, (max-width: 324px) 100vw, 324px\" \/><\/p>\n<p>\u3053\u3093\u306a\u3075\u3046\u306b\u3057\u3066\u300c\u3046\u3063\u304b\u308a\u300d\u3092\u6e1b\u3089\u3059\u3053\u3068\u304c\u3067\u304d\u308b\u308f\u3051\u3067\u3059\u306d\u3002<\/p>\n<h2>\u30b0\u30ed\u30fc\u30d0\u30eb\u30b9\u30b3\u30fc\u30d7\u3092\u89e3\u9664\u3059\u308b\u65b9\u6cd5<\/h2>\n<p>\u3068\u306f\u3044\u3048\u3001\u72b6\u6cc1\u306b\u3088\u3063\u3066\u306f\u3001\u30b0\u30ed\u30fc\u30d0\u30eb\u30fb\u30b9\u30b3\u30fc\u30d7\u3092\u89e3\u9664\u3057\u305f\u3044\u5834\u9762\u3082\u51fa\u3066\u304f\u308b\u3068\u601d\u3044\u307e\u3059\u3002<\/p>\n<p>\u203b \u4f8b\u3048\u3070\u3001\u7ba1\u7406\u8005\u304c<code>posts<\/code>\u306e\u5168\u30c7\u30fc\u30bf\u3092\u691c\u7d22\u3057\u3066\u5909\u66f4\u3084\u524a\u9664\u3092\u3059\u308b\u5834\u5408\u3067\u3059\u3002<\/p>\n<p>\u305d\u3093\u306a\u5834\u5408\u306f\u6b21\u306e\u3088\u3046\u306b\u3059\u308b\u3053\u3068\u3067\u7c21\u5358\u306b\u30b0\u30ed\u30fc\u30d0\u30eb\u30fb\u30b9\u30b3\u30fc\u30d7\u3092\u89e3\u9664\u3059\u308b\u3053\u3068\u304c\u3067\u304d\u307e\u3059\u3002<\/p>\n<pre>$posts = Post::<strong>withoutGlobalScope('user_id')<\/strong>-&gt;get();<\/pre>\n<p><code>user_id<\/code>\u3068\u3044\u3046\u306e\u306f\u5148\u307b\u3069\u8a2d\u5b9a\u3057\u305f\u540d\u524d\u306b\u306a\u308a\u307e\u3059\u3002<\/p>\n<h2>\u540c\u3058\u30b0\u30ed\u30fc\u30d0\u30eb\u30fb\u30b9\u30b3\u30fc\u30d7\u3092\u4f55\u5ea6\u3082\u8a2d\u5b9a\u3059\u308b\u306e\u304c\u9762\u5012\u306a\u5834\u5408<\/h2>\n<p>\u5b9f\u306f\u30b0\u30ed\u30fc\u30d0\u30eb\u30fb\u30b9\u30b3\u30fc\u30d7\u306f\u3082\u3046\u3072\u3068\u3064\u8a2d\u5b9a\u65b9\u6cd5\u304c\u3042\u3063\u3066\u3001\u305d\u308c\u304c\u72ec\u81ea\u306e\u30af\u30e9\u30b9\u3092\u3064\u304f\u308b\u65b9\u6cd5\u3067\u3059\u3002<\/p>\n<p>\u3053\u306e\u65b9\u6cd5\u3067\u306f\u3001<strong>\u4f55\u5ea6\u3082\u540c\u3058\u30b0\u30ed\u30fc\u30d0\u30eb\u30fb\u30b9\u30b3\u30fc\u30d7\u3092\u30e2\u30c7\u30eb\u306b\u66f8\u304b\u306a\u304f\u3066\u3082\u3088\u304f\u306a\u308a\u3001\u7701\u30b3\u30fc\u30c9\u5316\u3067\u304d\u307e\u3059\u3002<\/strong><\/p>\n<p>\u3067\u306f\u3001\u5b9f\u969b\u306b\u4f7f\u3044\u65b9\u3092\u898b\u3066\u3044\u304d\u307e\u3057\u3087\u3046\u3002<br \/>\n\u4ee5\u4e0b\u306e\u30d5\u30a1\u30a4\u30eb\u3092\u4f5c\u6210\u3057\u3066\u304f\u3060\u3055\u3044\u3002<\/p>\n<p><strong>app\/Scopes\/UserIdScope.php<\/strong><\/p>\n<pre><strong>&lt;?php<\/strong>\r\n\r\n<strong>namespace App\\Scopes;<\/strong>\r\n\r\n<strong>use Illuminate\\Database\\Eloquent\\Builder;<\/strong>\r\n<strong>use Illuminate\\Database\\Eloquent\\Model;<\/strong>\r\n<strong>use Illuminate\\Database\\Eloquent\\Scope;<\/strong>\r\n\r\n<strong>class UserIdScope implements Scope<\/strong>\r\n<strong>{<\/strong>\r\n<strong>    public function apply(Builder $builder, Model $model)<\/strong>\r\n<strong>    {<\/strong>\r\n<strong>        $user_id = auth()-&gt;id();<\/strong>\r\n<strong>        $builder-&gt;where('user_id', $user_id);<\/strong>\r\n<strong>    }<\/strong>\r\n<strong>}<\/strong><\/pre>\n<p>\u203b\u6b8b\u5ff5\u306a\u304c\u3089\u3001<code>artisan<\/code>\u30b3\u30de\u30f3\u30c9\u306f\u3042\u308a\u307e\u305b\u3093\ud83d\ude2d<\/p>\n<p>\u305d\u3057\u3066\u3001\u3053\u306e\u30af\u30e9\u30b9\u3092\u30e2\u30c7\u30eb\u306b\u8a2d\u5b9a\u3057\u307e\u3059\u3002<\/p>\n<pre>&lt;?php\r\n\r\nnamespace App\\Models;\r\n\r\nuse App\\Scopes\\UserIdScope;\r\nuse Illuminate\\Database\\Eloquent\\Builder;\r\nuse Illuminate\\Database\\Eloquent\\Model;\r\n\r\nclass Post extends Model\r\n{\r\n    protected static function booted()\r\n    {\r\n        <strong>static::addGlobalScope(new UserIdScope);<\/strong>\r\n    }\r\n}<\/pre>\n<p>\u3069\u3046\u3067\u3057\u3087\u3046\u3002\u3055\u3063\u304d\u3068\u6bd4\u3079\u308b\u3068\u3001\u305f\u3063\u305f\uff11\u884c\u3067\u30b0\u30ed\u30fc\u30d0\u30eb\u30fb\u30b9\u30b3\u30fc\u30d7\u3092\u6709\u52b9\u306b\u3059\u308b\u3053\u3068\u304c\u3067\u304d\u308b\u3088\u3046\u306b\u306a\u308a\u307e\u3057\u305f\u3002<\/p>\n<p>\u305d\u3057\u3066\u3001<code>Post<\/code>\u3060\u3051\u3067\u306a\u304f\u3001<code>user_id<\/code>\u3067\u7d5e\u308a\u8fbc\u307f\u3092\u3057\u305f\u3044\u30e2\u30c7\u30eb\u306b\u3082\u4f7f\u3044\u56de\u3057\u304c\u3067\u304d\u308b\u306e\u3067\u3068\u3066\u3082\u4fbf\u5229\u3067\u3059\ud83d\udc4d<\/p>\n<p>\u306a\u304a\u3001\u89e3\u9664\u3059\u308b\u5834\u5408\u306f\u6b21\u306e\u3088\u3046\u306b\u306a\u308a\u307e\u3059\u3002<\/p>\n<pre>Post::<strong>withoutGlobalScope(UserIdScope::class)<\/strong>-&gt;get();<\/pre>\n<h2>\u3061\u306a\u307f\u306b\uff1awhere\u53e5\u4ee5\u5916\u3082\u4f7f\u3048\u307e\u3059<\/h2>\n<p>\u308f\u304b\u308a\u3084\u3059\u304f\u3059\u308b\u305f\u3081\u306b<code>where\u53e5<\/code>\u306e\u307f\u3067\u8aac\u660e\u3057\u307e\u3057\u305f\u304c\u3001<code>orderBy()<\/code>\u306a\u3069\u3082\u4f7f\u3048\u307e\u3059\u3002<\/p>\n<pre>protected static function booted()\r\n{\r\n    static::addGlobalScope('user_id', function (Builder $builder) {\r\n\r\n        $builder-&gt;<strong>orderBy('user_id', 'desc');<\/strong>\r\n\r\n    });\r\n}<\/pre>\n<h1>\u300c\u30e9\u30af\u300d\u3067\u304d\u308b\u30ed\u30fc\u30ab\u30eb\u30fb\u30b9\u30b3\u30fc\u30d7<\/h1>\n<h2>\u30ed\u30fc\u30ab\u30eb\u30fb\u30b9\u30b3\u30fc\u30d7\u3068\u306f<\/h2>\n<p>\u300c<strong>\u30ed\u30fc\u30ab\u30eb\u30fb\u30b9\u30b3\u30fc\u30d7<\/strong>\u300d\u306f\u30b0\u30ed\u30fc\u30d0\u30eb\u30fb\u30b9\u30b3\u30fc\u30d7\u3068\u9055\u3063\u3066\u3001<strong>\u3044\u3064\u3067\u3082\u81ea\u52d5\u3067\u6709\u52b9\u306b\u306a\u308b\u3082\u306e\u3067\u306f\u3042\u308a\u307e\u305b\u3093<\/strong>\u304c\u3001\u300c\u5fc5\u8981\u306a\u3068\u304d\u306b\u81e8\u6a5f\u5fdc\u5909\u3067\u300d\u30e9\u30af\u304c\u3067\u304d\u308b\u6a5f\u80fd\u3067\u3059\u3002<\/p>\n<p>\u3053\u3061\u3089\u3082\u5b9f\u969b\u306e\u4f8b\u3092\u898b\u3066\u307f\u307e\u3057\u3087\u3046\u3002<\/p>\n<p>\u4f8b\u3048\u3070\u3001<code>users<\/code>\u30c6\u30fc\u30d6\u30eb\u306e\u4e2d\u304b\u3089\u4ee5\u4e0b\u306e\u6761\u4ef6\u306b\u5408\u3046\u30c7\u30fc\u30bf\u3092\u53d6\u5f97\u3059\u308b\u5834\u5408\u3067\u3059\u3002<\/p>\n<ul>\n<li>\u6307\u5b9a\u3057\u305f\u6708\uff08\u4f8b\u3048\u3070\u4eca\u6708\u3001\u5148\u6708\u306a\u3069\uff09\u306b\u30e6\u30fc\u30b6\u30fc\u767b\u9332\u3057\u305f<\/li>\n<li>\u30e1\u30fc\u30eb\u8a8d\u8a3c\u3092\u6e08\u307e\u305b\u3066\u3044\u308b\uff08<code>email_verified_at<\/code>\u304c<code>null<\/code>\u3067\u306f\u306a\u3044\u3082\u306e\uff09<\/li>\n<\/ul>\n<p>\u3053\u3046\u3044\u3063\u305f\u5834\u5408\u3001\u901a\u5e38\u306f\u4ee5\u4e0b\u306e\u3088\u3046\u306b\u3057\u3066\u30b3\u30fc\u30c9\u3092\u66f8\u304f\u3068\u601d\u3044\u307e\u3059\u3002<\/p>\n<pre><strong>$year = 2020;<\/strong>\r\n<strong>$month = 9;<\/strong>\r\n<strong>$start_dt = Carbon::createFromDate($year, $month)-&gt;firstOfMonth();<\/strong>\r\n<strong>$end_dt = $start_dt-&gt;copy()-&gt;endOfMonth();<\/strong>\r\n\r\n<strong>$users = User::whereBetween('created_at', [$start_dt, $end_dt])<\/strong>\r\n<strong>    -&gt;whereNotNull('email_verified_at')<\/strong>\r\n<strong>    -&gt;get();<\/strong><\/pre>\n<p>\u3053\u308c\u3082\u3082\u3061\u308d\u3093\u9593\u9055\u3044\u3067\u306f\u3042\u308a\u307e\u305b\u3093\u3002<br \/>\n\u305f\u3060\u3001<strong>\u3044\u308d\u3093\u306a\u5834\u6240\u3067\u4f55\u5ea6\u3082\u3053\u306e\u30b3\u30fc\u30c9\u304c\u5fc5\u8981\u306b\u306a\u308b\u3068\u3057\u305f\u3089\u3069\u3046\u3067\u3057\u3087\u3046\u3002<\/strong><\/p>\n<p>\u300c\u3046\u30fc\u3093\u3001\u6bce\u56de\u3053\u306e\u30b3\u30fc\u30c9\u66f8\u304f\u306e\u3081\u3093\u3069\u304f\u3055\u3044\u30fb\u30fb\u30fb\ud83d\ude2d\u300d<\/p>\n<p>\u3068\u306f\u306a\u3089\u306a\u3044\u3067\u3057\u3087\u3046\u304b\u3002<\/p>\n<p>\u305d\u3093\u306a\u3068\u304d\u306b\u30ed\u30fc\u30ab\u30eb\u30fb\u30b9\u30b3\u30fc\u30d7\u306e\u767b\u5834\u3067\u3059\u2757<\/p>\n<p>\u7c21\u5358\u306b\u3044\u3046\u3068\u3001\u4ee5\u4e0b\u306e\u3088\u3046\u306b\u72ec\u81ea\u306e<code>where\u53e5<\/code>\u3092\u4f5c\u3063\u3066\u305f\u3063\u305f\uff11\u884c\u3060\u3051\u3067OK\u306b\u306a\u308a\u307e\u3059\u3002<\/p>\n<pre><strong>$users = User::whereCreated($year, $month)-&gt;get(); \/\/ \ud83d\udc48 \u3055\u3063\u304d\u3068\u540c\u3058\u6761\u4ef6\u306b\u3067\u304d\u308b<\/strong><\/pre>\n<h2>\u30ed\u30fc\u30ab\u30eb\u30fb\u30b9\u30b3\u30fc\u30d7\u3092\u8a2d\u5b9a\u3059\u308b\u65b9\u6cd5<\/h2>\n<p>\u3067\u306f\u3001\u5b9f\u969b\u306b\u30ed\u30fc\u30ab\u30eb\u30fb\u30b9\u30b3\u30fc\u30d7\u3092\u3064\u304f\u3063\u3066\u307f\u307e\u3057\u3087\u3046\u3002<br \/>\n\u30b0\u30ed\u30fc\u30d0\u30eb\u30fb\u30b9\u30b3\u30fc\u30d7\u3068\u540c\u3058\u304f\u5404\u30e2\u30c7\u30eb\u5185\u306b\u8a2d\u5b9a\u3057\u307e\u3059\u3002<\/p>\n<p><strong>app\/Models\/User.php<\/strong><\/p>\n<pre>&lt;?php\r\n\r\nnamespace App\\Models;\r\n\r\n<strong>use Carbon\\Carbon; <\/strong>\/\/ \ud83d\udc48 \u8ffd\u52a0\u3057\u307e\u3057\u305f\r\n\r\n\/\/ \u7701\u7565\r\n\r\nclass User extends Authenticatable\r\n{\r\n    \/\/ \u7701\u7565\r\n\r\n    \/\/ \u30b9\u30b3\u30fc\u30d7\r\n<strong>    public function scopeWhereCreated($query, $year, $month) {<\/strong>\r\n\r\n<strong>        $start_dt = Carbon::createFromDate($year, $month)-&gt;firstOfMonth();<\/strong>\r\n<strong>        $end_dt = $start_dt-&gt;copy()-&gt;endOfMonth();<\/strong>\r\n\r\n<strong>        $query-&gt;whereBetween('created_at', [$start_dt, $end_dt])<\/strong>\r\n<strong>            -&gt;whereNotNull('email_verified_at');<\/strong>\r\n\r\n<strong>    }<\/strong><\/pre>\n<p>\u4e0a\u306e\u30b3\u30fc\u30c9\u3092\u898b\u3066\u3044\u305f\u3060\u304f\u3068\u308f\u304b\u308b\u3068\u304a\u308a\u3001<strong>\u300cscope\u300d+ \u300cxxxxx\u300d<\/strong>\u3068\u3044\u3046\u30e1\u30bd\u30c3\u30c9\u3092\u3064\u304f\u308b\u3068\u3001\u300c<strong>xxxxx()<\/strong>\u300d\u304c\u30ed\u30fc\u30ab\u30eb\u30fb\u30b9\u30b3\u30fc\u30d7\u306b\u306a\u308a\u307e\u3059\u3002<\/p>\n<p>\u3064\u307e\u308a\u3001\u4eca\u56de\u3067\u8a00\u3046\u3068<code>whereCreated()<\/code>\u3067\u3059\u306d\u3002<\/p>\n<p>\u306a\u304a\u3001\u5909\u6570\u304c\u4e0d\u8981\u306a\u5834\u5408\u306f\u3001<code>$query<\/code>\u3060\u3051\u6b8b\u3057\u3066\u304a\u3051\u3070OK\u3067\u3059\u3002<br \/>\n\u4f8b\u3048\u3070\u3001\u4eca\u6708\u767b\u9332\u3057\u305f\u30e6\u30fc\u30b6\u30fc\u3060\u3051\u3092\u53d6\u5f97\u3059\u308b\u30b9\u30b3\u30fc\u30d7\u306f\u3053\u3061\u3089\u3067\u3059\u3002<\/p>\n<pre><strong>public function scopeWhereCreatedThisMonth($query) {<\/strong>\r\n\r\n<strong>    $start_dt = today()-&gt;firstOfMonth();<\/strong>\r\n<strong>    $end_dt = $start_dt-&gt;copy()-&gt;endOfMonth();<\/strong>\r\n\r\n<strong>    $query-&gt;whereBetween('created_at', [$start_dt, $end_dt])<\/strong>\r\n<strong>        -&gt;whereNotNull('email_verified_at');<\/strong>\r\n\r\n<strong>}<\/strong><\/pre>\n<h2>\u3061\u306a\u307f\u306b\uff1awhere\u53e5\u4ee5\u5916\u3082\u4f7f\u3048\u307e\u3059<\/h2>\n<p>\u30b0\u30ed\u30fc\u30d0\u30eb\u30fb\u30b9\u30b3\u30fc\u30d7\u3068\u540c\u3058\u3067\u3059\u304c\u4ee5\u4e0b\u306e\u3088\u3046\u306b\u3059\u308b\u3068\u3001<code>orderBy()<\/code>\u306a\u3069\u5225\u306e\u30e1\u30bd\u30c3\u30c9\u3067\u6761\u4ef6\u6307\u5b9a\u3059\u308b\u3053\u3068\u3082\u3067\u304d\u307e\u3059\u3002<\/p>\n<pre><strong>public function scopeOrderByCreated($query, $direction = 'asc') {<\/strong>\r\n\r\n<strong>    $query-&gt;orderBy('created_at', $direction)<\/strong><strong>;<\/strong>\r\n\r\n<strong>}<\/strong><\/pre>\n<p>\u4f7f\u3044\u65b9\u306f\u3053\u3046\u306a\u308a\u307e\u3059\u3002<\/p>\n<pre><strong>$users = User::orderByCreated('desc')-&gt;get();<\/strong><\/pre>\n<h1>\u304a\u308f\u308a\u306b<\/h1>\n<p>\u3068\u3044\u3046\u3053\u3068\u3067\u3001\u4eca\u56de\u306f<code>Laravel<\/code>\u306e\u4fbf\u5229\u306a\u6a5f\u80fd\u300c\u30b9\u30b3\u30fc\u30d7\u300d\u306b\u3064\u3044\u3066\u3054\u7d39\u4ecb\u3057\u307e\u3057\u305f\u3002<\/p>\n<p>\u30b9\u30b3\u30fc\u30d7\u3092\u4f7f\u3046\u3053\u3068\u3067\u3001<strong>\u4f55\u5ea6\u3082\u540c\u3058\u30b3\u30fc\u30c9\u3092\u66f8\u304b\u306a\u304f\u3066\u3088\u304f\u306a\u308a\u307e\u3059<\/strong>\u3057\u3001\u3055\u3089\u306b\u3001\u3082\u3057\u5909\u66f4\u304c\u3042\u3063\u305f\u3068\u3057\u3066\u3082<strong>\u30b9\u30b3\u30fc\u30d7\u306e\u4e2d\u8eab\u3060\u3051\u66f8\u304d\u63db\u3048\u308b\u3060\u3051\u3067\u5168\u3066\u306e\u5834\u6240\u306b\u9069\u7528\u3067\u304d\u307e\u3059<\/strong>\u3002<\/p>\n<p>\u3068\u3044\u3046\u3053\u3068\u3067\u3001\u7701\u30b3\u30fc\u30c9\u306b\u306a\u308a\u307e\u3059\u306e\u3067\u3001<\/p>\n<ul>\n<li>\u958b\u767a\u901f\u5ea6\u304c\u65e9\u304f\u306a\u308b<\/li>\n<li>\u30b3\u30fc\u30c9\u304c\u5c11\u306a\u3044 \uff1d \u30d0\u30b0\u3082\u5c11\u306a\u3044<\/li>\n<\/ul>\n<p>\u3068\u306a\u3063\u3066\u3001\u6211\u3005\u306b\u3068\u3063\u3066\u306f\u30e1\u30ea\u30c3\u30c8\u304c\u5927\u304d\u3044\u3068\u3044\u3063\u3066\u3044\u3044\u3093\u3058\u3083\u306a\u3044\u3067\u3057\u3087\u3046\u304b\u3002\uff08\u3082\u3061\u308d\u3093\u4f7f\u3044\u3069\u3053\u308d\u306e\u5224\u65ad\u304c\u91cd\u8981\u3067\u3059\u3051\u3069\u306d\ud83d\udc4d\uff09<\/p>\n<p>\u305c\u3072\u307f\u306a\u3055\u3093\u3082\u30b9\u30b3\u30fc\u30d7\u3092\u6d3b\u7528\u3057\u3066\u30b3\u30fc\u30c9\u3092\u66f8\u3044\u3066\u307f\u3066\u304f\u3060\u3055\u3044\u306d\u3002<\/p>\n<p>\u3067\u306f\u3067\u306f\u301c\u2757<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-3405 aligncenter\" src=\"https:\/\/blog.capilano-fw.com\/wp-content\/uploads\/2019\/04\/no_18.png\" alt=\"\" width=\"310\" height=\"320\" srcset=\"https:\/\/blog.capilano-fw.com\/wp-content\/uploads\/2019\/04\/no_18.png 310w, https:\/\/blog.capilano-fw.com\/wp-content\/uploads\/2019\/04\/no_18-291x300.png 291w\" sizes=\"auto, (max-width: 310px) 100vw, 310px\" \/><\/p>\n<p style=\"text-align: center;\">\u300c\u30ea\u30a2\u30eb\u3067\u3082\u30b0\u30ed\u30fc\u30d0\u30eb\u306a\u8996\u91ce<br \/>\n\u304c\u307b\u3057\u3044\u3067\u3059\ud83d\ude02\u300d<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u3055\u3066\u3055\u3066\u3001\u3053\u306e\u9593\u30ea\u30ea\u30fc\u30b9\u3055\u308c\u305fLaravel\u306e\u65b0\u30d0\u30fc\u30b8\u30e7\u30f38.x\u306b\u3064\u3044\u3066\u306f\u79c1\u306e\u4e2d\u3067\u4e00\u65e6\u843d\u3061\u7740\u304d\u307e\u3057\u305f\u306e\u3067\u3001\u4eca\u56de\u306f\u65b0\u6a5f\u80fd\u3067\u306f\u306a\u304f\u3001\u5143\u3005\u3042\u308b\u4fbf\u5229\u306a\u6a5f\u80fd\u306b\u7684\u3092\u7d5e\u3063\u3066\u3054 &hellip; <\/p>\n<p class=\"link-more\"><a href=\"https:\/\/blog.capilano-fw.com\/?p=8019\" class=\"more-link\"><span class=\"screen-reader-text\">&#8220;\u3010Laravel\u3011\u30b9\u30b3\u30fc\u30d7\u3092\u4f7f\u3048\u3070\u300c\u3046\u3063\u304b\u308a\u300d\u304c\u6e1b\u3063\u3066\u300c\u30e9\u30af\u300d\u3067\u304d\u308b&#8221; \u306e<\/span>\u7d9a\u304d\u3092\u8aad\u3080<\/a><\/p>\n","protected":false},"author":1,"featured_media":8037,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3],"tags":[],"class_list":["post-8019","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-laravel"],"_links":{"self":[{"href":"https:\/\/blog.capilano-fw.com\/index.php?rest_route=\/wp\/v2\/posts\/8019","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=8019"}],"version-history":[{"count":11,"href":"https:\/\/blog.capilano-fw.com\/index.php?rest_route=\/wp\/v2\/posts\/8019\/revisions"}],"predecessor-version":[{"id":8040,"href":"https:\/\/blog.capilano-fw.com\/index.php?rest_route=\/wp\/v2\/posts\/8019\/revisions\/8040"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/blog.capilano-fw.com\/index.php?rest_route=\/wp\/v2\/media\/8037"}],"wp:attachment":[{"href":"https:\/\/blog.capilano-fw.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=8019"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.capilano-fw.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=8019"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.capilano-fw.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=8019"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}