{"id":504,"date":"2018-05-15T03:37:34","date_gmt":"2018-05-14T18:37:34","guid":{"rendered":"https:\/\/blog.capilano-fw.com\/?p=504"},"modified":"2020-03-10T06:14:49","modified_gmt":"2020-03-09T21:14:49","slug":"%e5%85%a812%e7%a8%ae%e9%a1%9e%ef%bc%81laravel5-6%e3%81%ae%e3%82%ad%e3%83%a3%e3%82%b9%e3%83%88%ef%bc%88%e5%9e%8b%e5%a4%89%e6%8f%9b%ef%bc%89%e5%ae%9f%e4%be%8b","status":"publish","type":"post","link":"https:\/\/blog.capilano-fw.com\/?p=504","title":{"rendered":"\u516813\u7a2e\u985e\uff01Laravel\u306e\u30ad\u30e3\u30b9\u30c8\uff08\u578b\u5909\u63db\uff09\u5b9f\u4f8b"},"content":{"rendered":"<p>\u3055\u3066\u3055\u3066\u3001Laravel\u3067\u958b\u767a\u3092\u3057\u3066\u3044\u308b\u3068\u304d\u3001\u3068\u3066\u3082\u91cd\u5b9d\u3059\u308b\u6a5f\u80fd\u306e\u4e00\u3064\u306b\u3001\u300c<strong>\u30c7\u30fc\u30bf\u306e\u30ad\u30e3\u30b9\u30c8\uff08\u578b\u5909\u63db\uff09<\/strong>\u300d\u304c\u3042\u308a\u307e\u3059\u3002<\/p>\n<p>\u30ad\u30e3\u30b9\u30c8\u3068\u3044\u3046\u306e\u306f\u3001\u4f8b\u3048\u3070\u6587\u5b57\u5217\u3067\u767b\u9332\u3057\u305f\u30c7\u30fc\u30bf\u3092\u6570\u5b57\u306b\u5909\u63db\u3059\u308b\u6a5f\u80fd\u3067\u3001\u901a\u5e38\u306ePHP\u3060\u3068\u3001<\/p>\n<pre><strong>$text = (string) 123;<\/strong><\/pre>\n<p>\u3068\u3059\u308b\u3053\u3068\u3067\u578b\u5909\u63db\u3067\u304d\u308b\u308f\u3051\u3067\u3059\u3051\u3069\u3001\u306a\u3093\u3068<code>Laravel<\/code>\u306e\u5834\u5408\u306f\u30e2\u30c7\u30eb\u306e<code>$casts<\/code>\u306b\u6307\u5b9a\u3059\u308b\u3060\u3051\u3067\u3001<\/p>\n<ul>\n<li><strong>\u4fdd\u5b58\u3059\u308b\u6642<\/strong><\/li>\n<li><strong>\u53d6\u5f97\u3059\u308b\u6642<\/strong><\/li>\n<\/ul>\n<p>\u306e\u4e21\u65b9\u3067\u3001\u81ea\u52d5\u7684\u306b\u578b\u5909\u63db\u3092\u3057\u3066\u304f\u308c\u3066\u3068\u3066\u3082\u4fbf\u5229\u3067\u3059\u3002\uff08\u305d\u3057\u3066\u3001\u5909\u6570\u306e\u578b\u3060\u3051\u3058\u3083\u306a\u304f\u914d\u5217\u3084\u30b3\u30ec\u30af\u30b7\u30e7\u30f3\u306b\u5909\u63db\u3082\u3057\u308c\u304f\u308c\u307e\u3059\u3002\uff09<\/p>\n<p>\u4f8b\u3048\u3070\u3053\u3093\u306a\u30ab\u30f3\u30b8\u3067\u3059\u3002<\/p>\n<pre>class Item extends Model\r\n{\r\n    protected $casts = [\r\n        <strong>'activated' =&gt; 'boolean'<\/strong>\r\n    ];\r\n}<\/pre>\n<p>\u3068\u3046\u3053\u3068\u3067\u3001\u4eca\u56de\u306f\u3053\u306e<strong>\u300c\u30ad\u30e3\u30b9\u30c8\u300d\u306e\u5b9f\u4f8b<\/strong>\u3092\u307e\u3068\u3081\u3066\u307f\u307e\u3057\u305f\u3002<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-6068 aligncenter\" src=\"https:\/\/blog.capilano-fw.com\/wp-content\/uploads\/2020\/03\/no_12.png\" alt=\"\" width=\"226\" height=\"320\" srcset=\"https:\/\/blog.capilano-fw.com\/wp-content\/uploads\/2020\/03\/no_12.png 226w, https:\/\/blog.capilano-fw.com\/wp-content\/uploads\/2020\/03\/no_12-212x300.png 212w\" sizes=\"auto, (max-width: 226px) 100vw, 226px\" \/><\/p>\n<p><strong>\u958b\u767a\u74b0\u5883\uff1a<\/strong> Laravel 5.4\u3001Laravel 7.x<\/p>\n<p>\u3010\u8ffd\u8a18\uff1a2020.03.10\u3011\u3053\u306e\u8a18\u4e8b\u306f\u3001\u516c\u958b\u5f53\u6642<code>Laravel 5.4<\/code>\u5411\u3051\u306b\u4f5c\u6210\u3055\u308c\u3066\u3044\u305f\u3082\u306e\u306b\u52a0\u7b46\u4fee\u6b63\u3057\u305f\u3082\u306e\u3067\u3059\u3002<\/p>\n<h1>Integer<\/h1>\n<p>\u6570\u5b57\u3078\u5909\u63db\u3059\u308b\u30ad\u30e3\u30b9\u30c8\u3067\u3059\u3002<\/p>\n<p>\u3082\u3057\u30c7\u30fc\u30bf\u304c\u6587\u5b57\u5217\u3067\u3082\u3053\u306e\u30ad\u30e3\u30b9\u30c8\u3092\u4f7f\u3046\u3053\u3068\u3067\u578b\u3092\u6570\u5b57\u306b\u3059\u308b\u3053\u3068\u304c\u3067\u304d\u307e\u3059\u3002<\/p>\n<pre>protected $casts = [\r\n    <strong>'field_name' =&gt; 'integer'<\/strong>\r\n];<\/pre>\n<p>\u4f8b\u3048\u3070\u3001&#8221;12345&#8243;\u3068\u3044\u3046\u6587\u5b57\u5217\u304c\u30ad\u30e3\u30b9\u30c8\u3055\u308c\u308b\u3068\u3001\u6570\u5b57\u306e\u300c12345\u300d\u306b\u5909\u63db\u3055\u308c\u307e\u3059\u3002<\/p>\n<pre>var_dump($item-&gt;field_name);   \/\/ <strong>int(12345)<\/strong><\/pre>\n<p>\u3061\u306a\u307f\u306b\u3001\u3082\u3057\u6587\u5b57\u5217\u3067 &#8220;1234.555&#8221;\u306a\u3069\u306e\u3088\u3046\u306b\u5c0f\u6570\u70b9\u304c\u3064\u3044\u3066\u3044\u308b\u5834\u5408\u306f\u3001\u5c0f\u6570\u70b9\u4ee5\u4e0b\u304c\u524a\u9664\u3055\u308c\u3066\u300c1234\u300d\u3068\u306a\u308a\u307e\u3059\u3002\uff08\u56db\u6368\u4e94\u5165\u3067\u306f\u306a\u3044\uff09<\/p>\n<p>\u307e\u305f\u3001\u300c150\u500b\u300d\u300c150\u518650\u92ad\u300d\u3068\u3044\u3046\u6587\u5b57\u5217\u306e\u5834\u5408\u306f\u3001\u6700\u521d\u306b\u51fa\u3066\u304f\u308b\u6570\u5b57\u300c150\u300d\u304c\u53d6\u5f97\u3067\u304d\u307e\u3059\u3002<\/p>\n<h1>float(real, double)<\/h1>\n<p>\u30c7\u30fc\u30bf\u3092\u5c0f\u6570\u70b9\u3064\u304d\u306e\u6570\u5b57\u306b\u5909\u63db\u3059\u308b\u30ad\u30e3\u30b9\u30c8\u3002\uff08<code>real<\/code>\u3068<code>double<\/code>\u306f\u540c\u3058\u3082\u306e\u306a\u306e\u3067\u3001<code>float<\/code>\u3067\u7d71\u4e00\u3057\u307e\u3059\uff09<\/p>\n<pre>protected $casts = [\r\n    'field_name' =&gt; '<strong>float<\/strong>' \/\/ \u3082\u3057\u304f\u306f\u3001real \u304b double\r\n];<\/pre>\n<p>\u3082\u3057&#8221;123.456&#8243;\u3068\u3044\u3046\u6587\u5b57\u5217\u304cfloat\u30ad\u30e3\u30b9\u30c8\u3055\u308c\u308b\u3068\u3001\u300c123.456\u300d\u3068\u3044\u3046\u5c0f\u6570\u70b9\u4ed8\u304d\u306e\u6570\u5b57\u306b\u306a\u308a\u307e\u3059\u3002<\/p>\n<pre>var_dump($item-&gt;field_name);   \/\/ <strong>float(123.456)<\/strong><\/pre>\n<p>integer\u30ad\u30e3\u30b9\u30c8\u3068\u540c\u3058\u304f\u3001&#8221;123.456\u56de&#8221;\u3084&#8221;123.456\u518650\u92ad&#8221;\u306e\u5834\u5408\u306f\u3001\u300c123.456\u300d\u3068\u3044\u3046\u5c0f\u6570\u70b9\u4ed8\u304d\u306e\u6570\u5b57\u306b\u306a\u308a\u307e\u3059\u3002<\/p>\n<h1>String<\/h1>\n<p>\u30c7\u30fc\u30bf\u3092\u6587\u5b57\u5217\u5316\u3059\u308b\u30ad\u30e3\u30b9\u30c8\u3067\u3059\u3002<\/p>\n<pre>protected $casts = [\r\n    'field_name' =&gt; '<strong>string<\/strong>'\r\n];<\/pre>\n<p>\u4f8b\u3048\u3070\u3001\u300c12345\u300d\u3068\u3044\u3046\u6570\u5b57\u304c\u30ad\u30e3\u30b9\u30c8\u3055\u308c\u308b\u3068&#8221;12345&#8243;\u3068\u3044\u3046\u6587\u5b57\u5217\u306b\u5909\u63db\u3055\u308c\u307e\u3059\u3002<\/p>\n<pre>var_dump($item-&gt;field_name);   \/\/ <strong>string(5) \"12345\"<\/strong><\/pre>\n<p>\u3061\u306a\u307f\u306b\u3001float\u578b\u306e\u5834\u5408\u3082\u540c\u69d8\u306b\u6587\u5b57\u5217\u306b\u5909\u63db\u3055\u308c\u307e\u3059\u3002<\/p>\n<pre>var_dump($item-&gt;field_name);   \/\/ <strong>string(7) \"12345.6\"<\/strong><\/pre>\n<h1>boolean<\/h1>\n<p>\u30c7\u30fc\u30bf\u3092<strong>true<\/strong>\u3082\u3057\u304f\u306f\u3001<strong>false<\/strong>\u306b\u5909\u63db\u3059\u308b\u30ad\u30e3\u30b9\u30c8\u3067\u3059\u3002<\/p>\n<pre>protected $casts = [\r\n    'field_name' =&gt; '<strong>boolean<\/strong>'\r\n];<\/pre>\n<p>\u4f8b\u3048\u3070\u3001\u30c7\u30fc\u30bf\u304c\u300c1\u300d\u3068\u3044\u3046\u6570\u5b57\u306e\u5834\u5408\u306f\u4ee5\u4e0b\u306e\u3088\u3046\u306b<strong>true<\/strong>\u306b\u306a\u308a\u3001\u300c0\u300d\u306e\u5834\u5408\u306f<strong>false<\/strong>\u306b\u306a\u308a\u307e\u3059\u3002<\/p>\n<pre>var_dump($item-&gt;field_name);   \/\/ <strong>bool(true)<\/strong> \u3082\u3057\u304f\u306f <strong>bool(false)<\/strong><\/pre>\n<p>\u3061\u306a\u307f\u306b\u3001true\u306b\u306a\u308b\u30d1\u30bf\u30fc\u30f3\u3001false\u306b\u306a\u308b\u30d1\u30bf\u30fc\u30f3\u306f\u4ee5\u4e0b\u306e\u3068\u304a\u308a\u3067\u3059\u3002<\/p>\n<ul>\n<li>true\u3000\u30fb\u30fb\u30fb\u30001, 2, <strong>-1<\/strong><\/li>\n<li>false\u3000\u30fb\u30fb\u30fb\u30000, \u6587\u5b57\u5217\u306e\u7a7a\u767d<\/li>\n<\/ul>\n<p>\u203b\u30de\u30a4\u30ca\u30b9\uff11\u306f\u76f4\u611f\u7684\u306b<strong>false<\/strong>\u306b\u306a\u308a\u305d\u3046\u3067\u3059\u3051\u3069\u3001\u5b9f\u969b\u306b\u306f<strong>true<\/strong>\u306b\u306a\u3063\u3066\u3057\u307e\u3046\u306e\u3067\u6ce8\u610f\u304c\u5fc5\u8981\u3067\u3059\u306d\u3002<\/p>\n<h1>object<\/h1>\n<p>PHP\u306e\u300c<a href=\"http:\/\/php.net\/manual\/ja\/language.types.object.php#language.types.object.casting\" target=\"_blank\" rel=\"noopener noreferrer\">stdClass<\/a>\u300d\u306b\u5909\u63db\u3055\u308c\u308b\u30ad\u30e3\u30b9\u30c8\u3067\u3059\u3002<\/p>\n<pre>protected $casts = [\r\n    'colors' =&gt; '<strong>object<\/strong>'\r\n];<\/pre>\n<p>\u4f8b\u3048\u3070\u3001\u4ee5\u4e0b\u306e\u3088\u3046\u306b\u9023\u60f3\u914d\u5217\u3092\u4fdd\u5b58\u3059\u308b\u3068\u3001json\u5316\u3055\u308c\u3066\u30c7\u30fc\u30bf\u30d9\u30fc\u30b9\u306b\u683c\u7d0d\u3055\u308c\u307e\u3059\u3002\uff08\u9023\u60f3\u914d\u5217\u3001\u591a\u6b21\u5143\u914d\u5217\u3067\u3082OK\uff09<\/p>\n<pre>\\App\\Item::create([\r\n    'colors' =&gt; <strong>[<\/strong>\r\n<strong>        'red' =&gt; '\u8d64', <\/strong>\r\n<strong>        'blue' =&gt; '\u9752', <\/strong>\r\n<strong>        'yellow' =&gt; '\u9ec4\u8272'<\/strong>\r\n<strong>    ]<\/strong>\r\n]);<\/pre>\n<p>\u2193\u2193\u2193DB\u5185<\/p>\n<pre>{\"red\":\"\\u8d64\",\"blue\":\"\\u9752\",\"yellow\":\"\\u9ec4\\u8272\"}<\/pre>\n<p>\u305d\u3057\u3066\u3001\u30c7\u30fc\u30bf\u53d6\u5f97\u3057\u305f\u5834\u5408\u306f\u3001\u4ee5\u4e0b\u306e\u3088\u3046\u306b<strong>stdClass<\/strong>\u3068\u3057\u3066\u5229\u7528\u3067\u304d\u307e\u3059\u3002<\/p>\n<pre>var_dump($item-&gt;colors-&gt;<strong>red<\/strong>);   \/\/ <strong>string(3) \"\u8d64\"<\/strong>\r\nvar_dump($item-&gt;colors-&gt;<strong>blue<\/strong>);   \/\/ <strong>string(3) \"\u9752\"<\/strong>\r\nvar_dump($item-&gt;colors-&gt;<strong>yellow<\/strong>);   \/\/ <strong>string(6) \"\u9ec4\u8272\"<\/strong><\/pre>\n<p>\u3061\u306a\u307f\u306b\u3001stdClass\u306fforeach()\u30eb\u30fc\u30d7\u3067\u4f7f\u3046\u3053\u3068\u3082\u3067\u304d\u307e\u3059\u3002<\/p>\n<pre><strong>foreach<\/strong> ($item-&gt;colors as $key =&gt; $value) {\r\n\r\n    echo $key .': '. $value .\"\\n\";\r\n\r\n}\r\n\r\n\/*** \u51fa\u529b\r\n\r\nred: \u8d64\r\nblue: \u9752\r\nyellow: \u9ec4\u8272\r\n\r\n***\/<\/pre>\n<h1>array<\/h1>\n<p>\u30c7\u30fc\u30bf\u3092\u914d\u5217\u5316\u3057\u3066\u304f\u308c\u308b\u30ad\u30e3\u30b9\u30c8\u3067\u3059\u3002<\/p>\n<pre>protected $casts = [\r\n    'colors' =&gt; '<strong>array<\/strong>'\r\n];<\/pre>\n<p>\u914d\u5217\u3092\u4fdd\u5b58\u3059\u308b\u3068\u3001\u30c7\u30fc\u30bf\u304cjson\u5316\u3055\u308c\u307e\u3059\u3002\uff08\u9023\u60f3\u914d\u5217\u3001\u591a\u6b21\u5143\u914d\u5217\u3067\u3082OK\uff09<\/p>\n<pre>\\App\\Item::create([\r\n    'colors' =&gt; <strong>[<\/strong>\r\n<strong>        '\u8d64',<\/strong>\r\n<strong>        '\u9752',<\/strong>\r\n<strong>        '\u9ec4\u8272'<\/strong>\r\n<strong>    ]<\/strong>\r\n]);<\/pre>\n<p>\u2193\u2193\u2193DB\u5185<\/p>\n<pre><strong>[\"\\u8d64\",\"\\u9752\",\"\\u9ec4\\u8272\"]<\/strong><\/pre>\n<p>\u53d6\u5f97\u3057\u305f\u3068\u3053\u308d\u3002<\/p>\n<pre>var_dump($item-&gt;colors);\r\n\r\n\/*** \u51fa\u529b\r\n\r\n    array(3) {\r\n      [0]=&gt;\r\n      string(3) \"<strong>\u8d64<\/strong>\"\r\n      [1]=&gt;\r\n      string(3) \"<strong>\u9752<\/strong>\"\r\n      [2]=&gt;\r\n      string(6) \"<strong>\u9ec4\u8272<\/strong>\"\r\n    }\r\n \r\n***\/<\/pre>\n<h1>collection<\/h1>\n<p>Laravel\u306e\u300c\u30b3\u30ec\u30af\u30b7\u30e7\u30f3\u300d\u306b\u5909\u63db\u3057\u3066\u304f\u308c\u308b\u30ad\u30e3\u30b9\u30c8\u3067\u3059\u3002<br \/>\n\u3053\u308c\u3082\u300carray\u300d\u3084\u300cobject\u300d\u306a\u3069\u3068\u540c\u3058\u304f\u4fdd\u5b58\u3059\u308b\u3068json\u5316\u3055\u308c\u307e\u3059\u3002<\/p>\n<pre>protected $casts = [\r\n    'colors' =&gt; '<strong>collection<\/strong>'\r\n];<\/pre>\n<p>\u30c7\u30fc\u30bf\u53d6\u5f97\u3059\u308b\u3068\u3053\u3046\u306a\u308a\u307e\u3059\u3002<\/p>\n<pre>var_dump($item-&gt;colors);\r\n\r\n\/*** \u51fa\u529b\r\n\r\n    object(Illuminate\\Support\\Collection)#699 (1) {\r\n        [\"items\":protected]=&gt;\r\n        array(3) {\r\n            [\"red\"]=&gt;\r\n            string(3) \"<strong>\u8d64<\/strong>\"\r\n            [\"blue\"]=&gt;\r\n            string(3) \"<strong>\u9752<\/strong>\"\r\n            [\"yellow\"]=&gt;\r\n            string(6) \"<strong>\u9ec4\u8272<\/strong>\"\r\n        }\r\n    }\r\n\r\n***\/<\/pre>\n<h1>date, datetime<\/h1>\n<p><a href=\"https:\/\/carbon.nesbot.com\/docs\/\" target=\"_blank\" rel=\"noopener noreferrer\">Carbon<\/a>\u30a4\u30f3\u30b9\u30bf\u30f3\u30b9\u306b\u5909\u63db\u3057\u3066\u304f\u308c\u308b\u30ad\u30e3\u30b9\u30c8\u3067\u3059\u3002<\/p>\n<pre>protected $casts = [\r\n    'start_date' =&gt; '<strong>date<\/strong>',\r\n    'start_datetime' =&gt; '<strong>datetime<\/strong>'\r\n];<\/pre>\n<pre>var_dump($item-&gt;start_date);\r\n\r\n\/*** \u51fa\u529b\r\n\r\nobject(Illuminate\\Support\\Carbon)#700 (3) {\r\n  [\"date\"]=&gt;\r\n  string(26) \"<strong>2018-05-15 00:00:00.000000<\/strong>\"\r\n  [\"timezone_type\"]=&gt;\r\n  int(3)\r\n  [\"timezone\"]=&gt;\r\n  string(10) \"Asia\/Tokyo\"\r\n}\r\n\r\n***\/<\/pre>\n<p>date\u3068datetime\u306e\u9055\u3044\u306f\u3001<strong>date\u306e\u5834\u5408\u3001\u6642\u9593\uff0f\u5206\uff0f\u79d2\u304c\u5168\u3066\u30bc\u30ed\u306b\u306a\u308b\u3053\u3068\u3067\u3059\u3002<\/strong>\u3053\u308c\u306f\u3001\u30c7\u30fc\u30bf\u304c\u300c2018-05-15 01:15:30\u300d\u3068\u3044\u3046\u98a8\u306b\u6642\u5206\u79d2\u304c\u6307\u5b9a\u3055\u308c\u3066\u3044\u3066\u3082\u540c\u3058\u3067\u3059\u3002<\/p>\n<pre>echo $music-&gt;start_date-&gt;hour; \/\/ 0\r\necho $music-&gt;start_date-&gt;minute; \/\/ 0\r\necho $music-&gt;start_date-&gt;second; \/\/ 0<\/pre>\n<p>\u3061\u306a\u307f\u306b\u3001\u30c7\u30fc\u30bf\u304c\u7a7a\u767d\u306e\u5834\u5408\u306f\u300c<strong>Data missing<\/strong>\u300d\u3068\u3044\u3046\u30a8\u30e9\u30fc\u304c\u767a\u751f\u3057\u307e\u3059\u3002<\/p>\n<p>\u306a\u304a\u3001\u4ee5\u4e0b\u306e\u3088\u3046\u306b\u65e5\u4ed8\u306e\u30d5\u30a9\u30fc\u30de\u30c3\u30c8\u3092\u6307\u5b9a\u3059\u308b\u3053\u3068\u304c\u3067\u304d\u307e\u3059\u3002<\/p>\n<pre>protected $casts = [\r\n    'start_date' =&gt; 'datetime:<strong>Y\u5e74m\u6708d\u65e5<\/strong>'\r\n];<\/pre>\n<p>\u8a73\u3057\u304f\u306f\u3001\u904e\u53bb\u8a18\u4e8b\u300c<a href=\"https:\/\/blog.capilano-fw.com\/?p=277\">Laravel5.6\u306e\u6c17\u306b\u306a\u308b\uff13\u3064\u306e\u65b0\u6a5f\u80fd<\/a>\u300d\u306e\u300c<strong>\uff12\uff0e\u30e2\u30c7\u30eb\u5185\u306e\u65e5\u4ed8\u30fb\u81ea\u52d5\u5909\u63db<\/strong>\u300d\u3092\u53c2\u7167\u3057\u3066\u304f\u3060\u3055\u3044\u3002<\/p>\n<h1>timestamp<\/h1>\n<p>\u30c7\u30fc\u30bf\u3092UNIX\u6642\u9593\u306b\u5909\u63db\u3057\u3066\u304f\u308c\u308b\u30ad\u30e3\u30b9\u30c8\u3067\u3059\u3002\u3064\u307e\u308aint\u578b\u306b\u306a\u308a\u307e\u3059\u3002<\/p>\n<pre>protected $casts = [\r\n    'created_at' =&gt; '<strong>timestamp<\/strong>'\r\n];<\/pre>\n<pre>var_dump($item-&gt;created_at); \/\/ <strong>int(1526314530)<\/strong><\/pre>\n<h1>\u30ab\u30b9\u30bf\u30e0\u30ad\u30e3\u30b9\u30c8\u6a5f\u80fd<\/h1>\n<p><code>Laravel 7.x<\/code>\u304b\u3089\u306f\u3001\u72ec\u81ea\u30ad\u30e3\u30b9\u30c8\u3092\u4f5c\u308b\u3053\u3068\u304c\u3067\u304d\u308b\u300c\u30ab\u30b9\u30bf\u30e0\u30ad\u30e3\u30b9\u30c8\u6a5f\u80fd\u300d\u304c\u8ffd\u52a0\u306b\u306a\u3063\u3066\u3044\u307e\u3059\u3002\u8a73\u3057\u304f\u306f\u4ee5\u4e0b\u306eURL\u3092\u3054\u89a7\u304f\u3060\u3055\u3044\u3002<\/p>\n<p><a href=\"https:\/\/blog.capilano-fw.com\/?p=6053\">Laravel 7.x\u306e\u65b0\u3057\u3044\u30ad\u30e3\u30b9\u30c8\uff08\u30c7\u30fc\u30bf\u5909\u63db\uff09\u6a5f\u80fd\uff01\u5b9f\u4f8b<\/a><\/p>\n<h1>\u30af\u30a8\u30ea\u30fc\u7528\u306e\u6642\u9593\u30ad\u30e3\u30b9\u30c8<\/h1>\n<p><em>\u3010\u6ce8\u610f\u3011\u3053\u306e\u6a5f\u80fd\u306f\u3001<code>Laravel 7.x<\/code>\u4ee5\u964d\u3067\u6709\u52b9\u3067\u3059\u3002<\/em><\/p>\n<p>\u4f8b\u3048\u3070\u3001<code>users<\/code>\u3068<code>posts<\/code>\u304c1\uff1a\u591a\uff08hasMany\uff09\u306e\u95a2\u4fc2\u306b\u3042\u308b\u3068\u3057\u3066\u3001\u300c<strong>\u8a8d\u8a3c\u3055\u308c\u305fpost\u30c7\u30fc\u30bf\u306e\u6700\u65b0\u306e\u65e5\u4ed8\u3092\u53d6\u5f97\u3059\u308b<\/strong>\u300d\u5834\u5408\u3092\u8003\u3048\u3066\u307f\u307e\u3057\u3087\u3046\u3002<\/p>\n<p>\u30b3\u30fc\u30c9\u3068\u3057\u3066\u306f\u4ee5\u4e0b\u306e\u3088\u3046\u306b\u306a\u308a\u307e\u3059\u3002<\/p>\n<pre>$users = \\App\\User::select([\r\n    'users.id',\r\n    <strong>'verified_at' =&gt; \\App\\Post::selectRaw('MAX(verified_at)')<\/strong>\r\n<strong>        -&gt;whereColumn('user_id', 'users.id')<\/strong>\r\n])-&gt;get();<\/pre>\n<p>\u305f\u3060\u3001\u3053\u306e\u5834\u5408\u3060\u3068<code>verified_at<\/code>\u306f\u6587\u5b57\u5217\u306a\u306e\u3067\u3001\u65e5\u4ed8\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u3068\u3057\u3066\u4f7f\u3046\u5834\u5408\u306f<code>new Carbon()<\/code>\u3067\u5909\u63db\u3059\u308b\u5fc5\u8981\u304c\u3042\u308a\u3001\u3081\u3093\u3069\u3046\u3067\u3059\u3002<\/p>\n<p>\u305d\u3093\u306a\u5834\u5408\u306b\u91cd\u5b9d\u3059\u308b\u306e\u304c\u3001<code>withCasts()<\/code>\u3067\u3059\u3002<br \/>\n\u4ee5\u4e0b\u306e\u3088\u3046\u306b\u4f7f\u3046\u3053\u3068\u3067\u3001\u5143\u3005\u306f\u6587\u5b57\u5217\u3060\u3063\u305f<code>verified_at<\/code>\u3092\u65e5\u4ed8\u306b\u30ad\u30e3\u30b9\u30c8\u3059\u308b\u3053\u3068\u304c\u3067\u304d\u307e\u3059\u3002<\/p>\n<pre>$users = \\App\\User::select([\r\n    'users.id',\r\n    'verified_at' =&gt; \\App\\Post::selectRaw('MAX(verified_at)')\r\n        -&gt;whereColumn('user_id', 'users.id')\r\n])-&gt;<strong>withCasts([<\/strong>\r\n<strong>    'verified_at' =&gt; 'date' \/\/ \u65e5\u4ed8\u306b\u5909\u63db<\/strong>\r\n<strong>])<\/strong>-&gt;get();<\/pre>\n<h1>\u304a\u308f\u308a\u306b<\/h1>\n<p>\u3068\u3044\u3046\u3053\u3068\u3067\u3001\u4eca\u56de\u306f\u30ad\u30e3\u30b9\u30c8\u3092\u307e\u3068\u3081\u3066\u307f\u307e\u3057\u305f\u3002<br \/>\n\u307f\u306a\u3055\u3093\u306e\u304a\u5f79\u306b\u7acb\u3066\u305f\u3089\u5b09\u3057\u3044\u3067\u3059\uff01<\/p>\n<p>\u3067\u306f\u3067\u306f\u301c\u3002<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-417 aligncenter\" src=\"https:\/\/blog.capilano-fw.com\/wp-content\/uploads\/2018\/04\/9.png\" alt=\"\" width=\"370\" height=\"220\" srcset=\"https:\/\/blog.capilano-fw.com\/wp-content\/uploads\/2018\/04\/9.png 370w, https:\/\/blog.capilano-fw.com\/wp-content\/uploads\/2018\/04\/9-300x178.png 300w\" sizes=\"auto, (max-width: 370px) 100vw, 370px\" \/><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u3055\u3066\u3055\u3066\u3001Laravel\u3067\u958b\u767a\u3092\u3057\u3066\u3044\u308b\u3068\u304d\u3001\u3068\u3066\u3082\u91cd\u5b9d\u3059\u308b\u6a5f\u80fd\u306e\u4e00\u3064\u306b\u3001\u300c\u30c7\u30fc\u30bf\u306e\u30ad\u30e3\u30b9\u30c8\uff08\u578b\u5909\u63db\uff09\u300d\u304c\u3042\u308a\u307e\u3059\u3002 \u30ad\u30e3\u30b9\u30c8\u3068\u3044\u3046\u306e\u306f\u3001\u4f8b\u3048\u3070\u6587\u5b57\u5217\u3067\u767b\u9332\u3057\u305f &hellip; <\/p>\n<p class=\"link-more\"><a href=\"https:\/\/blog.capilano-fw.com\/?p=504\" class=\"more-link\"><span class=\"screen-reader-text\">&#8220;\u516813\u7a2e\u985e\uff01Laravel\u306e\u30ad\u30e3\u30b9\u30c8\uff08\u578b\u5909\u63db\uff09\u5b9f\u4f8b&#8221; \u306e<\/span>\u7d9a\u304d\u3092\u8aad\u3080<\/a><\/p>\n","protected":false},"author":1,"featured_media":506,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3],"tags":[66],"class_list":["post-504","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-laravel","tag-66"],"_links":{"self":[{"href":"https:\/\/blog.capilano-fw.com\/index.php?rest_route=\/wp\/v2\/posts\/504","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=504"}],"version-history":[{"count":4,"href":"https:\/\/blog.capilano-fw.com\/index.php?rest_route=\/wp\/v2\/posts\/504\/revisions"}],"predecessor-version":[{"id":6120,"href":"https:\/\/blog.capilano-fw.com\/index.php?rest_route=\/wp\/v2\/posts\/504\/revisions\/6120"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/blog.capilano-fw.com\/index.php?rest_route=\/wp\/v2\/media\/506"}],"wp:attachment":[{"href":"https:\/\/blog.capilano-fw.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=504"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.capilano-fw.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=504"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.capilano-fw.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=504"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}