{"id":4590,"date":"2019-09-05T02:18:24","date_gmt":"2019-09-04T17:18:24","guid":{"rendered":"https:\/\/blog.capilano-fw.com\/?p=4590"},"modified":"2019-10-03T18:19:24","modified_gmt":"2019-10-03T09:19:24","slug":"laravel-6-0%e3%81%aelazy-collection%ef%bc%88%e6%96%b0%e6%a9%9f%e8%83%bd%ef%bc%89%e3%82%92%e7%b4%b9%e4%bb%8b%e3%81%97%e3%81%be%e3%81%99","status":"publish","type":"post","link":"https:\/\/blog.capilano-fw.com\/?p=4590","title":{"rendered":"Laravel 6.x\u306eLazy Collection\uff08\u65b0\u6a5f\u80fd\uff09\u3092\u7d39\u4ecb\u3057\u307e\u3059"},"content":{"rendered":"<p>\u3055\u3066\u3055\u3066\u3001\u5148\u65e5<code>Laravel<\/code>\u306e\u30d0\u30fc\u30b8\u30e7\u30f3<code>6.x<\/code>\u304c\u30ea\u30ea\u30fc\u30b9\u3055\u308c\u305f\u306e\u3067<a href=\"https:\/\/blog.capilano-fw.com\/?p=4576\">\u524d\u56de\u8a18\u4e8b<\/a>\u3067\u306f\uff08\u5b9f\u88c5\u65b9\u6cd5\u304c\u5909\u66f4\u306b\u306a\u3063\u3066\u3044\u305f\uff09\u30ed\u30b0\u30a4\u30f3\u6a5f\u80fd\u306e\u4f7f\u3044\u65b9\u3092\u3054\u7d39\u4ecb\u3057\u307e\u3057\u305f\u3002<\/p>\n<p>\u305d\u3057\u3066\u3001\u65b0\u30d0\u30fc\u30b8\u30e7\u30f3\u306e<code>Laravel<\/code>\u306b\u306f\u5909\u66f4\u3060\u3051\u3067\u306a\u304f\u8ffd\u52a0\u6a5f\u80fd\u3082\u3044\u304f\u3064\u304b\u7528\u610f\u3055\u308c\u3066\u3044\u3066\u4e2d\u3067\u3082\u958b\u767a\u8005\u304c<strong>\u300c\u30b2\u30fc\u30e0\u30fb\u30c1\u30a7\u30f3\u30b8\u30e3\u30fc\u300d<\/strong>\u3068\u547c\u3076\u6a5f\u80fd\u304c\u3042\u308a\u307e\u3059\u3002<\/p>\n<p>\u305d\u308c\u304c\u3001<\/p>\n<p><strong style=\"font-size: 35px;\">Lazy Collection\uff08\u308c\u3044\u3058\u30fc\u3053\u308c\u304f\u3057\u3087\u3093\uff09<\/strong><\/p>\n<p>\u3067\u3059\u3002<\/p>\n<p>\u3053\u308c\u306f\u3001\u3053\u308c\u307e\u3067\u306e<code>Laravel<\/code>\u3067\u3082\u4eba\u6c17\u304c\u9ad8\u3044<a href=\"https:\/\/laravel.com\/docs\/6.0\/collections\">Collection<\/a>\u306e\u6a5f\u80fd\u3092\u62e1\u5f35\u3059\u308b\u3082\u306e\u3067\u3001\u4f7f\u3044\u65b9\u306b\u3088\u3063\u3066\u306f\u30b3\u30fc\u30c9\u306e\u5b9f\u884c\u3092\u7701\u529b\u5316\u3059\u308b\u3053\u3068\u304c\u3067\u304d\u308b\u6a5f\u80fd\u306b\u306a\u3063\u3066\u3044\u307e\u3059\u3002<\/p>\n<p>\u3068\u3044\u3046\u3053\u3068\u3067\u3001\u4eca\u56de\u306f<code>Laravel 6.x<\/code>\u306e\u65b0\u6a5f\u80fd\u306e\u4e2d\u304b\u3089<code>Lazy Collection<\/code>\u3092\u7d39\u4ecb\u3057\u307e\u3059\u3002\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-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><strong>\u958b\u767a\u74b0\u5883\uff1a<\/strong> Laravel 6.1<\/p>\n<h1>Lazy Collection\u3068\u306f<\/h1>\n<p>\u307e\u305a\u3001<code>Lazy Collection<\/code>\u304c\u3069\u3093\u306a\u3082\u306e\u304b\u3068\u3044\u3046\u3068<strong>\u300c\u5fc5\u8981\u306a\u3068\u304d\u306b\u5fc5\u8981\u306a\u3082\u306e\u3060\u3051\u51e6\u7406\u3059\u308b\u300d<\/strong>\u30b3\u30ec\u30af\u30b7\u30e7\u30f3\u3067\u3001\u3053\u308c\u306b\u3088\u3063\u3066\u3069\u3093\u306a\u3044\u3044\u3053\u3068\u304c\u8d77\u3053\u308b\u304b\u3068\u3044\u3046\u3068<strong>PHP\u306e\u30e1\u30e2\u30ea\u4f7f\u7528\u91cf\u3092\u6e1b\u3089\u3059\u3053\u3068\u304c\u3067\u304d\u308b<\/strong>\u306e\u3067\u3059\u3002<\/p>\n<p>\u4f8b\u3048\u3070\u3001DB\u304b\u3089100\u4e07\u4ef6\u3082\u3042\u308b\u3088\u3046\u306a\u5927\u91cf\u306e\u30c7\u30fc\u30bf\u3092\u53d6\u5f97\u3059\u308b\u3068\u3057\u307e\u3059\u3002\u901a\u5e38\u3060\u3068\u3001\u307e\u305a100\u4e07\u4ef6\u306e\u30c7\u30fc\u30bf\u3092\u30e1\u30e2\u30ea\u4e0a\u306b\u4fdd\u5b58\u3057\u3066\u3001\u305d\u3053\u304b\u3089\u5fc5\u8981\u306a\u30c7\u30fc\u30bf\u3092\u3068\u308a\u3060\u3059\u3068\u3044\u3046\u6d41\u308c\u306b\u306a\u308b\u308f\u3051\u3067\u3059\u304c\u3001<code>Lazy Collection<\/code>\u306e\u5834\u5408\u306f\u5fc5\u8981\u306a\u30c7\u30fc\u30bf\u306e\u307f\u306e\u30e1\u30e2\u30ea\u91cf\u3067OK\u306b\u306a\u308a\u307e\u3059\u3002<\/p>\n<p>\u3061\u3087\u3063\u3068\u9055\u3046\u304b\u3082\u3057\u308c\u307e\u305b\u3093\u304c\u3001\u5fc5\u8981\u306a\u6642\u306b\u5fc5\u8981\u306a\u3060\u3051\u51e6\u7406\u3092\u3059\u308b\u3068\u3044\u3046\u610f\u5473\u3067\u306f\u753b\u9762\u304c\u79fb\u52d5\u3057\u305f\u6642\u306b\u3060\u3051\u753b\u50cf\u3092\u8aad\u307f\u8fbc\u3080<strong>\u300cLazy loading\u300d<\/strong>\u3068\u3044\u3046\u6a5f\u80fd\u306b\u30a2\u30a4\u30c7\u30a2\u306f\u4f3c\u3066\u3044\u308b\u304b\u3082\u3057\u308c\u307e\u305b\u3093\u3002<\/p>\n<h1>Lazy Collection\u306e\u4ed5\u7d44\u307f<\/h1>\n<p><code>Lazy Collection<\/code>\u306f\u3001<code>Laravel<\/code>\u72ec\u81ea\u306e\u3082\u306e\u3067\u306f\u306a\u304f<code>PHP<\/code>\u306e<code>Generators<\/code>\u3068\u3044\u3046\u6a5f\u80fd\u3092\u5229\u7528\u3057\u305f\u3082\u306e\u306b\u306a\u3063\u3066\u3044\u307e\u3059\u3002<\/p>\n<p>\u4f8b\u3048\u3070\u3001<code>Generators<\/code>\u306f\u4ee5\u4e0b\u306e\u3088\u3046\u306b<strong>\u300c1\u301c100\u4e07\u306e\u7bc4\u56f2\u5185\u3067\uff11\u304b\u3089\uff12\u305a\u3064\u9032\u3081\u305f\u6570\u5b57\u3092\u8868\u793a\u3059\u308b\u300d<\/strong>\u3068\u3044\u3046\u4f8b\u3092\u898b\u3066\u307f\u307e\u3057\u3087\u3046\u3002<\/p>\n<pre><strong>foreach(range(1, 1000000, 2) as $number) {<\/strong>\r\n\r\n<strong>    echo \"$number \"; \/\/ \u3053\u3053\u306f\u30011\u3001\uff13\u3001\uff15\u30fb\u30fb\u30fb\u3068\u306a\u308b<\/strong>\r\n\r\n<strong>}<\/strong><\/pre>\n<p>\u3053\u306e\u5834\u5408\u306f<code>range()<\/code>\u306f\u307e\u305a<code>1 \u301c 100\u4e07<\/code>\u307e\u3067\u306e\u6570\u5b57\u3092\u30e1\u30e2\u30ea\u4e0a\u306b\u7528\u610f\u3057\u307e\u3059\u3002\u305d\u3057\u3066\u3001\u305d\u306e\u5f8c\u3067\uff12\u305a\u3064\u9032\u3081\u305f\u6570\u5b57\u3092\u30d4\u30c3\u30af\u30a2\u30c3\u30d7\u3059\u308b\u5f62\u306b\u306a\u308a\u307e\u3059\u3002<\/p>\n<p>\u5f53\u7136\u5148\u306b\u5168\u3066\u306e\u6570\u5b57\u3092\u6e96\u5099\u3059\u308b\u306e\u3067\u3001\u30e1\u30e2\u30ea\u3082\u305d\u306e\u5168\u3066\u306b\u5fc5\u8981\u306b\u306a\u3063\u3066\u304d\u307e\u3059\u3002<\/p>\n<p>\u3067\u306f\u3001<code>Generators<\/code>\u3092\u4f7f\u3063\u305f\u5834\u5408\u3092\u898b\u3066\u307f\u307e\u3057\u3087\u3046\u3002<\/p>\n<pre><strong>function xrange($start, $limit, $step = 1) {<\/strong>\r\n\r\n<strong>    for($i = $start; $i &lt;= $limit; $i += $step) {<\/strong>\r\n\r\n<strong>        yield $i; \/\/ \u3053\u3053\u3067\u5fc5\u8981\u306a\u30c7\u30fc\u30bf\u3092\u8fd4\u3059<\/strong>\r\n\r\n<strong>    }<\/strong>\r\n\r\n<strong>}<\/strong>\r\n\r\n<strong>foreach(xrange(1, 1000000, 2) as $number) {<\/strong>\r\n\r\n<strong>    echo \"$number \";<\/strong>\r\n\r\n<strong>}<\/strong><\/pre>\n<p>\u307e\u305a\u3001<code>xrange()<\/code>\u3068\u3044\u3046\u306e\u304c\u72ec\u81ea\u306b\u3064\u304f\u3063\u305f<code>Generators<\/code>\u3067\u3001\u3053\u306e\u4e2d\u3067\u5fc5\u8981\u306a\u30c7\u30fc\u30bf\u3060\u3051<code>yield<\/code>\u3067\u8fd4\u3059\u3053\u3068\u306b\u306a\u308a\u307e\u3059\u3002\u3053\u308c\u3092\u5b9f\u884c\u3059\u308b\u3068\u5148\u307b\u3069\u7d39\u4ecb\u3057\u305f<code>range()<\/code>\u306e\u4f8b\u3068\u307e\u3063\u305f\u304f\u540c\u3058\u7d50\u679c\u306b\u306a\u308a\u307e\u3059\u304c\u3001\u30e1\u30e2\u30ea\u306e\u4f7f\u7528\u91cf\u306f\u5fc5\u8981\u306a\u5206\u3060\u3051\u3067\u3088\u304f\u306a\u308a\u307e\u3059\u3002<\/p>\n<p>\u3067\u306f\u3001\u30e1\u30e2\u30ea\u306e\u6700\u5927\u5229\u7528\u5024\u304c\u5206\u304b\u308b<code>memory_get_peak_usage()<\/code>\u3067\u3053\u306e\uff12\u3064\u30b3\u30fc\u30c9\u3092\u5b9f\u884c\u3057\u3066\u8a08\u6e2c\u3059\u308b\u3068\u3069\u3046\u306a\u308b\u3067\u3057\u3087\u3046\u304b\u3002<\/p>\n<p>\u79c1\u306e\u74b0\u5883\u3067\u306f\u3001\u7d50\u679c\u306f\u4ee5\u4e0b\u306e\u3088\u3046\u306b\u306a\u308a\u307e\u3057\u305f\u3002<\/p>\n<ul>\n<li>range()\u3000\u30fb\u30fb\u30fb\u300019,556,472\uff08\u7d04 19.5 MB\uff09<\/li>\n<li>xrange()\u3000\u30fb\u30fb\u30fb\u30002,799,800\uff08\u7d04 2.7 MB\uff09<\/li>\n<\/ul>\n<p>\u306a\u3093\u3068<code>Generators<\/code>\u3092\u4f7f\u3046\u3068\u30e1\u30e2\u30ea\u306f\uff17\u5206\u306e\uff11\u7a0b\u5ea6\u3067\u5b9f\u884c\u3067\u304d\u307e\u3057\u305f\u3002<\/p>\n<p>\u3064\u307e\u308a\u3001<code>Lazy Collection<\/code>\u306f\u3053\u306e<code>Generators<\/code>\u3092\u5229\u7528\u3057\u305f<strong>\u300c\u30e1\u30e2\u30ea\u3092\u7701\u529b\u5316\u3057\u305f\u30b3\u30ec\u30af\u30b7\u30e7\u30f3\u300d<\/strong>\u3068\u3044\u3046\u308f\u3051\u3067\u3059\u306d\u3002<\/p>\n<h1>Lazy Collection\u306e\u4f7f\u3044\u65b9<\/h1>\n<p>\u5927\u304d\u304f\u5206\u3051\u3066\uff12\u30d1\u30bf\u30fc\u30f3\u3042\u308a\u307e\u3059\u306e\u3067\u3072\u3068\u3064\u305a\u3064\u7d39\u4ecb\u3057\u3066\u3044\u304d\u307e\u3059\u3002<\/p>\n<h2>DB\u3092\u4f7f\u308f\u306a\u3044\u5834\u5408<\/h2>\n<p>\u4f8b\u3048\u3070\u3001\u4ee5\u4e0b\u306e\u30b5\u30f3\u30d7\u30eb\u306e\u3088\u3046\u306a\uff11\u884c\u305a\u3064\u540d\u524d\u304c\u8a18\u8ff0\u3055\u308c\u305f\u30c6\u30ad\u30b9\u30c8\u30d5\u30a1\u30a4\u30eb\u304c\u3042\u308b\u3068\u3057\u307e\u3059\u3002<\/p>\n<p>\uff08\u30b5\u30f3\u30d7\u30eb\u306e\u30c6\u30ad\u30b9\u30c8\u30d5\u30a1\u30a4\u30eb\uff09<\/p>\n<pre><strong>\u5c71\u7530\u592a\u90ce\r\n\u9234\u6728\u6b21\u90ce\r\n\u7530\u4e2d\u4e09\u90ce<\/strong>\r\n\uff08\u4ee5\u4e0b\u7d9a\u304f...\uff09<\/pre>\n<p>\u305d\u3057\u3066\u3001\u3053\u306e\u4e2d\u304b\u3089\u540d\u524d\u306e\u4e2d\u306b\u300c\u7530\u300d\u304c\u5165\u3063\u3066\u3044\u308b\u4eba\u3060\u3051\u3092<code>Lazy Collection<\/code>\u3068\u3057\u3066\u53d6\u5f97\u3059\u308b\u306b\u306f\u4ee5\u4e0b\u306e\u3088\u3046\u306b\u3057\u307e\u3059\u3002<\/p>\n<pre><strong>$collection = LazyCollection::make(function() {<\/strong>\r\n\r\n<strong>    $handle = fopen('name.txt', 'r');<\/strong>\r\n\r\n<strong>    while(($line = fgets($handle)) !== false) {<\/strong>\r\n\r\n<strong>        if(Str::contains($line, '\u7530')) {<\/strong>\r\n\r\n<strong>            yield trim($line);<\/strong>\r\n\r\n<strong>        }<\/strong>\r\n\r\n<strong>    }<\/strong>\r\n\r\n<strong>});<\/strong><\/pre>\n<p>\u3061\u306a\u307f\u306b\u3053\u306e\u30b3\u30fc\u30c9\u3067\u5fc5\u8981\u306a\u30cd\u30fc\u30e0\u30b9\u30da\u30fc\u30b9\u306f\u4ee5\u4e0b\u306e\uff12\u3064\u3067\u3059\u3002<\/p>\n<pre><strong>use Illuminate\\Support\\LazyCollection;<\/strong>\r\n<strong>use Illuminate\\Support\\Str;<\/strong><\/pre>\n<p>\u3053\u3046\u3059\u308b\u3053\u3068\u3067\u3001<strong>\u5168\u54e1\u306e\u540d\u524d\u3092\u30e1\u30e2\u30ea\u306b\u7528\u610f\u3057\u306a\u304f\u3066\u3082\u901a\u5e38\u306e\u30b3\u30ec\u30af\u30b7\u30e7\u30f3\u3068\u540c\u3058\u4f7f\u3044\u65b9\u304c\u51fa\u6765\u308b<\/strong>\u3068\u3044\u3046\u308f\u3051\u3067\u3059\u3002\u4f8b\u3048\u3070\u3001\u5168\u54e1\u306b\u300c\u3055\u3093\u300d\u3092\u3064\u3051\u308b\u5834\u5408\u306f\u3053\u306e\u3088\u3046\u306b\u306a\u308a\u307e\u3059\u3002<\/p>\n<pre><strong>$collection = LazyCollection::make(function() {<\/strong>\r\n\r\n<strong>    \/\/ \u7701\u7565<\/strong>\r\n\r\n<strong>})-&gt;map(function($name){<\/strong>\r\n\r\n<strong>    return $name .' \u3055\u3093';<\/strong>\r\n\r\n<strong>});<\/strong>\r\n\r\ndump($collection-&gt;toArray());<\/pre>\n<p>\u7d50\u679c\u306f\u3053\u3046\u306a\u308a\u307e\u3059\u3002<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-4591\" src=\"https:\/\/blog.capilano-fw.com\/wp-content\/uploads\/2019\/09\/lazy_collection_1.png\" alt=\"\" width=\"165\" height=\"313\" srcset=\"https:\/\/blog.capilano-fw.com\/wp-content\/uploads\/2019\/09\/lazy_collection_1.png 165w, https:\/\/blog.capilano-fw.com\/wp-content\/uploads\/2019\/09\/lazy_collection_1-158x300.png 158w\" sizes=\"auto, (max-width: 165px) 100vw, 165px\" \/><\/p>\n<h2>DB\u3092\u4f7f\u3046\u5834\u5408<\/h2>\n<p>\u7d9a\u3044\u3066\u306f\u30c7\u30fc\u30bf\u30d9\u30fc\u30b9\u3092\u4f7f\u3046\u5834\u5408\u3067\u3059\u3002<br \/>\n\u4f8b\u3048\u3070<a href=\"https:\/\/blog.capilano-fw.com\/?p=4576\">\u524d\u56de\u306e\u8a18\u4e8b<\/a>\u3067\u4f5c\u3063\u305f\u4ee5\u4e0b\u306e\u30c6\u30b9\u30c8\u30e6\u30fc\u30b6\u30fc\u3067\u8a66\u3057\u3066\u307f\u307e\u3057\u3087\u3046\u3002<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-4579\" style=\"border: 3px solid #000;\" src=\"https:\/\/blog.capilano-fw.com\/wp-content\/uploads\/2019\/09\/laravel_60_auth_3.png\" alt=\"\" width=\"609\" height=\"464\" srcset=\"https:\/\/blog.capilano-fw.com\/wp-content\/uploads\/2019\/09\/laravel_60_auth_3.png 609w, https:\/\/blog.capilano-fw.com\/wp-content\/uploads\/2019\/09\/laravel_60_auth_3-300x229.png 300w\" sizes=\"auto, (max-width: 609px) 100vw, 609px\" \/><\/p>\n<p>\u3067\u306f\u3001\u3053\u306e\u4e2d\u304b\u3089<strong>\u300c\u30e1\u30fc\u30eb\u30a2\u30c9\u30ec\u30b9\u306b &#8220;s&#8221; \u304c\u5165\u3063\u3066\u3044\u308b\u30c7\u30fc\u30bf\u300d<\/strong>\u3060\u3051\u53d6\u5f97\u3057\u3066\u307f\u307e\u3059\u3002<\/p>\n<pre><strong>$users = \\App\\User::cursor()-&gt;filter(function($user) {<\/strong>\r\n\r\n<strong>    return Str::contains($user-&gt;email, 's');<\/strong>\r\n\r\n<strong>});<\/strong>\r\n\r\n<strong>dd($users-&gt;toArray());<\/strong><\/pre>\n<p>\u91cd\u8981\u306a\u90e8\u5206\u306f\u3001\u4eca\u56de\u306f\u5148\u307b\u3069\u306e\u3088\u3046\u306b<code>yield<\/code>\u3092\u4f7f\u3063\u3066\u3044\u306a\u3044\u70b9\u3067\u3059\u3002<br \/>\n\u3053\u308c\u306f\u3001<code>cursor()<\/code>\u30e1\u30bd\u30c3\u30c9\u306e\u4e2d\u3067\u3059\u3067\u306b\u5b9a\u7fa9\u3055\u308c\u3066\u3044\u308b\u304b\u3089\u3067\u3059\u3002<\/p>\n<p>\uff08Laravel\u306e\u30b3\u30fc\u30c9\u304b\u3089\u306e\u5f15\u7528\uff09<\/p>\n<pre><strong>return new LazyCollection(function () {<\/strong>\r\n<strong>    yield from $this-&gt;connection-&gt;cursor(<\/strong>\r\n<strong>        $this-&gt;toSql(), $this-&gt;getBindings(), ! $this-&gt;useWritePdo<\/strong>\r\n<strong>    );<\/strong>\r\n<strong>});<\/strong><\/pre>\n<h1>Lazy Collection\u306e\u30e1\u30bd\u30c3\u30c9<\/h1>\n<h2>eager()<\/h2>\n<p>Laravel 6.1\u304b\u3089\u8ffd\u52a0\u3055\u308c\u305f<code>eager()<\/code>\u30e1\u30bd\u30c3\u30c9\u3092\u4f7f\u3048\u3070<code>cursor()<\/code>\u3092\u4f7f\u3063\u305fSQL\u5b9f\u884c\u3092\u300c\u7701\u30a8\u30cd\u5316\u300d\u3059\u308b\u3053\u3068\u304c\u3067\u304d\u307e\u3059\u3002<\/p>\n<p>\u4f8b\u3048\u3070\u3001\u4ee5\u4e0b\u306e\u3088\u3046\u306a\u5834\u5408\u3092\u898b\u3066\u307f\u307e\u3057\u3087\u3046\u3002<\/p>\n<pre><strong>\\DB::listen(function($q) {<\/strong>\r\n\r\n<strong>    dump($q-&gt;sql); \/\/ \u3053\u3053\u3067\u5b9f\u884c\u3055\u308c\u305fSQL\u6587\u3092\u8868\u793a\u3059\u308b<\/strong>\r\n\r\n<strong>});<\/strong>\r\n\r\n<strong>$users = \\App\\User::cursor();<\/strong>\r\n\r\n<strong>$count = $users-&gt;count();<\/strong>\r\n<strong>$user_array = $users-&gt;toArray();<\/strong>\r\n<strong>$user_json = $users-&gt;toJson();<\/strong><\/pre>\n<p>\u5b9f\u306f\u3001\u3053\u308c\u3092\u5b9f\u884c\u3059\u308b\u3068\u540c\u3058SQL\u6587\u304c\uff13\u56de\u3082\u5b9f\u884c\u3055\u308c\u3066\u3057\u307e\u3044\u307e\u3059\u3002<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-4702\" src=\"https:\/\/blog.capilano-fw.com\/wp-content\/uploads\/2019\/09\/laravel_60_auth_6.png\" alt=\"\" width=\"175\" height=\"120\" \/><\/p>\n<p>\u3053\u308c\u3092\uff11\u56de\u3067\u6e08\u307e\u305b\u308b\u305f\u3081\u306b\u7528\u610f\u3055\u308c\u305f\u306e\u304c<code>eager()<\/code>\u3067\u3059\u3002<br \/>\n<code>count()<\/code>\u3001<code>toArray()<\/code>\u3001<code>toJson()<\/code>\u304c\u547c\u3070\u308c\u308b\u524d\u306b\u4ee5\u4e0b\u306e\u3088\u3046\u306b\u3057\u307e\u3059\u3002<\/p>\n<pre>$users = \\App\\User::cursor();\r\n<strong>$users = $users-&gt;eager(); \/\/ \u2190\u3053\u3053\u3092\u8ffd\u52a0<\/strong>\r\n\r\n$count = $users-&gt;count();\r\n$user_array = $users-&gt;toArray();\r\n$user_json = $users-&gt;toJson();<\/pre>\n<p>\u203b \u3053\u306e\u30e1\u30bd\u30c3\u30c9\u306f\u30012019.10.03\u306b\u8ffd\u8a18\u3057\u307e\u3057\u305f\u3002<\/p>\n<h2>tapEach()<\/h2>\n<p><code>tapEach()<\/code>\u3092\u4f7f\u3048\u3070\u3001<code>Lazy Collection<\/code>\u304c\u5fc5\u8981\u306a\u30c7\u30fc\u30bf\u306e\u307f\u30b3\u30fc\u30eb\u30d0\u30c3\u30af\u95a2\u6570\u3092\u5b9f\u884c\u3055\u305b\u308b\u3053\u3068\u304c\u51fa\u6765\u307e\u3059\u3002<\/p>\n<p>\u307e\u305a\u3001\u901a\u5e38\u306e<code>each()<\/code>\u30e1\u30bd\u30c3\u30c9\u3092\u4f7f\u3063\u305f\u5834\u5408\u306f\u3001\u4ee5\u4e0b\u306e\u3088\u3046\u306b\u56de\u6570\u5206\u3059\u3079\u3066\u304c\u5b9f\u884c\u3055\u308c\u308b\u7570\u306a\u308a\u307e\u3059\u3002\uff08<code>times()<\/code>\u306f\u305d\u306e\u56de\u6570\u5206\u3060\u3051\u5b9f\u884c\u3059\u308b\u30e1\u30bd\u30c3\u30c9\u3067\u3059\uff09<\/p>\n<pre><strong>LazyCollection::times(10)-&gt;each(function($value){<\/strong>\r\n\r\n<strong>    \/\/ \u3053\u3053\u306f\u300110\u56de\u547c\u3070\u308c\u308b<\/strong>\r\n\r\n<strong>})<\/strong>\r\n<strong>-&gt;take(3)<\/strong>\r\n<strong>-&gt;all();<\/strong><\/pre>\n<p>\u305d\u308c\u304c\u3001<code>tapEach()<\/code>\u3092\u4f7f\u3046\u3068\u3001\u5fc5\u8981\u306a\u30c7\u30fc\u30bf\uff13\u3064\u5206\u3060\u3051\u5b9f\u884c\u3055\u308c\u308b\u3053\u3068\u306b\u306a\u308a\u307e\u3059\u3002<\/p>\n<pre><strong>LazyCollection::times(10)-&gt;tapEach(function($value) {<\/strong>\r\n\r\n<strong>    \/\/ \u3053\u3053\u306f\u30013\u56de\u3060\u3051\u547c\u3070\u308c\u308b<\/strong>\r\n\r\n<strong>})<\/strong>\r\n<strong>-&gt;take(3) \/\/ \u3053\u3053\u306e\u56de\u6570\u5206 tapEach() \u3092\u5b9f\u884c\u3059\u308b\u3053\u3068\u306b\u306a\u308b<\/strong>\r\n<strong>-&gt;all();<\/strong><\/pre>\n<h1>\u3061\u306a\u307f\u306b &#8211; 1<\/h1>\n<p>\u3061\u306a\u307f\u306b\u3001<code>Lazy Collection<\/code>\u306f\u30b3\u30ec\u30af\u30b7\u30e7\u30f3\u306e\u69cb\u9020\u81ea\u4f53\u304c\u5909\u5316\u3059\u308b\u4ee5\u4e0b\u306e\u3088\u3046\u306a\u30e1\u30bd\u30c3\u30c9\u306f\u4f7f\u3048\u306a\u3044\u3088\u3046\u3067\u3059\u3002<\/p>\n<ul>\n<li>shift<\/li>\n<li>pop<\/li>\n<li>prepend<\/li>\n<\/ul>\n<h1>\u3061\u306a\u307f\u306b &#8211; 2<\/h1>\n<p>\u82f1\u8a9e\u3067\u3059\u304c\u3001<code>Laracasts<\/code>\u306e<a href=\"https:\/\/laracasts.com\/series\/whats-new-in-laravel-6\/episodes\/1\" target=\"_blank\" rel=\"noopener noreferrer\">\u3053\u3061\u3089\u306e\u30da\u30fc\u30b8<\/a>\u3067<code>Lazy Collection<\/code>\u306e\u52d5\u753b\u304c\u516c\u958b\u3055\u308c\u3066\u3044\u307e\u3059\u3002\u73fe\u5728\u3001\u7121\u6599\u3067\u898b\u3089\u308c\u307e\u3059\u306e\u3067\u8208\u5473\u306e\u3042\u308b\u65b9\u306f\u305c\u3072\u3054\u89a7\u306b\u306a\u3063\u3066\u304f\u3060\u3055\u3044\u3002<\/p>\n<h1>\u304a\u308f\u308a\u306b<\/h1>\n<p>\u3068\u3044\u3046\u3053\u3068\u3067\u3001\u4eca\u56de\u306f<code>Laravel<\/code> 6.x\u306e\u65b0\u6a5f\u80fd<code>Lazy Collection<\/code>\u3092\u7d39\u4ecb\u3057\u307e\u3057\u305f\u3002<\/p>\n<p>\u305d\u308c\u307b\u3069\u30c7\u30fc\u30bf\u91cf\u304c\u591a\u304f\u306a\u3044\u30b5\u30a4\u30c8\u3060\u3068\u3042\u307e\u308a\u6069\u6075\u306f\u5c11\u306a\u3044\u304b\u3082\u3057\u308c\u307e\u305b\u3093\u304c\u3001\u4f55\u5e74\u3082\u5229\u7528\u3057\u3066\u30c7\u30fc\u30bf\u304c\u5927\u91cf\u306b\u3042\u308b\u3088\u3046\u306a\u30b5\u30a4\u30c8\u3067\u306f\u4f7f\u3044\u65b9\u306b\u3088\u3063\u3066\u306f\u5927\u5e45\u306b\u30e1\u30e2\u30ea\u4f7f\u7528\u91cf\u3092\u6e1b\u3089\u3059\u3053\u3068\u304c\u3067\u304d\u308b\u3068\u601d\u3044\u307e\u3059\u3002<\/p>\n<p>\u306a\u304a\u3001\u4eca\u56de<code>Lazy Collection<\/code>\u3092\u52c9\u5f37\u3059\u308b\u306b\u3042\u305f\u3063\u3066\u521d\u3081\u3066<code>Generators<\/code>\u3068\u3044\u3046\u6a5f\u80fd\u3092\u77e5\u308b\u3053\u3068\u306b\u306a\u308a\u307e\u3057\u305f\u3002\u305d\u308c\u307b\u3069\u5927\u91cf\u306e\u30c7\u30fc\u30bf\u3092\u6271\u3046\u6848\u4ef6\u306b\u643a\u308f\u3063\u3066\u3044\u306a\u304b\u3063\u305f\u3053\u3068\u306b\u52a0\u3048\u3066\u6700\u8fd1\u306e\u30b3\u30f3\u30d4\u30e5\u30fc\u30bf\u306f\u3068\u3066\u3082\u9ad8\u6027\u80fd\u306b\u306a\u3063\u305f\u3053\u3068\u304c\u539f\u56e0\u3067\u3057\u3087\u3046\u304c\u3001\u3053\u3093\u306a\u4fbf\u5229\u306a\u6a5f\u80fd\u304c\u3042\u308b\u3068\u306f\u77e5\u308a\u307e\u305b\u3093\u3067\u3057\u305f\u3002<\/p>\n<p>\u3084\u3063\u3071\u308a\u30d7\u30ed\u30b0\u30e9\u30e0\u306e\u5965\u306f\u6df1\u3044\u3067\u3059\u306d\u3002<br \/>\n\u3053\u308c\u304b\u3089\u3082\u7cbe\u9032\u3057\u3066\u3044\u304d\u305f\u3044\u3068\u601d\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\u5148\u65e5Laravel\u306e\u30d0\u30fc\u30b8\u30e7\u30f36.x\u304c\u30ea\u30ea\u30fc\u30b9\u3055\u308c\u305f\u306e\u3067\u524d\u56de\u8a18\u4e8b\u3067\u306f\uff08\u5b9f\u88c5\u65b9\u6cd5\u304c\u5909\u66f4\u306b\u306a\u3063\u3066\u3044\u305f\uff09\u30ed\u30b0\u30a4\u30f3\u6a5f\u80fd\u306e\u4f7f\u3044\u65b9\u3092\u3054\u7d39\u4ecb\u3057\u307e\u3057\u305f\u3002 \u305d\u3057\u3066\u3001\u65b0\u30d0 &hellip; <\/p>\n<p class=\"link-more\"><a href=\"https:\/\/blog.capilano-fw.com\/?p=4590\" class=\"more-link\"><span class=\"screen-reader-text\">&#8220;Laravel 6.x\u306eLazy Collection\uff08\u65b0\u6a5f\u80fd\uff09\u3092\u7d39\u4ecb\u3057\u307e\u3059&#8221; \u306e<\/span>\u7d9a\u304d\u3092\u8aad\u3080<\/a><\/p>\n","protected":false},"author":1,"featured_media":4594,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3],"tags":[65,82],"class_list":["post-4590","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-laravel","tag-65","tag-82"],"_links":{"self":[{"href":"https:\/\/blog.capilano-fw.com\/index.php?rest_route=\/wp\/v2\/posts\/4590","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=4590"}],"version-history":[{"count":6,"href":"https:\/\/blog.capilano-fw.com\/index.php?rest_route=\/wp\/v2\/posts\/4590\/revisions"}],"predecessor-version":[{"id":4701,"href":"https:\/\/blog.capilano-fw.com\/index.php?rest_route=\/wp\/v2\/posts\/4590\/revisions\/4701"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/blog.capilano-fw.com\/index.php?rest_route=\/wp\/v2\/media\/4594"}],"wp:attachment":[{"href":"https:\/\/blog.capilano-fw.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=4590"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.capilano-fw.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=4590"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.capilano-fw.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=4590"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}