{"id":665,"date":"2018-07-04T01:56:06","date_gmt":"2018-07-03T16:56:06","guid":{"rendered":"https:\/\/blog.capilano-fw.com\/?p=665"},"modified":"2018-07-05T18:58:44","modified_gmt":"2018-07-05T09:58:44","slug":"laravel%e3%83%bb%e3%83%87%e3%83%bc%e3%82%bf%e3%83%99%e3%83%bc%e3%82%b9%e3%81%8b%e3%82%89%e3%83%87%e3%83%bc%e3%82%bf%e5%8f%96%e5%be%97%e3%81%99%e3%82%8b%e5%85%a8%e5%ae%9f%e4%be%8b","status":"publish","type":"post","link":"https:\/\/blog.capilano-fw.com\/?p=665","title":{"rendered":"Laravel\u30fb\u30c7\u30fc\u30bf\u30d9\u30fc\u30b9\u304b\u3089\u30c7\u30fc\u30bf\u53d6\u5f97\u3059\u308b\u5168\u5b9f\u4f8b"},"content":{"rendered":"<p>\u3055\u3066\u3055\u3066\u3001Laravel\u306e\u63d0\u4f9b\u3059\u308b\u6a5f\u80fd\u3067\u7279\u306b\u4fbf\u5229\u306a\u306e\u304c\u300c<strong>Query Builder<\/strong>\u300d\u3001\u3064\u307e\u308a\u30c7\u30fc\u30bf\u30d9\u30fc\u30b9\u306e\u64cd\u4f5c\u3068\u3044\u3063\u3066\u3044\u3044\u3068\u601d\u3044\u307e\u3059\u3002<\/p>\n<p>Laravel\u306b\u9650\u3089\u305a\u3067\u3059\u304c\u3001\u30d5\u30ec\u30fc\u30e0\u30ef\u30fc\u30af\u304c\u306a\u304b\u3063\u305f\u9803\u306f\u3044\u3061\u3044\u3061DB\u306b\u63a5\u7d9a\u3059\u308b\u30b3\u30fc\u30c9\u3092\u5b9f\u884c\u3057\u3001\u305d\u308c\u304b\u3089\u5197\u9577\u306aSQL\u6587\u3092\u7e70\u308a\u8fd4\u3057\u8a18\u8ff0\u3057\u305f\u3082\u306e\u3067\u3059\u3051\u3069\u3001\u73fe\u5728\u306f\u304a\u304b\u3052\u3055\u307e\u3067\u30db\u30f3\u30c8\u306b\u3059\u3063\u304d\u308a\u3057\u305f\u30b3\u30fc\u30c9\u3067\u958b\u767a\u3067\u304d\u308b\u3088\u3046\u306b\u306a\u308a\u307e\u3057\u305f\u3002<\/p>\n<p>\u3068\u3044\u3046\u3053\u3068\u3067\u3001\u4eca\u56de\u306fLaravel\u306e\u6839\u672c\u7684\u306a\u6a5f\u80fd\u306eDB\u64cd\u4f5c\u306e\u4e2d\u304b\u3089\u3001\u30c7\u30fc\u30bf\u53d6\u5f97\u306b\u30d5\u30a9\u30fc\u30ab\u30b9\u3057\u305f\u5168\u5b9f\u4f8b\u3092\u7d39\u4ecb\u3057\u307e\u3059\u3002<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-418 aligncenter\" src=\"https:\/\/blog.capilano-fw.com\/wp-content\/uploads\/2018\/04\/35.png\" alt=\"\" width=\"370\" height=\"240\" srcset=\"https:\/\/blog.capilano-fw.com\/wp-content\/uploads\/2018\/04\/35.png 370w, https:\/\/blog.capilano-fw.com\/wp-content\/uploads\/2018\/04\/35-300x195.png 300w\" sizes=\"auto, (max-width: 370px) 100vw, 370px\" \/><\/p>\n<p>\u3010\u52d5\u4f5c\u74b0\u5883\u3011<\/p>\n<ul>\n<li>Laravel 5.6<\/li>\n<li>MySQL 5.7<\/li>\n<\/ul>\n<h1>\u30c7\u30fc\u30bf\u53d6\u5f97\u306e\u57fa\u672c<\/h1>\n<h2>get()\u3067\u5168\u3066\u306e\u30c7\u30fc\u30bf\u3092\u53d6\u5f97\u3059\u308b<\/h2>\n<p>Laravel\u3067\u30c7\u30fc\u30bf\u30d9\u30fc\u30b9\u304b\u3089\u30c7\u30fc\u30bf\u53d6\u5f97\u3059\u308b\u57fa\u672c\u306f\u4ee5\u4e0b\u306e\u3088\u3046\u306b\u306a\u308a\u307e\u3059\u3002<\/p>\n<pre>$items = <strong>\\DB::table('items')-&gt;get();<\/strong>    \/\/ \u5168\u3066\u306e\u30c7\u30fc\u30bf\u304c\u53d6\u5f97\u3067\u304d\u308b<\/pre>\n<p>\u30c7\u30fc\u30bf\u30d9\u30fc\u30b9\u540d\u3092<strong>table()<\/strong>\u5185\u306b\u66f8\u304d\u8fbc\u3093\u3067<strong>get()<\/strong>\u3092\u547c\u3076\u3060\u3051\u3002\u3053\u308c\u3067\u5168\u30c7\u30fc\u30bf\u3092\u30c7\u30fc\u30bf\u30d9\u30fc\u30b9\u304b\u3089\u5f15\u3063\u5f35\u3063\u3066\u304f\u308b\u3053\u3068\u304c\u3067\u304d\u307e\u3059\u3002\u3068\u3066\u3082\u7c21\u5358\u3067\u3059\u306d\u3002<\/p>\n<p>\u305d\u3057\u3066\u3001\u53d6\u5f97\u3057\u305f\u30c7\u30fc\u30bf\u3092\u3072\u3068\u3064\u305a\u3064\u53d6\u308a\u51fa\u3057\u305f\u3044\u5834\u5408\u306f\u30eb\u30fc\u30d7\u3092\u4f7f\u3044\u307e\u3059\u3002<\/p>\n<pre>foreach ($items as $item) {\r\n\r\n    <strong>echo $item-&gt;name;   \/\/ \u5404\u30c7\u30fc\u30bf\u306e\u540d\u524d\u3092\u8868\u793a<\/strong>\r\n\r\n}<\/pre>\n<p>\u307e\u305f\u3001\u3088\u308a\u30b7\u30f3\u30d7\u30eb\u306b\u66f8\u304d\u305f\u3044\u5834\u5408\u306f\u3001Eloquent\uff08\u30e2\u30c7\u30eb\uff09\u3092\u4f5c\u3063\u3066\u304a\u304f\u3068\u3001\u3044\u3061\u3044\u3061<strong>DB::table()<\/strong>\u3092\u4f7f\u308f\u306a\u304f\u3068\u3082\u4ee5\u4e0b\u306e\u3088\u3046\u306b\u66f8\u304f\u3053\u3068\u304c\u3067\u304d\u307e\u3059\u3002<\/p>\n<pre>$items = <strong>\\App\\Item::get();<\/strong>    \/\/ \u5168\u3066\u306e\u30c7\u30fc\u30bf\u304c\u53d6\u5f97\u3067\u304d\u308b<\/pre>\n<p>\u3061\u306a\u307f\u306b\u3001<strong>\\App<\/strong>\u3068\u3044\u3046\u306e\u306f\u30cd\u30fc\u30e0\u30b9\u30da\u30fc\u30b9\u306a\u306e\u3067\u3001\u300cuse App\\Item;\u300d\u3092\u8ffd\u52a0\u3057\u3066\u304a\u304f\u3068\u3001\u4ee5\u4e0b\u306e\u3088\u3046\u306b\u3088\u308a\u30b7\u30f3\u30d7\u30eb\u306b\u30b3\u30fc\u30c9\u3092\u66f8\u304f\u3053\u3068\u3082\u3067\u304d\u307e\u3059\u3002<\/p>\n<pre><strong>use App\\Item;<\/strong>\r\n\r\nclass HomeController extends Controller\r\n{\r\n    public function index() {\r\n\r\n        $items = <strong>Item::get();<\/strong>\r\n\r\n    }<\/pre>\n<p>\u203b\u3061\u306a\u307f\u306ball()\u3082get()\u3068\u540c\u3058\u3067\u3059\u3002<\/p>\n<h2>first()\u3067\u3072\u3068\u3064\u3060\u3051\u30c7\u30fc\u30bf\u53d6\u5f97\u3059\u308b<\/h2>\n<p><strong>get()<\/strong>\u306f\u8a72\u5f53\u3059\u308b\u30c7\u30fc\u30bf\u5168\u3066\u3092\u53d6\u5f97\u3059\u308b\u3084\u308a\u65b9\u3067\u3057\u305f\u304c\u3001<strong>first()<\/strong>\u3092\u4f7f\u3048\u3070\u3072\u3068\u3064\u3060\u3051\u30c7\u30fc\u30bf\u53d6\u5f97\u3059\u308b\u3053\u3068\u304c\u3067\u304d\u307e\u3059\u3002<\/p>\n<p>\u4f8b\u3048\u3070\u3001\u4ee5\u4e0b\u306e\u3088\u3046\u306b\u3057\u305f\u5834\u5408\u306f\u3069\u3046\u306a\u308b\u3067\u3057\u3087\u3046\u304b\u3002<\/p>\n<pre><strong>$item = \\App\\Item::first();<\/strong><\/pre>\n<p>\u3053\u306e\u5834\u5408\u306f\u4ee5\u4e0b\u306e\u3088\u3046\u306b\u3001\u305f\u304f\u3055\u3093\u53d6\u5f97\u3055\u308c\u308b\u30c7\u30fc\u30bf\u306e\u4e2d\u304b\u3089<strong>\u6700\u521d\u306e\u3082\u306e\u3060\u3051<\/strong>\u304c\u53d6\u5f97\u3055\u308c\u308b\u3053\u3068\u306b\u306a\u308a\u307e\u3059\u3002<\/p>\n<pre>Array\r\n(\r\n    <strong>[id] =&gt; 1<\/strong>\r\n    [name] =&gt; \u540d\u524d0\r\n    [created_at] =&gt; 2018-07-02 17:21:50\r\n    [updated_at] =&gt; 2018-07-02 17:21:50\r\n)<\/pre>\n<p>\u79c1\u306e\u74b0\u5883\u3067\u306f\u4e26\u3079\u66ff\u3048\u6307\u5b9a\u304c\u3055\u308c\u3066\u3044\u306a\u3044\u5834\u5408\u3001\u300cid\u300d\u306e\u6607\u9806\u3068\u306a\u308a\u307e\u3059\u306e\u3067\u3001\u4eca\u56de\u306f\u300cid\u300d\u304c\uff11\u756a\u76ee\u306e\u30c7\u30fc\u30bf\u304c\u53d6\u5f97\u3055\u308c\u307e\u3057\u305f\u3002<\/p>\n<h2>find()\u3067id\u3092\u6307\u5b9a\u3057\u3066\u53d6\u5f97<\/h2>\n<p>Laravel\u3067\u306f\u30c7\u30fc\u30bf\u30d9\u30fc\u30b9\u306e\u30c6\u30fc\u30d6\u30eb\u306b\u300cid\u300d\u3068\u3044\u3046\u30ab\u30e9\u30e0\u304c\u7528\u610f\u3055\u308c\u3066\u3044\u308b\u3053\u3068\u304c\u524d\u63d0\u306b\u306a\u3063\u3066\u3044\u307e\u3059\u3002<\/p>\n<p>\u3053\u308c\u3092\u5229\u7528\u3057\u305f\u4fbf\u5229\u306a\u66f8\u304d\u65b9\u304c<strong>find()<\/strong>\u3067\u3059\u3002\u3064\u307e\u308a\u3001id\u756a\u53f7\u3092\u6307\u5b9a\u3059\u308b\u3060\u3051\u3067<strong>get()<\/strong>\u3084<strong>first()<\/strong>\u3092\u4f7f\u308f\u306a\u304f\u3066\u3082\u30c7\u30fc\u30bf\u304c\u53d6\u5f97\u3067\u304d\u307e\u3059\u3002<\/p>\n<p>\u3084\u308a\u65b9\u306f\u3053\u3046\u3067\u3059\u3002<\/p>\n<pre>$id = 1;\r\n$item = \\App\\Item:<strong>:find($id)<\/strong>;<\/pre>\n<p><strong>find()<\/strong>\u3092\u4f7f\u3046\u3068\u5f8c\u3067\u8aac\u660e\u3059\u308b<strong>where()<\/strong>\u3092\u4f7f\u308f\u306a\u304f\u3066\u3088\u304f\u306a\u308a\u307e\u3059\u3002\u305d\u306e\u305f\u3081\u30b3\u30fc\u30c9\u91cf\u304c\u5c11\u306a\u304f\u306a\u308b\u306e\u3067\u3001\u7279\u5225\u306a\u7406\u7531\u304c\u306a\u3044\u5834\u5408\u306f\u3053\u3061\u3089\u3092\u4f7f\u3046\u3053\u3068\u3092\u304a\u3059\u3059\u3081\u3057\u307e\u3059\u3002<\/p>\n<p>\u307e\u305f\u3001id\u306f\u8907\u6570\u6307\u5b9a\u3059\u308b\u3053\u3068\u3082\u3067\u304d\u307e\u3059\u3002\u3053\u306e\u5834\u5408\u306f\u8907\u6570\u30c7\u30fc\u30bf\u304c\u53d6\u5f97\u3055\u308c\u307e\u3059\u306e\u3067\u3001foreach()\u306a\u3069\u3092\u4f7f\u3063\u3066\u30eb\u30fc\u30d7\u3055\u305b\u306a\u304c\u3089\u30c7\u30fc\u30bf\u53d6\u5f97\u3059\u308b\u3068\u3044\u3044\u3067\u3057\u3087\u3046\u3002<\/p>\n<pre>$items = \\App\\Item::<strong>find([1, 2])<\/strong>;<\/pre>\n<h2>chunk()\u3067\u3042\u308b\u4ef6\u6570\u3054\u3068\u306b\u30c7\u30fc\u30bf\u53d6\u5f97<\/h2>\n<p>\u4f8b\u3048\u3070\u300110\u4ef6\u3054\u3068\u306b\u30e6\u30fc\u30b6\u30fc\u30fb\u30c7\u30fc\u30bf\u3092\u53d6\u5f97\u3057\u305f\u3044\u5834\u5408\u306fchunk()\u3092\u4f7f\u3046\u3068\u7c21\u5358\u306b\u3067\u304d\u307e\u3059\u3002<\/p>\n<pre>\\App\\User::<strong>chunk(10, function($users){<\/strong>\r\n\r\n    print_r($users-&gt;toArray()); \/\/ \u6700\u592710\u4ef6\u306e\u30e6\u30fc\u30b6\u30fc\u30c7\u30fc\u30bf\r\n\r\n<strong>});<\/strong><\/pre>\n<p>\u53d6\u5f97\u3055\u308c\u305f\u30c7\u30fc\u30bf\uff08\u3053\u3053\u3067\u306f$users\uff09\u306f<strong>foreach()\u3067\u30eb\u30fc\u30d7\u3059\u308b\u3053\u3068\u304c\u3067\u304d\u307e\u3059\u3002<\/strong><\/p>\n<p>\u307e\u305f\u3001\u3082\u3057\u9014\u4e2d\u3067\u7d42\u4e86\u3057\u305f\u3044\u5834\u5408\u306f\u4ee5\u4e0b\u306e\u3088\u3046\u306b\u300c<strong>return false;<\/strong>\u300d\u3092\u8ffd\u52a0\u3057\u3066\u3042\u3052\u307e\u3057\u3087\u3046\u3002<\/p>\n<pre>\\App\\User::chunk(1, function($users){\r\n\r\n    foreach ($users as $user) {\r\n\r\n        if($user-&gt;id == 1) {\r\n\r\n            <strong>return false;<\/strong>\r\n\r\n        }\r\n\r\n    }\r\n\r\n});<\/pre>\n<h1>\u30ab\u30e9\u30e0\uff08\u30d5\u30a3\u30fc\u30eb\u30c9\uff09\u3092\u6307\u5b9a\u3059\u308b<\/h1>\n<h2>select()\u3067\u6307\u5b9a<\/h2>\n<p>\u300cid\u300d\u3084\u300cname\u300d\u3068\u3044\u3063\u305f\u30ab\u30e9\u30e0\u3092\u6307\u5b9a\u3057\u305f\u3044\u5834\u5408\u306f<strong>select()<\/strong>\u3092\u4f7f\u3044\u307e\u3059\u3002<\/p>\n<p>\u3067\u306f\u3001\u300cname\u300d\u3060\u3051\u3092\u53d6\u5f97\u3059\u308b\u5834\u5408\u3092\u898b\u3066\u307f\u307e\u3057\u3087\u3046\u3002<\/p>\n<pre>$items = \\App\\Item::<strong>select('name')<\/strong>-&gt;get();<\/pre>\n<p>\u53d6\u5f97\u3055\u308c\u308b\u30c7\u30fc\u30bf\u306f\u3053\u3046\u306a\u308a\u307e\u3059\u3002\u300cname\u300d\u3060\u3051\u304c\u53d6\u5f97\u3067\u304d\u3066\u3044\u307e\u3059\u306d\u3002<\/p>\n<pre>Array\r\n(\r\n    [0] =&gt; Array\r\n    (\r\n        [<strong>name<\/strong>] =&gt; \u540d\u524d0\r\n    )\r\n\r\n    [1] =&gt; Array\r\n    (\r\n        [<strong>name<\/strong>] =&gt; \u540d\u524d1\r\n    )\r\n\r\n    [2] =&gt; Array\r\n    (\r\n        [<strong>name<\/strong>] =&gt; \u540d\u524d2\r\n    )\r\n\r\n)<\/pre>\n<p>\u3067\u306f\u3001\u8907\u6570\u30ab\u30e9\u30e0\u3092\u53d6\u5f97\u3059\u308b\u5834\u5408\u306f\u3069\u3046\u3059\u308c\u3070\u3044\u3044\u3067\u3057\u3087\u3046\u304b\u3002<br \/>\n\u305d\u306e\u307e\u307e\u7b2c\uff12\u5f15\u6570\u3001\u7b2c\uff13\u5f15\u6570\u306b\u30ab\u30e9\u30e0\u3092\u8ffd\u52a0\u3057\u3066\u3044\u3063\u3066\u304f\u3060\u3055\u3044\u3002<\/p>\n<pre>$items = \\App\\Item::<strong>select('id', 'name', 'created_at')<\/strong>-&gt;get();<\/pre>\n<p>\u3053\u308c\u3092\u5b9f\u884c\u3059\u308b\u3068\u4e2d\u8eab\u306f\u3053\u3046\u306a\u308a\u307e\u3059\u3002<\/p>\n<pre>Array\r\n(\r\n    [0] =&gt; Array\r\n    (\r\n        [<strong>id<\/strong>] =&gt; 1\r\n        [<strong>name<\/strong>] =&gt; \u540d\u524d0\r\n        [<strong>created_at<\/strong>] =&gt; 2018-07-02 17:07:17\r\n    )\r\n\r\n    [1] =&gt; Array\r\n    (\r\n        [<strong>id<\/strong>] =&gt; 2\r\n        [<strong>name<\/strong>] =&gt; \u540d\u524d1\r\n        [<strong>created_at<\/strong>] =&gt; 2018-07-02 17:07:17\r\n    )\r\n\r\n    [2] =&gt; Array\r\n    (\r\n        [<strong>id<\/strong>] =&gt; 3\r\n        [<strong>name<\/strong>] =&gt; \u540d\u524d2\r\n        [<strong>created_at<\/strong>] =&gt; 2018-07-02 17:07:17\r\n    )\r\n\r\n)<\/pre>\n<p>\u203b\u3061\u306a\u307f\u306bCakePHP\u306a\u3069\u3067\u306f\u3001\u30ab\u30e9\u30e0\u304c\u8907\u6570\u306b\u306a\u308b\u3068\u914d\u5217\u306b\u3057\u306a\u3044\u3068\u3044\u3051\u306a\u304b\u3063\u305f\u308a\u3057\u307e\u3059\u304c\u3001Laravel\u3067\u306f\u5f15\u6570\u3092\u8ffd\u52a0\u3059\u308b\u3060\u3051\u306a\u306e\u3067\u3001\u3088\u308a\u30b7\u30f3\u30d7\u30eb\u306b\u4f7f\u3048\u308b\u3068\u601d\u3044\u307e\u3059\u3002\u6614\u306farray()\u3068\u66f8\u3044\u3066\u3044\u305f\u306e\u3067\u4f59\u8a08\u306b\u53ef\u8aad\u6027\u304c\u60aa\u304f\u306a\u3063\u3066\u3044\u305f\u306e\u3092\u899a\u3048\u3066\u3044\u307e\u3059\u3002<\/p>\n<p>\u3061\u306a\u307f\u306b\u3001\u5f8c\u304b\u3089\u30ab\u30e9\u30e0\u3092\u8ffd\u52a0\u3059\u308b\u3053\u3068\u3082\u3067\u304d\u307e\u3059\u3002\u305d\u306e\u5834\u5408\u306f<strong>addSelect()<\/strong>\u3092\u4f7f\u3044\u307e\u3057\u3087\u3046\u3002<\/p>\n<pre>$query = \\App\\Item::select('id');\r\n$query-&gt;<strong>addSelect('name')<\/strong>;\r\n$items = $query-&gt;get();<\/pre>\n<p>\u3053\u306e\u30c6\u30af\u30cb\u30c3\u30af\u3092\u4f7f\u3048\u3070\u3001GET\u3084POST\u30d1\u30e9\u30e1\u30fc\u30bf\u306e\u5185\u5bb9\u306b\u3088\u3063\u3066\u53d6\u5f97\u3059\u308b\u30ab\u30e9\u30e0\u3092\u5909\u66f4\u3059\u308b\u3053\u3068\u3082\u3067\u304d\u308b\u3067\u3057\u3087\u3046\u3002<\/p>\n<h2>\u30ab\u30e9\u30e0\u540d\u3092\u5909\u66f4\u3059\u308b<\/h2>\n<p>\u4f8b\u3048\u3070\u3001\u300cname\u300d\u3068\u3044\u3046\u30ad\u30fc\u3092\u300ccustomer\u300d\u306b\u5909\u66f4\u3057\u305f\u3044\u5834\u5408\u3067\u3059\u3002<br \/>\n\u3053\u306e\u5834\u5408\u306f\u3001select\u5185\u306b\u300c<strong>as<\/strong>\u300d\u3092\u3064\u3051\u3066\u8a18\u8ff0\u3057\u307e\u3059\u3002<\/p>\n<p>\u5b9f\u969b\u306e\u4f8b\u306f\u3053\u3046\u306a\u308a\u307e\u3059\u3002<\/p>\n<pre>$items = \\App\\Item::<strong>select('name as customer')<\/strong>-&gt;get();<\/pre>\n<p>\u5b9f\u969b\u306e\u53d6\u5f97\u30c7\u30fc\u30bf\u306f\u4ee5\u4e0b\u306e\u3088\u3046\u306b\u306a\u308a\u307e\u3059\u3002<\/p>\n<pre>Array\r\n(\r\n    [0] =&gt; Array\r\n    (\r\n        [<strong>customer<\/strong>] =&gt; \u540d\u524d0\r\n    )\r\n\r\n    [1] =&gt; Array\r\n    (\r\n        [<strong>customer<\/strong>] =&gt; \u540d\u524d1\r\n    )\r\n\r\n    [2] =&gt; Array\r\n    (\r\n        [<strong>customer<\/strong>] =&gt; \u540d\u524d2\r\n    )\r\n)<\/pre>\n<h2>value()\u3067\uff11\u3064\u306e\u30c7\u30fc\u30bf\u3060\u3051\u3092\u53d6\u5f97\u3059\u308b<\/h2>\n<p>\u4f8b\u3048\u3070\u3001\u30c7\u30fc\u30bf\u30d9\u30fc\u30b9\u306b\u8a2d\u5b9a\u60c5\u5831\u3092\u4fdd\u5b58\u3057\u3066\u3044\u3066\u300c\u30b5\u30a4\u30c8\u306e\u6240\u6709\u8005\u60c5\u5831\u300d\u3092\u53d6\u5f97\u3057\u305f\u3044\u3068\u3044\u3063\u305f\u3001<strong>\u305f\u3063\u305f\uff11\u3064\u3060\u3051\u30c7\u30fc\u30bf\u53d6\u5f97\u3057\u305f\u3044\u5834\u5408<\/strong>\u3001value()\u3092\u4f7f\u3048\u3070\u30b7\u30f3\u30d7\u30eb\u306b\u5b9f\u88c5\u3067\u304d\u307e\u3059\u3002<\/p>\n<p>\u5b9f\u969b\u306e\u4f8b\u3092\u898b\u3066\u307f\u307e\u3057\u3087\u3046\u3002<\/p>\n<pre>$owner = \\App\\Configuration::where('key', 'owner')-&gt;<strong>value('owner')<\/strong>;<\/pre>\n<p>\u305f\u3060\u3057\u3001\u6ce8\u610f\u304c\u5fc5\u8981\u306a\u306e\u306f\u8907\u6570\u306e\u30c7\u30fc\u30bf\u304c\u3042\u308b\u5834\u5408\u306f\u300c\uff11\u3064\u3081\u306e\u300d\u30c7\u30fc\u30bf\u304b\u3089\u3060\u3051\u3057\u304b\u53d6\u5f97\u304c\u3067\u304d\u306a\u3044\u3068\u3044\u3046\u3053\u3068\u3067\u3059\u3002<\/p>\n<p>\u4f8b\u3048\u3070\u3001\u4ee5\u4e0b\u306f\uff12\u4eba\u306e\u30e6\u30fc\u30b6\u30fc\u304c\u53d6\u5f97\u3067\u304d\u308b\u4f8b\u3067\u3059\u304c<strong>value()<\/strong>\u3067\u53d6\u5f97\u3067\u304d\u308b\u306e\u306f\u6700\u521d\u306e\u30c7\u30fc\u30bf\u306e\u540d\u524d\uff08name\uff09\u3060\u3051\u3067\u3059\u3002<\/p>\n<pre>$name = \\App\\User::<strong>whereIn('id', [1, 2])-&gt;value('name');<\/strong><\/pre>\n<p>\u8907\u6570\u306e\u30c7\u30fc\u30bf\u304c\u5fc5\u8981\u306a\u5834\u5408\u306f\u6b21\u306e<strong>pluck()<\/strong>\u3092\u4f7f\u3044\u307e\u3057\u3087\u3046\u3002<\/p>\n<h2>pluck()\u3067\u7279\u5b9a\u306e\u30c7\u30fc\u30bf\u3092\u8907\u6570\u53d6\u5f97\u3059\u308b<\/h2>\n<p>value()\u306f\u305f\u3063\u305f\uff11\u3064\u3060\u3051\u306e\u30c7\u30fc\u30bf\u3057\u304b\u53d6\u5f97\u3067\u304d\u307e\u305b\u3093\u3067\u3057\u305f\u304c\u3001\u914d\u5217\u306e\u3088\u3046\u306b\u30c7\u30fc\u30bf\u3092\u8907\u6570\u53d6\u5f97\u3059\u308b\u65b9\u6cd5\u3082\u3042\u308a\u307e\u3059\u3002\u305d\u308c\u304c<strong>pluck()<\/strong>\u3067\u3059\u3002<\/p>\n<p>\u4f8b\u3092\u898b\u3066\u307f\u307e\u3057\u3087\u3046\u3002<\/p>\n<pre>$names = \\App\\User::<strong>pluck('name')<\/strong>;\r\n\r\nforeach ($names as $name) {\r\n\r\n    echo $name; \/\/ \u5404\u30c7\u30fc\u30bf\u306e\u540d\u524d\r\n\r\n}<\/pre>\n<p>\u3053\u306e\u4f8b\u306f\u3001\u300cname\u300d\u306e\u30c7\u30fc\u30bf\u3060\u3051\u3092\u8907\u6570\u53d6\u5f97\u3059\u308b\u30b3\u30fc\u30c9\u3067\u3059\u3002<br \/>\n\u53d6\u5f97\u3057\u305f\u30c7\u30fc\u30bf\u306f\u901a\u5e38\u306e\u914d\u5217\u306e\u3088\u3046\u306bforeach()\u3067\u53d6\u5f97\u304c\u3067\u304d\u307e\u3059\u3002<\/p>\n<p>\u307e\u305f\u3001\u9023\u60f3\u914d\u5217\u306e\u3088\u3046\u306b\u30ad\u30fc\u3092\u3064\u3051\u3066\u53d6\u5f97\u3059\u308b\u3053\u3068\u3082\u3067\u304d\u3066\u3001\u305d\u306e\u5834\u5408\u306f<strong>\u7b2c\uff12\u5f15\u6570\u306b\u30ad\u30fc\u306b\u3057\u305f\u3044\u30ab\u30e9\u30e0<span style=\"font-size: 1rem;\">\u3092\u6307\u5b9a\u3057\u307e\u3059\u3002<\/span><\/strong><\/p>\n<p>\u5b9f\u969b\u306e\u4f8b\u306f\u3053\u3046\u306a\u308a\u307e\u3059\u3002<\/p>\n<pre>$names = \\App\\User::<strong>pluck('name', 'id')<\/strong>;\r\n\r\nforeach ($names as <strong>$id<\/strong> =&gt; $name) {\r\n\r\n    echo $id;  \/\/ User ID\r\n    echo $name;     \/\/ \u5404\u30c7\u30fc\u30bf\u306e\u540d\u524d\r\n\r\n}<\/pre>\n<p>\u540c\u3058\u304f\u5404\u30c7\u30fc\u30bf\u3092\u53d6\u308a\u51fa\u3059\u5834\u5408\u306fforeach()\u304c\u4f7f\u3048\u307e\u3059\u304c\u3001\u3055\u3063\u304d\u3068\u9055\u3046\u306e\u306f\u30ad\u30fc\uff08\u3053\u3053\u3067\u306f$id\uff09\u3082\u540c\u6642\u306b\u53d6\u5f97\u3067\u304d\u308b\u3068\u3044\u3046\u90e8\u5206\u3067\u3059\u3002<\/p>\n<h1>\u30ea\u30ec\u30fc\u30b7\u30e7\u30f3\u5148\u306e\u30c7\u30fc\u30bf\u3092\u53d6\u5f97\u3059\u308b<\/h1>\n<h2>with()\u3067\u30ea\u30ec\u30fc\u30b7\u30e7\u30f3\u5148\u306e\u30c7\u30fc\u30bf\u3092\u4e00\u7dd2\u306b\u53d6\u5f97\u3059\u308b<\/h2>\n<p>\u4f8b\u3048\u3070\u3001\u3042\u308b\u30e6\u30fc\u30b6\u30fc\u306e\u30c7\u30fc\u30bf\u3092\u53d6\u5f97\u3059\u308b\u5834\u5408\u306b\u4ee5\u4e0b\u306e\u3088\u3046\u306a\u30ea\u30ec\u30fc\u30b7\u30e7\u30f3\u3057\u3066\u3044\u308b\u30c6\u30fc\u30d6\u30eb\u306e\u5185\u5bb9\u3082\u4e00\u7dd2\u306b\u53d6\u5f97\u3059\u308b\u65b9\u6cd5\u3067\u3059\u3002<\/p>\n<ul>\n<li><strong>\uff11 \u5bfe \uff11<\/strong><\/li>\n<li><strong>\uff11 \u5bfe \u591a<\/strong><\/li>\n<li><strong>\u591a \u5bfe \u591a<\/strong><\/li>\n<\/ul>\n<p>\u5b9f\u969b\u306e\u4f8b\u3092\u898b\u3066\u307f\u307e\u3057\u3087\u3046\u3002<\/p>\n<pre>$users = \\App\\User::<strong>with('contacts')<\/strong>-&gt;get();<\/pre>\n<p>with()\u3092\u4f7f\u3063\u3066\u4ee5\u4e0b\u306e\u3088\u3046\u306a\u30e2\u30c7\u30eb\u5185\u306b\u6307\u5b9a\u3057\u305f\u30ea\u30ec\u30fc\u30b7\u30e7\u30f3\u3092\u6307\u5b9a\u3057\u3066\u3044\u307e\u3059\u3002<\/p>\n<pre>public function <strong>contacts<\/strong>() {\r\n\r\n    return $this-&gt;hasMany('App\\Contact');\r\n\r\n}<\/pre>\n<p>\u307e\u305f\u3001\u30ea\u30ec\u30fc\u30b7\u30e7\u30f3\u5148\u306e\u30c6\u30fc\u30d6\u30eb\u306b\u6761\u4ef6\u3092\u3064\u3051\u305f\u3044\u5834\u5408\u306f\u4ee5\u4e0b\u306e\u3088\u3046\u306b\u914d\u5217\u306b\u3057\u3066function()\u5185\u3067\u30af\u30a8\u30ea\u3092\u4f5c\u6210\u3059\u308b\u3053\u3068\u304c\u3067\u304d\u307e\u3059\u3002<\/p>\n<pre>$users = \\App\\User::<strong>with(['contacts' =&gt; function($q){<\/strong>\r\n\r\n<strong>    $q-&gt;where('created_at', '&gt;', '2018-07-01');<\/strong>\r\n\r\n<strong>}])<\/strong>-&gt;get();<\/pre>\n<p>\u203b\u5f8c\u3067\u51fa\u3066\u304f\u308b<strong>whereHas()<\/strong>\u3068\u4f3c\u3066\u3044\u307e\u3059\u304c\u5185\u5bb9\u306f\u5168\u304f\u9055\u3063\u3066\u3044\u307e\u3059\u3002with()\u306e\u5834\u5408\u306f\u30ea\u30ec\u30fc\u30b7\u30e7\u30f3\u5148\u306b\u30c7\u30fc\u30bf\u304c\u306a\u304f\u3066\u3082\u30e6\u30fc\u30b6\u30fc\u30fb\u30c7\u30fc\u30bf\u306f\u53d6\u5f97\u3055\u308c\u307e\u3059\u3002\u305d\u308c\u306b\u5bfe\u3057\u3066\u3001whereHas()\u3067\u306f\u30ea\u30ec\u30fc\u30b7\u30e7\u30f3\u5148\u306b\u30c7\u30fc\u30bf\u304c\u306a\u3044\u30e6\u30fc\u30b6\u30fc\u306f\u53d6\u5f97\u3055\u308c\u307e\u305b\u3093\u3002<\/p>\n<h1>\u8a72\u5f53\u3059\u308b\u30c7\u30fc\u30bf\u3060\u3051\u53d6\u5f97\u3059\u308b\uff08\u691c\u7d22\uff09<\/h1>\n<h2>where()\u3067\u6307\u5b9a\u3059\u308b<\/h2>\n<p>\u3082\u3057\u3001\u30ab\u30e9\u30e0\u300cname\u300d\u304c\u300c\u540d\u524d1\u300d\u3068\u3044\u3046\u30c7\u30fc\u30bf\u3092\u53d6\u5f97\u3057\u305f\u3044\u5834\u5408\u306fwhere()\u3092\u4f7f\u3063\u3066\u8a72\u5f53\u30c7\u30fc\u30bf\u3060\u3051\u3092\u63a2\u3057\u3060\u3057\u307e\u3059\u3002<\/p>\n<pre>$item = \\App\\Item::<strong>where('name', '\u540d\u524d1')<\/strong>-&gt;first();<\/pre>\n<p>\u3061\u306a\u307f\u306b\u3001\u300c<strong>=<\/strong>\u300d\u3092\u3064\u3051\u305f\u4ee5\u4e0b\u306e\u3084\u308a\u65b9\u3082\u7d50\u679c\u306f\u540c\u3058\u3067\u3059\u304c\u30b3\u30fc\u30c9\u306f\u5c11\u306a\u3044\u307b\u3046\u304c\u3044\u3044\u306e\u3067\u3001\u4f55\u304b\u7279\u5225\u306a\u7406\u7531\u304c\u306a\u3044\u5834\u5408\u306f\u4e0a\u306e\u7701\u7565\u30d0\u30fc\u30b8\u30e7\u30f3\u3092\u304a\u3059\u3059\u3081\u3057\u307e\u3059\u3002<\/p>\n<pre>$item = \\App\\Item::<strong>where('name', '=', '\u540d\u524d1')<\/strong>-&gt;first();<\/pre>\n<h2>AND\uff0fOR\u691c\u7d22<\/h2>\n<h3>\u300c-&gt;\u300d\u3067\u3064\u306a\u3052\u3066AND\u691c\u7d22<\/h3>\n<p>\u4f8b\u3048\u3070\u3001\u4ee5\u4e0b\u306eAND\u6761\u4ef6\u306e\u5834\u5408\u3067\u3059\u3002<\/p>\n<ul>\n<li><strong>ID\u756a\u53f7\u306f\uff11\u756a<\/strong><\/li>\n<li><strong>\u540d\u524d\u306f\u9234\u6728<\/strong><\/li>\n<\/ul>\n<p>\u5b9f\u969b\u306e\u4f8b\u306f\u3053\u3046\u306a\u308a\u307e\u3059\u3002<\/p>\n<pre>$user = \\App\\User::<strong>where('id', 1)<\/strong>\r\n<strong>    -&gt;where('name', '\u9234\u6728')<\/strong>\r\n    -&gt;first();<\/pre>\n<p>AND\u691c\u7d22\u306f\u5834\u5408\u306f\u3001where()\u306e\u5f8c\u306b\u307e\u305f\u30a2\u30ed\u30fc\u6f14\u7b97\u5b50\u300c<strong>-&gt;<\/strong>\u300d\u3092\u4f7f\u3063\u3066\u3055\u3089\u306bwhere()\u3092\u8ffd\u52a0\u3059\u308c\u3070OK\u3067\u3059\u3002<\/p>\n<h3>orWhere()\u3067OR\u691c\u7d22<\/h3>\n<p>OR\u691c\u7d22\u3057\u305f\u3044\u5834\u5408\u306f\u3001\u540c\u3058\u304f\u300c-&gt;\u300d\u3092\u4f7f\u3063\u3066orWhere()\u3092\u3064\u306a\u3052\u308b\u3053\u3068\u3067\u5b9f\u73fe\u3067\u304d\u307e\u3059\u3002<\/p>\n<p>\u5b9f\u969b\u306e\u4f8b\u3092\u898b\u3066\u307f\u307e\u3057\u3087\u3046\u3002<\/p>\n<pre>$user = \\App\\User::where('id', 1)\r\n    <strong>-&gt;orWhere('name', '\u9234\u6728')<\/strong>\r\n    -&gt;get();<\/pre>\n<p>\u3053\u306e\u5834\u5408\u3001\u4ee5\u4e0b\u306e\u6761\u4ef6\u306e<strong>\u3069\u3061\u3089\u304b\u306b<\/strong>\u8a72\u5f53\u3059\u308b\u30c7\u30fc\u30bf\u3092\u53d6\u5f97\u3059\u308b\u3053\u3068\u304c\u3067\u304d\u307e\u3059\u3002\uff08\u3082\u3061\u308d\u3093\u3069\u3061\u3089\u3082\u8a72\u5f53\u3059\u308b\u5834\u5408\u3067\u3082\u53d6\u5f97\u3067\u304d\u307e\u3059\uff09<\/p>\n<ul>\n<li><strong>id\u304c\uff11<\/strong><\/li>\n<li><strong>name\u304c\u300c\u9234\u6728\u300d<\/strong><\/li>\n<\/ul>\n<h2>LIKE\u3067\u3042\u3044\u307e\u3044\u691c\u7d22<\/h2>\n<p>SQL\u6587\u306b\u306f\u300cLIKE\u300d\u3092\u4f7f\u3063\u3066\u3042\u3044\u307e\u3044\u691c\u7d22\u3092\u3059\u308b\u3053\u3068\u3082\u3067\u304d\u307e\u3059\u3002\u4f8b\u3048\u3070\u3001\u4ee5\u4e0b\u306e\u3088\u3046\u306a\u5834\u5408\u3067\u3059\u3002<\/p>\n<ul>\n<li><strong>\u300c\u9234\u6728\u300d\u3067\u59cb\u307e\u308b\u540d\u524d\u306e\u30c7\u30fc\u30bf\u3060\u3051\u53d6\u5f97\u3059\u308b<\/strong><\/li>\n<li><strong>\u300c\u5b50\u300d\u3067\u7d42\u308f\u308b\u540d\u524d\u306e\u30c7\u30fc\u30bf\u3060\u3051\u53d6\u5f97\u3059\u308b<\/strong><\/li>\n<li><strong>\u540d\u524d\u306b\u300c\u5ddd\u300d\u304c\u5165\u3063\u3066\u3044\u308b\u5168\u30c7\u30fc\u30bf\u3092\u53d6\u5f97<\/strong><\/li>\n<\/ul>\n<p>\u3072\u3068\u3064\u305a\u3064\u898b\u3066\u3044\u304d\u307e\u3057\u3087\u3046\u3002<\/p>\n<h3>\u7279\u5b9a\u306e\u6587\u5b57\u5217\u304b\u3089\u59cb\u307e\u308b\u30c7\u30fc\u30bf\u306e\u53d6\u5f97<\/h3>\n<p>\u3067\u306f\u540d\u524d\u304c\u300c\u9234\u6728\u300d\u3067\u59cb\u307e\u308b\u30c7\u30fc\u30bf\u3060\u3051\u3092\u53d6\u5f97\u3057\u3066\u307f\u307e\u3057\u3087\u3046\u3002<\/p>\n<pre>$users = \\App\\User::<strong>where('name', 'like', '\u540d\u524d%')<\/strong>-&gt;get();<\/pre>\n<p>\u30ef\u30a4\u30eb\u30c9\u30ab\u30fc\u30c9\uff08\u3069\u3093\u306a\u6587\u5b57\u5217\u3067\u3082OK\u306b\u3067\u304d\u308b\u6587\u5b57\u3002\u30c8\u30e9\u30f3\u30d7\u306e\u30b8\u30e7\u30fc\u30ab\u30fc\u307f\u305f\u3044\u306a\u3082\u306e\u3067\u3059\u306d\uff09\u306f\u300c<strong>%<\/strong>\u300d\u3067\u3001MySQL\u306a\u3069\u3068\u540c\u3058\u3067\u3059\u3002<\/p>\n<h3>\u7279\u5b9a\u306e\u6587\u5b57\u5217\u3067\u7d42\u308f\u308b\u30c7\u30fc\u30bf\u306e\u53d6\u5f97<\/h3>\n<p>\u3082\u3061\u308d\u3093\u9006\u3082\u3067\u304d\u307e\u3059\u3002<\/p>\n<p>\u6b21\u306b\u300c\u5b50\u300d\u3067\u7d42\u308f\u308b\u540d\u524d\u3060\u3051\u53d6\u5f97\u3057\u3066\u307f\u307e\u3057\u3087\u3046\u3002\u4f8b\u3048\u3070\u300c\u82b1\u5b50\u300d\u3084\u300c\u666f\u5b50\u300d\u306a\u3069\u3067\u3059\u3002<\/p>\n<pre>$users = \\App\\User::<strong>where('name', 'like', '%\u5b50')<\/strong>-&gt;get();<\/pre>\n<p>\u4eca\u56de\u306f\u30ef\u30a4\u30eb\u30c9\u30ab\u30fc\u30c9\u304c\u5148\u982d\u306b\u304d\u3066\u3044\u307e\u3059\u3002<\/p>\n<h3>\u7279\u5b9a\u306e\u6587\u5b57\u5217\u304c\u542b\u307e\u308c\u3066\u3044\u308b\u5168\u30c7\u30fc\u30bf\u3092\u53d6\u5f97<\/h3>\n<p>\u3067\u306f\u3001\u3068\u306b\u304b\u304f\u300c\u5ddd\u300d\u3068\u3044\u3046\u6587\u5b57\u304c\u540d\u524d\u306e\u3069\u3053\u304b\u306b\u5165\u3063\u3066\u3044\u308b\u30c7\u30fc\u30bf\u3060\u3051\u3092\u53d6\u5f97\u3059\u308b\u5834\u5408\u306f\u3069\u3046\u3059\u308c\u3070\u3044\u3044\u3067\u3057\u3087\u3046\u304b\u3002<\/p>\n<p>\u7b54\u3048\u306f\u3001\u3053\u3046\u306a\u308a\u307e\u3059\u3002<\/p>\n<pre>$users = \\App\\User::<strong>where('name', 'like', '%\u5ddd%')<\/strong>-&gt;get();<\/pre>\n<p>\u30ef\u30a4\u30eb\u30c9\u30ab\u30fc\u30c9\u3092\uff12\u3064\u4f7f\u3063\u3066\u3044\u308b\u3093\u3067\u3059\u306d\u3002\u3053\u308c\u3067\u300c\u5ddd\u300d\u304c\u540d\u524d\u306b\u5165\u3063\u3066\u3044\u308b\u30c7\u30fc\u30bf\u3092\u53d6\u5f97\u3059\u308b\u3053\u3068\u304c\u3067\u304d\u307e\u3059\u3002<\/p>\n<h2>\u300c\u5927\u304d\u3044\u300d\u300c\u5c11\u306a\u3044\u300d\u300c\u540c\u3058\u300d\u3067\u6bd4\u8f03\u3057\u3066\u691c\u7d22<\/h2>\n<p>\u6b21\u306b\u6570\u5b57\u3084\u65e5\u4ed8\u306a\u3069\u3067\u4ee5\u4e0b\u306e\u3088\u3046\u306a\u6761\u4ef6\u3067\u691c\u7d22\u3059\u308b\u5834\u5408\u3092\u307f\u3066\u3044\u304d\u307e\u3057\u3087\u3046\u3002<\/p>\n<ul>\n<li><strong>\u3053\u308c\u3088\u308a\u5927\u304d\u3044\uff08\u5f8c\uff09<\/strong><\/li>\n<li><strong>\u3053\u308c\u3088\u308a\u5c0f\u3055\u3044\uff08\u524d\uff09<\/strong><\/li>\n<li><strong>\u3053\u308c\u3068\u540c\u3058<\/strong><\/li>\n<li><strong>\u3053\u308c\u4ee5\u5916<\/strong><\/li>\n<li><strong>\u3053\u308c\u4ee5\u4e0a\uff08\u540c\u3058\u304b\u5927\u304d\u3044\uff09<\/strong><\/li>\n<li><strong>\u3053\u308c\u4ee5\u4e0b\uff08\u540c\u3058\u304b\u5c11\u306a\u3044\uff09<\/strong><\/li>\n<\/ul>\n<p>\u3084\u308a\u65b9\u306f\u7c21\u5358\u3067\u6bd4\u8f03\u6f14\u7b97\u5b50\u3092<strong>where()<\/strong>\u306b\u8ffd\u52a0\u3059\u308b\u3060\u3051\u3067\u3059\u3002<\/p>\n<h3>\u300c&gt;\u300d\u3067\u3088\u308a\u5927\u304d\u3044\uff08\u5f8c\u306e\u65e5\u4ed8\uff09\u30c7\u30fc\u30bf\u3060\u3051\u53d6\u5f97<\/h3>\n<pre>$items = \\App\\Item::<strong>where('id', '&gt;', 1)<\/strong>-&gt;get();<\/pre>\n<h3>\u300c&lt;\u300d\u3088\u308a\u5c0f\u3055\u3044\uff08\u524d\uff09\u30c7\u30fc\u30bf\u3060\u3051\u53d6\u5f97<\/h3>\n<pre>$items = \\App\\Item::<strong>where('id', '&lt;', 3)<\/strong>-&gt;get();<\/pre>\n<h3>\u300c=\u300d\u3067\u540c\u3058\u30c7\u30fc\u30bf\u3060\u3051\u53d6\u5f97<\/h3>\n<pre>$items = \\App\\Item::<strong>where('id', '=', 3)<\/strong>-&gt;get();<\/pre>\n<p>\u203b\u305f\u3060\u3057\u3001\u30a4\u30b3\u30fc\u30eb\u306e\u5834\u5408\u306f\u7701\u7565\u3067\u304d\u308b\u306e\u3067\u4ee5\u4e0b\u306e\u65b9\u304c\u30b3\u30fc\u30c9\u304c\u5c11\u306a\u304f\u3066\u3044\u3044\u3067\u3057\u3087\u3046\u3002<\/p>\n<pre>$items = \\App\\Item::<strong>where('id', 3)<\/strong>-&gt;get();<\/pre>\n<h3>\u300c!=\u300d\u3067\u7279\u5b9a\u306e\u30c7\u30fc\u30bf\u300c\u4ee5\u5916\u300d\u306e\u3082\u306e\u3092\u53d6\u5f97<\/h3>\n<pre>$items = \\App\\Item::<strong>where('id', '!=', 1)<\/strong>-&gt;get();<\/pre>\n<p>\u203b\u3061\u306a\u307f\u306b\u3001\u300c\u4ee5\u5916\u300d\u306e\u6307\u5b9a\u65b9\u6cd5\u306f\u300c<strong>&lt;&gt;<\/strong>\u300d\u3082\u4f7f\u3048\u307e\u3059\u3002\uff08\u305f\u3057\u304b\u3001\u3053\u3061\u3089\u306e\u307b\u3046\u304c\u3088\u308a\u5bfe\u5fdc\u3057\u3066\u3044\u308bDB\u304c\u591a\u304b\u3063\u305f\u306f\u305a\u3067\u3059\uff09<\/p>\n<pre>$items = \\App\\Item::<strong>where('id', '&lt;&gt;', 1)<\/strong>-&gt;get();<\/pre>\n<h3>\u300c&gt;=\u300d\u3067\u4ee5\u4e0a\uff08\u540c\u3058\u304b\u5927\u304d\u3044\uff09\u306e\u30c7\u30fc\u30bf\u3060\u3051\u53d6\u5f97<\/h3>\n<pre>$items = \\App\\Item::<strong>where('id', '&gt;=', 2)<\/strong>-&gt;get();<\/pre>\n<h3>\u300c&lt;=\u300d\u3067\u4ee5\u4e0b\uff08\u540c\u3058\u304b\u5c11\u306a\u3044\uff09\u306e\u30c7\u30fc\u30bf\u3060\u3051\u53d6\u5f97<\/h3>\n<pre>$items = \\App\\Item::<strong>where('id', '&lt;=', 2)<\/strong>-&gt;get();<\/pre>\n<h2>\u8907\u6570\u306e\u6761\u4ef6\u3092\u4e00\u6c17\u306b\u6307\u5b9a<\/h2>\n<p>\u3061\u306a\u307f\u306b\u3001where()\u306f\u591a\u91cd\u914d\u5217\u3092\u6307\u5b9a\u3059\u308b\u3053\u3068\u3067\u8907\u6570\u6761\u4ef6\u3092\u4e00\u5ea6\u306b\u6307\u5b9a\u3059\u308b\u3053\u3068\u304c\u3067\u304d\u307e\u3059\u3002<\/p>\n<pre>$users = \\App\\User::<strong>where([<\/strong>\r\n<strong>    ['id', 2],<\/strong>\r\n<strong>    ['name', 'like', '\u9234\u6728%']<\/strong>\r\n<strong>])<\/strong>-&gt;get();<\/pre>\n<p>\u3082\u3061\u308d\u3093\u3001\u300clike\u300d\u3084\u300c&gt;=\u300d\u306a\u3069\u306e\u6bd4\u8f03\u6f14\u7b97\u5b50\u3082\u4f7f\u3048\u307e\u3059\u3002<\/p>\n<h2>function()\u3092\u4f7f\u3063\u305f\u8907\u96d1\u306awhere()<\/h2>\n<p>\u5b9f\u306f\u3001where()\u306e\u5f15\u6570\u306ffunction()\u3092\u53d7\u3051\u4ed8\u3051\u3066\u304f\u308c\u3066\u3001\u3053\u308c\u3092\u4f7f\u3048\u3070\u8907\u96d1\u306aSQL\u6587\u3092\u3064\u304f\u308b\u3053\u3068\u304c\u3067\u304d\u307e\u3059\u3002<\/p>\n<p>\u4f8b\u3048\u3070\u3001\u4ee5\u4e0b\u306e\u3088\u3046\u306aAND\u691c\u7d22\u3068OR\u691c\u7d22\u304c\u6df7\u3056\u3063\u3066\u3044\u308b\u3088\u3046\u306a\u5834\u5408\u3067\u3059\u3002<\/p>\n<p><strong>\u300c\u540d\u524d\u304c\u9234\u6728\u304b\u3089\u59cb\u307e\u308b\u30c7\u30fc\u30bf\u3067\u3001\u306a\u304a\u304b\u3064\u5e74\u9f62\u304c20\u4ee5\u4e0b\u3082\u3057\u304f\u306f80\u4ee5\u4e0a\u300d<\/strong><\/p>\n<p>\u3064\u307e\u308a\u3001\u300c20\u6b73\u4ee5\u4e0b or 80\u624d\u4ee5\u4e0a\u306e\u9234\u6728\u3055\u3093\u3092\u63a2\u3057\u305f\u3044\u300d\u5834\u5408\u3067\u3059\u3002<\/p>\n<p>\u5b9f\u969b\u306e\u4f8b\u306f\u3053\u3046\u306a\u308a\u307e\u3059\u3002<\/p>\n<pre>$users = \\App\\User::<strong>where('name', 'like', '\u9234\u6728%')<\/strong>\r\n<strong>    -&gt;where(function($q){<\/strong>\r\n\r\n<strong>        $q-&gt;where('age', '&lt;=', 20)<\/strong>\r\n<strong>            -&gt;orWhere('age', '&gt;=', 80);<\/strong>\r\n\r\n<strong>    })<\/strong>\r\n    -&gt;get();<\/pre>\n<p>\u307e\u305a\uff11\u3064\u76ee\u306ewhere()\u306f\u901a\u5e38\u306e\u3042\u3044\u307e\u3044\u691c\u7d22\u3067\u9234\u6728\u3055\u3093\u3060\u3051\u3092\u62bd\u51fa\u3002\u305d\u3057\u3066\u3001\u3055\u3089\u306bwhere()\u3092\u3064\u306a\u3052\u3066AND\u691c\u7d22\u306b\u3059\u308b\u3093\u3067\u3059\u304c\u3001\u3053\u3053\u3067function()\u3092\u4f7f\u3063\u3066<strong>\u300c20\u624d\u4ee5\u4e0b or 80\u624d\u4ee5\u4e0a\u300d\u3068\u3044\u3046OR\u691c\u7d22\u304c\u5b9f\u884c\u3057\u3066\u3044\u307e\u3059\u3002<\/strong><\/p>\n<p>\u91cd\u8981\u306a\u306e\u306f\u3001function()\u3092\u4f7f\u3063\u3066AND\u691c\u7d22\u306e\u4e2d\u306bOR\u691c\u7d22\u304c\u5165\u3063\u3066\u3044\u308b\u3068\u3053\u308d\u3067\u3059\u3002\u3064\u307e\u308a\u3001\u4ee5\u4e0b\u306e\u3088\u3046\u306a\u901a\u5e38\u901a\u308a\u306ewhere()\u306b\u3057\u3066\u3057\u307e\u3046\u3068\u3001\u6700\u5f8c\u306eorWhere()\u304c\u6709\u52b9\u306b\u306a\u3063\u3066<strong>\u9234\u6728\u3055\u3093\u4ee5\u5916\u306e\u30c7\u30fc\u30bf\uff08\u4f8b\u3048\u3070\u300185\u624d\u306e\u4f50\u85e4\u3055\u3093\uff09\u3082\u5165\u3063\u3066\u304d\u3066\u3057\u307e\u3046\u306e\u3067\u3059\u3002<\/strong><\/p>\n<p><strong>\uff08\u9593\u9055\u3063\u305f\u4f8b\uff09<\/strong><\/p>\n<pre>$users = \\App\\User::where('name', 'like', '\u9234\u6728%')\r\n    -&gt;where('age', '&lt;=', 20)\r\n    <strong>-&gt;orWhere('age', '&gt;=', 80)<\/strong>\r\n    -&gt;get();<\/pre>\n<h2>\u7279\u5b9a\u306e\u7bc4\u56f2\u3092\u6307\u5b9a\u3059\u308b<\/h2>\n<h3>whereBetween()\u3067\u7bc4\u56f2\u3092\u6307\u5b9a\u3059\u308b<\/h3>\n<p>\u4f8b\u3048\u3070\u300120\u6b73\u301c30\u6b73\u307e\u3067\u306e\u30e6\u30fc\u30b6\u30fc\u3060\u3051\u53d6\u5f97\u3057\u305f\u3044\u5834\u5408\u306f\u3001whereBetween()\u304c\u4f7f\u3048\u307e\u3059\u3002<\/p>\n<pre>$users = \\App\\User::<strong>whereBetween('age', [20, 30])<\/strong>-&gt;get();<\/pre>\n<p>\u203b\u3053\u306e\u4f8b\u3067\u306f20\u624d\u306e\u30c7\u30fc\u30bf\u308230\u6b73\u306e\u30c7\u30fc\u30bf\u3082\u542b\u307e\u308c\u307e\u3059\u3002<\/p>\n<h3>whereNotBetween\u3067\u7bc4\u56f2\u300c\u4ee5\u5916\u300d\u3092\u6307\u5b9a\u3059\u308b<\/h3>\n<p>\u307e\u305f\u3001whereBetween()\u3068\u306f\u9006\u3067\u6307\u5b9a\u306e\u7bc4\u56f2\u4ee5\u5916\u306e\u30c7\u30fc\u30bf\u3092\u53d6\u5f97\u3059\u308b\u3053\u3068\u3082\u3067\u304d\u307e\u3059\u3002<\/p>\n<pre>$users = \\App\\User::<strong>whereNotBetween('age', [20, 25])<\/strong>-&gt;get();<\/pre>\n<p>\u203b\u3053\u306e\u4f8b\u3067\u306f20\u6b73\u300130\u6b73\u306e\u4eba\u306f\u30c7\u30fc\u30bf\u306b\u5165\u3063\u3066\u306f\u6765\u307e\u305b\u3093\u3002<\/p>\n<h2>\u7279\u5b9a\u306e\u30ab\u30e9\u30e0\u306b\u8907\u6570\u306e\u6761\u4ef6\u3092\u6307\u5b9a\u3059\u308b<\/h2>\n<h3>whereIn()\u3067\u8a72\u5f53\u3059\u308b\u30c7\u30fc\u30bf\u306e\u307f\u53d6\u5f97\u3059\u308b<\/h3>\n<p>\u4f8b\u3048\u3070\u3001\u8907\u6570\u306e\u5e74\u9f62\u304c\u3042\u308a\u3001\u305d\u306e\u5e74\u9f62\u306b\u8a72\u5f53\u3059\u308b\u5168\u3066\u306e\u30c7\u30fc\u30bf\u3092\u53d6\u5f97\u3057\u305f\u3044\u5834\u5408\u3067\u3059\u3002<\/p>\n<pre>$ages = [20, 30, 40];\r\n$users = \\App\\User::<strong>whereIn('age', $ages)<\/strong>-&gt;get();<\/pre>\n<p>\u203b\u3053\u306e\u4f8b\u3067\u306f\u300120\u6b73\u300130\u6b73\u300140\u6b73\u306e\u4eba\u306e\u30c7\u30fc\u30bf\u3060\u3051\u53d6\u5f97\u3055\u308c\u307e\u3059\u3002<\/p>\n<h3>whereNotIn()\u3067\u8a72\u5f53\u300c\u3057\u306a\u3044\u300d\u30c7\u30fc\u30bf\u306e\u307f\u3092\u53d6\u5f97\u3059\u308b<\/h3>\n<p>whereIn()\u3068\u9006\u306e\u691c\u7d22\u65b9\u6cd5\u3067\u3059\u3002\u4ee5\u4e0b\u306e\u4f8b\u306f20\u6b73\u300130\u6b73\u300140\u6b73\u300c\u4ee5\u5916\u300d\u306e\u30c7\u30fc\u30bf\u3092\u53d6\u5f97\u3057\u307e\u3059\u3002<\/p>\n<pre>$ages = [20, 30, 40];\r\n$users = \\App\\User::<strong>whereNotIn('age', $ages)<\/strong>-&gt;get();<\/pre>\n<h2>Null\u3092\u691c\u7d22\u6761\u4ef6\u306b\u3059\u308b<\/h2>\n<h3>whereNull()\u3067null\u306e\u30c7\u30fc\u30bf\u3060\u3051\u691c\u7d22\u3059\u308b<\/h3>\n<p>\u30c7\u30fc\u30bf\u304cNull\u306e\u3082\u306e\u3060\u3051\u3092\u53d6\u5f97\u3059\u308b\u306b\u306fwhereNull()\u3092\u4f7f\u3044\u307e\u3059\u3002<\/p>\n<pre>$users = \\App\\User::<strong>whereNull('age')<\/strong>-&gt;get();<\/pre>\n<p>\u203bSQL\u6587\u3060\u3068\u300c<strong>is NULL<\/strong>\u300d\u3068\u3059\u3079\u304d\u3068\u3053\u308d\u3067\u3059\u304c\u3001\u3053\u3046\u3044\u3046\u3061\u3087\u3063\u3068\u3057\u305f\u4f8b\u5916\u3082\u30d5\u30ec\u30fc\u30e0\u30ef\u30fc\u30af\u304c\u89e3\u6c7a\u3057\u3066\u304f\u308c\u308b\u306e\u3067\u4fbf\u5229\u3067\u3059\u306d\u3002<\/p>\n<h3>whereNotNull()\u3067null\u300c\u4ee5\u5916\u300d\u306e\u30c7\u30fc\u30bf\u3060\u3051\u691c\u7d22\u3059\u308b<\/h3>\n<p>whereNull()\u3068\u306f\u9006\u306bnull\u4ee5\u5916\u306e\u3082\u306e\u3092\u53d6\u5f97\u3059\u308b\u5834\u5408\u306f\u4ee5\u4e0b\u306e\u3088\u3046\u306b\u306a\u308a\u307e\u3059\u3002<\/p>\n<pre>$users = \\App\\User::<strong>whereNotNull('age')<\/strong>-&gt;get();<\/pre>\n<h2>\u6642\u9593\u60c5\u5831\u3067\u691c\u7d22\u3059\u308b<\/h2>\n<h3>whereDate()\u3067\u65e5\u4ed8\u3092\u691c\u7d22<\/h3>\n<p>\u4f8b\u3048\u3070\u3001\u300c2000-01-01\u300d\u3068\u3044\u3046\u65e5\u4ed8\u3067\u691c\u7d22\u3059\u308b\u5834\u5408\u306fwhereDate()\u3092\u4f7f\u3044\u307e\u3059\u3002<\/p>\n<p>\u5b9f\u969b\u306e\u4f8b\u3092\u898b\u3066\u307f\u307e\u3057\u3087\u3046\u3002<\/p>\n<pre>$users = \\App\\User::<strong>whereDate('created_at', '2018-07-02')<\/strong>-&gt;get();<\/pre>\n<p>\u3053\u306e<strong>whereDate()<\/strong>\u304c\u3059\u3054\u3044\u306e\u306f\u3001\u300c\u65e5\u6642\u30c7\u30fc\u30bf\u300d\u3067\u3082\u691c\u7d22\u304c\u53ef\u80fd\u306a\u3068\u3053\u308d\u3067\u3059\u3002<\/p>\n<p>\u4f8b\u3048\u3070\u3001Laravel\u306e\u30c6\u30fc\u30d6\u30eb\u306b\u306f\u30c7\u30d5\u30a9\u30eb\u30c8\u3067\u300ccreated_at\u300d\u3068\u300cupdated_at\u300d\u3068\u3044\u3046\u30c7\u30fc\u30bf\u306e\u300c\u4f5c\u6210\u65e5\u6642\u300d\u3068\u300c\u66f4\u65b0\u65e5\u6642\u300d\u304c\u308f\u304b\u308b\u30ab\u30e9\u30e0\u304c\u3042\u308a\u307e\u3059\u3002\u305f\u3060\u3001\u3053\u306e\u30c7\u30fc\u30bf\u306f\u4ee5\u4e0b\u306e\u3088\u3046\u306b\u300c\u65e5\u4ed8\uff0b\u6642\u9593\u300d\u306b\u306a\u3063\u3066\u3044\u307e\u3059\u3002<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-676\" src=\"https:\/\/blog.capilano-fw.com\/wp-content\/uploads\/2018\/07\/db_timestamps.png\" alt=\"\" width=\"261\" height=\"73\" \/><\/p>\n<p>\u901a\u5e38\u306a\u3089\u6700\u521d\u306e10\u6587\u5b57\u3092\u5207\u308a\u51fa\u3057\u3066\u691c\u7d22\u3059\u308b\u5fc5\u8981\u304c\u3042\u308a\u307e\u3059\u304c\u3001whereDate()\u3092\u4f7f\u3048\u3070\u3001\u65e5\u4ed8\u90e8\u5206\u3060\u3051\u3092\u81ea\u52d5\u7684\u306b\u691c\u7d22\u3057\u3066\u304f\u308c\u308b\u3068\u3044\u3046\u308f\u3051\u3067\u3059\u306d\u3002<\/p>\n<h3>whereYear()\u3067\u300c\u5e74\u300d\u3092\u691c\u7d22<\/h3>\n<p>\u300c\u5e74\u300d\u3067\u30c7\u30fc\u30bf\u53d6\u5f97\u3057\u305f\u3044\u5834\u5408\u3067\u3059\u3002<\/p>\n<p>\u4f8b\u3048\u3070\u30012018\u5e74\u306e\u30c7\u30fc\u30bf\u3060\u3051\u3092\u691c\u7d22\u3059\u308b\u5834\u5408\u306f\u4ee5\u4e0b\u306e\u3088\u3046\u306b\u306a\u308a\u307e\u3059\u3002<\/p>\n<pre>$users = \\App\\User::<strong>whereYear('created_at', '2018')<\/strong>-&gt;get();<\/pre>\n<p>\u203bwhereYear()\u3082whereDate()\u3068\u540c\u3058\u304f\u300c\u65e5\u4ed8\uff0b\u6642\u9593\u300d\u30c7\u30fc\u30bf\u306b\u3082\u6709\u52b9\u3067\u3059\u3002<\/p>\n<h3>whereMonth()\u3067\u300c\u6708\u300d\u3092\u691c\u7d22<\/h3>\n<p>\u540c\u3058\u304f\u300c\u6708\u300d\u3067\u691c\u7d22\u3057\u305f\u3044\u5834\u5408\u3067\u3059\u3002<\/p>\n<p>\u4f8b\u3048\u3070\u30017\u6708\u306e\u30c7\u30fc\u30bf\u3060\u3051\u3092\u53d6\u5f97\u3059\u308b\u5834\u5408\u306f\u4ee5\u4e0b\u306e\u3088\u3046\u306b\u306a\u308a\u307e\u3059\u3002<\/p>\n<pre>$users = \\App\\User::<strong>whereMonth('created_at', '7')<\/strong>-&gt;get();<\/pre>\n<p>\u203b\u6708\u3054\u3068\u306e\u53d7\u6ce8\u30c7\u30fc\u30bf\u306a\u3069\u3092\u53d6\u5f97\u3059\u308b\u5834\u5408\u306b\u4fbf\u5229\u304b\u3082\u3057\u308c\u307e\u305b\u3093\u3002<br \/>\n<span style=\"font-size: 1rem;\">\u203bwhereMonth()\u3082whereDate()\u3068\u540c\u3058\u304f\u300c\u65e5\u4ed8\uff0b\u6642\u9593\u300d\u30c7\u30fc\u30bf\u306b\u3082\u6709\u52b9\u3067\u3059\u3002<\/span><\/p>\n<h3>whereDay()\u3067\u300c\u65e5\u300d\u3092\u691c\u7d22<\/h3>\n<p>\u540c\u3058\u304f\u300c\u65e5\u300d\u3067\u691c\u7d22\u3057\u305f\u3044\u5834\u5408\u3067\u3059\u3002<\/p>\n<p>\u4f8b\u3048\u3070\u30013\u65e5\u306e\u30c7\u30fc\u30bf\u3060\u3051\u3092\u53d6\u5f97\u3059\u308b\u5834\u5408\u306f\u4ee5\u4e0b\u306e\u3088\u3046\u306b\u306a\u308a\u307e\u3059\u3002<\/p>\n<pre>$users = \\App\\User::<strong>whereDay('created_at', '3')<\/strong>-&gt;get();<\/pre>\n<p>\u203b\u6bce\u6708\u25cb\u65e5\u306e\u304a\u5ba2\u69d8\u30b5\u30fc\u30d3\u30b9\u30c7\u30fc\u306e\u60c5\u5831\u3060\u3051\u53d6\u5f97\u3059\u308b\u3001\u3068\u3044\u3063\u305f\u7528\u9014\u306b\u4f7f\u3046\u3068\u4fbf\u5229\u304b\u3082\u3057\u308c\u307e\u305b\u3093\u3002<br \/>\n\u203bwhereDay()\u3082whereDate()\u3068\u540c\u3058\u304f\u300c\u65e5\u4ed8\uff0b\u6642\u9593\u300d\u30c7\u30fc\u30bf\u306b\u3082\u6709\u52b9\u3067\u3059\u3002<\/p>\n<h3>whereTime()\u3067\u300c\u6642\u9593\u300d\u3092\u691c\u7d22<\/h3>\n<p>\u540c\u3058\u304f\u300c\u6642\u9593\u300d\u3067\u691c\u7d22\u3057\u305f\u3044\u5834\u5408\u3067\u3059\u3002<\/p>\n<p>\u4f8b\u3048\u3070\u300100\u664243\u520629\u79d2\u306e\u30c7\u30fc\u30bf\u3060\u3051\u3092\u53d6\u5f97\u3059\u308b\u5834\u5408\u306f\u4ee5\u4e0b\u306e\u3088\u3046\u306b\u306a\u308a\u307e\u3059\u3002<\/p>\n<pre>$users = \\App\\User::<strong>whereTime('created_at', '00:43:29')<\/strong>-&gt;get();<\/pre>\n<p>\u203bwhereDay()\u3082whereDate()\u3068\u540c\u3058\u304f\u300c\u65e5\u4ed8\uff0b\u6642\u9593\u300d\u30c7\u30fc\u30bf\u306b\u3082\u6709\u52b9\u3067\u3059\u3002<br \/>\n\u203b\u6b8b\u5ff5\u306a\u304c\u3089\u6642\u9593\u3001\u5206\u3001\u79d2\u3060\u3051\u3067\u306e\u691c\u7d22\u30e1\u30bd\u30c3\u30c9\u306f\u7528\u610f\u3055\u308c\u3066\u3044\u306a\u3044\u3088\u3046\u3067\u3059\u3002<\/p>\n<h2>\uff12\u3064\u306e\u30ab\u30e9\u30e0\u3092\u6bd4\u8f03\u3057\u3066\u691c\u7d22<\/h2>\n<p>\u4f8b\u3048\u3070\u3001\u767b\u9332\u65e5\u6642\uff08created_at\uff09\u3068\u66f4\u65b0\u65e5\u6642\uff08updated_at\uff09\u304c\u540c\u3058\u30e6\u30fc\u30b6\u30fc\u3001\u3064\u307e\u308a\u300c\u767b\u9332\u5f8c\u306b\u4f55\u3082\u66f4\u65b0\u3057\u3066\u3044\u306a\u3044\u30e6\u30fc\u30b6\u30fc\u300d\u3060\u3051\u3092\u691c\u7d22\u3057\u305f\u3044\u5834\u5408\u306b\u306fwhereColumn()\u3092\u4f7f\u3046\u3068\u3044\u3044\u3067\u3057\u3087\u3046\u3002<\/p>\n<p>\u5b9f\u969b\u306e\u4f8b\u306f\u3053\u3046\u306a\u308a\u307e\u3059\u3002<\/p>\n<pre>$users = \\App\\User::<strong>whereColumn('created_at', 'updated_at')<\/strong>-&gt;get();<\/pre>\n<p>\u306a\u304a\u3001<strong>whereColumn()<\/strong>\u306b\u306f<strong>where()<\/strong>\u3068\u540c\u3058\u304f\u6bd4\u8f03\u6f14\u7b97\u5b50\u304c\u4f7f\u3048\u308b\u306e\u3067\u3001\u4ee5\u4e0b\u306e\u3088\u3046\u306a\u4f7f\u3044\u65b9\u3082\u53ef\u80fd\u3067\u3059\u3002<\/p>\n<pre>$users = \\App\\User::<strong>whereColumn('created_at', '&lt;', 'updated_at')<\/strong>-&gt;get();<\/pre>\n<p>\u203b\u3053\u306e\u4f8b\u306f\u3001\u300c\u767b\u9332\u65e5\u6642\u300d\u3088\u308a\u3082\u300c\u66f4\u65b0\u65e5\u6642\u300d\u306e\u65b9\u304c\u5f8c\u3001\u3064\u307e\u308a\u767b\u9332\u5f8c\u306b\u4f55\u304b\u66f4\u65b0\u3092\u3057\u305f\u30e6\u30fc\u30b6\u30fc\u3060\u3051\u3092\u53d6\u5f97\u3067\u304d\u307e\u3059\u3002<\/p>\n<p>\u3061\u306a\u307f\u306b\u3001\u8907\u6570\u306e\u6761\u4ef6\u3092\u914d\u5217\u306b\u5165\u308c\u3066\u4e00\u6c17\u306b\u691c\u7d22\u3059\u308b\u3053\u3068\u3082\u3067\u304d\u307e\u3059\u3002<\/p>\n<pre>$users = \\App\\User::<strong>whereColumn([<\/strong>\r\n<strong>    ['created_at', 'updated_at'],<\/strong>\r\n<strong>    ['sent_at', '!=', 'updated_at'],<\/strong>\r\n<strong>])<\/strong>-&gt;get();<\/pre>\n<p>\u3053\u308c\u3082<strong>where()<\/strong>\u3068\u540c\u3058\u3067\u3059\u306d\u3002<\/p>\n<h2>whereExists()\u3067\u300c\u3042\u308b\u6761\u4ef6\u300d\u3067\u5b58\u5728\u3059\u308b\u30c7\u30fc\u30bf\u3060\u3051\u3092\u53d6\u5f97\u3059\u308b<\/h2>\n<p>\u4f8b\u3048\u3070\u3001\u300c\u6700\u4f4e\u3067\u30821\u56de\u306f\u554f\u3044\u5408\u308f\u305b\u3092\u3057\u305f\u30e6\u30fc\u30b6\u30fc\u300d\u3060\u3051\u3092\u691c\u7d22\u3057\u305f\u3044\u5834\u5408\u306fwhereExists()\u304c\u4fbf\u5229\u3067\u3059\u3002<\/p>\n<p>\u5b9f\u969b\u306e\u4f8b\u3092\u898b\u3066\u307f\u307e\u3057\u3087\u3046\u3002<\/p>\n<pre>$users = \\App\\User::<strong>whereExists(function($q){<\/strong>\r\n\r\n<strong>    $q-&gt;from('contacts')<\/strong>\r\n<strong>        -&gt;whereRaw('contacts.user_id = users.id');<\/strong>\r\n\r\n<strong>})<\/strong>-&gt;get();<\/pre>\n<p>whereExists()\u306e\u5f15\u6570\u306b\u306ffunction()\u3092\u4f7f\u3044\u3001\u305d\u306e\u4e2d\u3067query\u3092\u4f5c\u3063\u3066\u3044\u307e\u3059\u3002<\/p>\n<p>\u3053\u306e\u65b9\u6cd5\u3092\u4f7f\u3048\u3070\u3001\u300c1\u56de\u306f\u6ce8\u6587\u3092\u3057\u3066\u304f\u308c\u305f\u30e6\u30fc\u30b6\u30fc\u300d\u306a\u3069\u3082\u62bd\u51fa\u3067\u304d\u308b\u305f\u3081\u3001\u8ca9\u58f2\u4fc3\u9032\u306a\u3069\u306b\u6d3b\u7528\u3067\u304d\u308b\u3067\u3057\u3087\u3046\u3002<\/p>\n<h2>JSON\u30c7\u30fc\u30bf\u306e\u4e2d\u8eab\u304b\u3089\u691c\u7d22<\/h2>\n<p>\u5b9f\u306f\u3001Laravel\u306ewhere()\u306fjson\u306e\u4e2d\u8eab\u3092\u691c\u7d22\u3059\u308b\u3053\u3068\u3082\u3067\u304d\u307e\u3059\u3002\u4f8b\u3048\u3070\u4ee5\u4e0b\u306e\u3088\u3046\u306a\u30c7\u30fc\u30bf\u3067\u3059\u3002<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-679\" src=\"https:\/\/blog.capilano-fw.com\/wp-content\/uploads\/2018\/07\/db_json.png\" alt=\"\" width=\"421\" height=\"73\" srcset=\"https:\/\/blog.capilano-fw.com\/wp-content\/uploads\/2018\/07\/db_json.png 421w, https:\/\/blog.capilano-fw.com\/wp-content\/uploads\/2018\/07\/db_json-300x52.png 300w\" sizes=\"auto, (max-width: 421px) 100vw, 421px\" \/><\/p>\n<p>\u3067\u306f\u3001\u3053\u306e\u30c7\u30fc\u30bf\u306e\u4e2d\u304b\u3089\u300cos\u300d\u304c\u300cmac\u300d\u306e\u3082\u306e\u3060\u3051\u53d6\u5f97\u3057\u3066\u307f\u307e\u3057\u3087\u3046\u3002<\/p>\n<pre>$users = \\App\\User::<strong>where('network-&gt;os', 'mac')<\/strong>-&gt;get();<\/pre>\n<p>\u91cd\u8981\u306a\u306e\u306f\u300c-&gt;\u300d\u306e\u90e8\u5206\u3067\u3059\u3002\u3053\u308c\u3067json\u5185\u306e\u30ad\u30fc\u3092\u6307\u5b9a\u3059\u308b\u3053\u3068\u304c\u3067\u304d\u308b\u3093\u3067\u3059\u306d\u3002<\/p>\n<p>\u3082\u3061\u308d\u3093\u3001\u3088\u308a\u968e\u5c64\u304c\u6df1\u304f\u3066\u3082\u691c\u7d22\u3082\u53ef\u80fd\u3067\u3001\u4ee5\u4e0b\u306e\u3088\u3046\u306b\u300c-&gt;\u300d\u3067\u3044\u304f\u3064\u3082\u3064\u306a\u3052\u308b\u3053\u3068\u3067\u5b9f\u88c5\u3067\u304d\u307e\u3059\u3002<\/p>\n<pre>$users = \\App\\User::where('<strong>network-&gt;browser-&gt;version<\/strong>', '67')-&gt;get();<\/pre>\n<p>\u203b\u305f\u3060\u3057\u3001\u3053\u306e\u6a5f\u80fd\u306fMySQL\u3067\u306f\u30d0\u30fc\u30b8\u30e7\u30f3\u304c\u300c5.7.8\u300d\u4ee5\u4e0a\u3067\u30b5\u30dd\u30fc\u30c8\u3055\u308c\u3066\u3044\u308b\u306e\u3067\u53e4\u3044\u74b0\u5883\u3067\u306f\u4f7f\u3048\u306a\u3044\u5834\u5408\u3082\u3042\u308a\u307e\u3059\u3002<\/p>\n<h2>\u30ea\u30ec\u30fc\u30b7\u30e7\u30f3\u5148\u306e\u30c6\u30fc\u30d6\u30eb\u304b\u3089\u691c\u7d22<\/h2>\n<h3>whereHas()\u3067\u30ea\u30ec\u30fc\u30b7\u30e7\u30f3\u5148\u306e\u30c6\u30fc\u30d6\u30eb\u304b\u3089\u691c\u7d22<\/h3>\n<p>\u4f8b\u3048\u3070\u3001\u300c2018\u5e747\u67081\u65e5\u4ee5\u964d\u306b\u9023\u7d61\u3057\u3066\u304f\u308c\u305f\u30e6\u30fc\u30b6\u30fc\u3060\u3051\u3092\u53d6\u5f97\u3059\u308b\u300d\u5834\u5408\u3067\u3059\u3002\u307e\u305a\u3001\u3053\u308c\u3092\u5b9f\u73fe\u3059\u308b\u305f\u3081\u306b\u306f\u901a\u5e38\u306a\u3089\u4ee5\u4e0b\u306e\u624b\u9806\u304c\u5fc5\u8981\u306b\u306a\u3063\u3066\u304d\u307e\u3059\u3002<\/p>\n<ol>\n<li><strong>\u6307\u5b9a\u306e\u65e5\u4ed8\u4ee5\u964d\u306e\u30c7\u30fc\u30bf\u304b\u3089\u8a72\u5f53\u3059\u308b\u30e6\u30fc\u30b6\u30fcID\u3092\u53d6\u5f97<\/strong><\/li>\n<li><strong>\u30e6\u30fc\u30b6\u30fcID\u3092\u4f7f\u3063\u3066\u30e6\u30fc\u30b6\u30fc\u306e\u30c7\u30fc\u30bf\u3092\u53d6\u5f97<\/strong><\/li>\n<\/ol>\n<p>\u3057\u304b\u3057\u3001Laravel\u3067\u306f\u3053\u308c\u3092\u4e00\u6c17\u306b\u5b9f\u73fe\u3059\u308b\u65b9\u6cd5\u304c\u63d0\u4f9b\u3055\u308c\u3066\u3044\u307e\u3059\u3002\u305d\u308c\u304cwhereHas()\u3067\u3059\u3002<\/p>\n<p>\u5b9f\u969b\u306e\u4f8b\u306f\u3053\u3046\u306a\u308a\u307e\u3059\u3002<\/p>\n<pre>$users = \\App\\User::<strong>whereHas('contacts', function($q){<\/strong>\r\n\r\n<strong>    $q-&gt;where('created_at', '&gt;', '2018-07-01');<\/strong>\r\n\r\n<strong>})<\/strong>-&gt;get();<\/pre>\n<p>\u307e\u305a\u7b2c\uff11\u5f15\u6570\u306e\u300ccontacts\u300d\u306f\u30e2\u30c7\u30eb\u5185\u306b\u8ffd\u52a0\u3055\u308c\u305f\u30ea\u30ec\u30fc\u30b7\u30e7\u30f3\u30b7\u30c3\u30d7\u3067\u3001\u4eca\u56de\u306f\u4ee5\u4e0b\u306e\u3088\u3046\u306a<strong>hasMany()<\/strong>\u3067\u3059\u3002<\/p>\n<pre>public function <strong>contacts<\/strong>() {\r\n\r\n    return $this-&gt;<strong>hasMany('App\\Contact')<\/strong>;\r\n\r\n}<\/pre>\n<p>\u305d\u3057\u3066\u3001\u3053\u306e\u30ea\u30ec\u30fc\u30b7\u30e7\u30f3\u5148\u306e\u300cContact\u300d\u30c6\u30fc\u30d6\u30eb\u3067\u691c\u7d22\u3092\u3059\u308b\u305f\u3081\u306b\u7b2c\uff12\u5f15\u6570\u306efunction()\u5185\u3067where()\u3092\u6307\u5b9a\u3057\u3066\u3044\u307e\u3059\u3002\u3064\u307e\u308a\u3001\u3053\u306e\u90e8\u5206\u3067\u691c\u7d22\u3055\u308c\u305f\u30c7\u30fc\u30bf\u306b\u300c\u95a2\u9023\u3057\u305f\u300d\u30e6\u30fc\u30b6\u30fc\u3060\u3051\u3092\u53d6\u5f97\u3059\u308b\u3053\u3068\u304c\u3067\u304d\u308b\u306e\u3067\u3059\u3002\uff08\uff1d2018\u5e74\uff17\u6708\uff11\u65e5\u4ee5\u964d\u306b\u9023\u7d61\u3092\u3057\u3066\u304d\u305f\u30e6\u30fc\u30b6\u30fc\uff09<\/p>\n<h3>orWhereHas()\u3067\u30ea\u30ec\u30fc\u30b7\u30e7\u30f3\u5148\u306e\u30c6\u30fc\u30d6\u30eb\u304b\u3089\u691c\u7d22\uff08OR\u691c\u7d22\uff09<\/h3>\n<p>whereHas()\u306eOR\u691c\u7d22\u30d0\u30fc\u30b8\u30e7\u30f3\u3067\u3059\u3002<br \/>\n\u8a73\u3057\u304f\u306f\u524d\u9805\u76ee\u306e<strong>whereHas()<\/strong>\u3092\u3054\u89a7\u304f\u3060\u3055\u3044\u3002<\/p>\n<h1>\u4e26\u3073\u9806\u3092\u6307\u5b9a\u3059\u308b<\/h1>\n<h2>orderBy()\u3067\u4e26\u3073\u9806\u3092\u6307\u5b9a\u3059\u308b<\/h2>\n<p>\u4e26\u3073\u9806\u3092\u5909\u66f4\u3059\u308b\u306b\u306f<strong>orderBy()<\/strong>\u3092\u4f7f\u3044\u307e\u3059\u3002\u65b9\u6cd5\u306f\u7c21\u5358\u3067\u3001\u300c\u30ab\u30e9\u30e0\u300d\u3068\u300c\u6607\u9806 or \u964d\u9806\u300d\u3092\u6307\u5b9a\u3059\u308b\u3060\u3051\u3067\u3059\u3002<\/p>\n<pre>$users = \\App\\User::<strong>orderBy('id', 'desc')<\/strong>-&gt;get();<\/pre>\n<p>\u3061\u306a\u307f\u306b\u6607\u9806\u3068\u964d\u9806\u306f\u4ee5\u4e0b\u306e\u3088\u3046\u306b\u306a\u308a\u307e\u3059\u3002<\/p>\n<ul>\n<li><strong>asc<\/strong>\u3000\u30fb\u30fb\u30fb\u3000\u6607\u9806\u3002<em>ascending order\u306e\u7565\u3002<\/em>\u3010\u4f8b\u30111,2,3,4,5&#8230;<\/li>\n<li><strong>desc<\/strong>\u3000\u30fb\u30fb\u30fb\u3000\u964d\u9806\u3002descending order\u306e\u7565\u3002\u3010\u4f8b\u30115,4,3,2,1&#8230;<\/li>\n<\/ul>\n<h2>\u65e5\u6642\u3092\u4f7f\u3063\u305f\u4e26\u3073\u66ff\u3048<\/h2>\n<h3>latest()\u3067\u6700\u65b0\u306e\u3082\u306e\u304b\u3089\u964d\u9806\u3067\u4e26\u3073\u66ff\u3048<\/h3>\n<p>\u3082\u3057\u30c7\u30fc\u30bf\u3092\u4f5c\u6210\u3057\u305f\u65e5\u6642\uff08created_at\uff09\u3067\u6700\u65b0\u306e\u3082\u306e\u304b\u3089\u964d\u9806\u3067\u4e26\u3073\u66ff\u3048\u3057\u305f\u3044\u5834\u5408\u306f<strong>latest()<\/strong>\u3092\u4f7f\u3046\u3068\u30b7\u30f3\u30d7\u30eb\u306a\u30b3\u30fc\u30c9\u8a18\u8ff0\u304c\u3067\u304d\u307e\u3059\u3002<\/p>\n<p>\u4f8b\u3048\u3070\u3001\u4ee5\u4e0b\u306e\u3088\u3046\u306b\u306a\u308a\u307e\u3059\u3002<\/p>\n<pre>$users = \\App\\User::<strong>latest()<\/strong>-&gt;get();<\/pre>\n<p>\u3053\u3046\u3059\u308b\u3060\u3051\u3067\u3001\u4ee5\u4e0b\u306e\u540c\u3058\u4e26\u3079\u66ff\u3048\u3092\u3059\u308b\u3053\u3068\u304c\u3067\u304d\u307e\u3059\u3002<\/p>\n<pre>$users = \\App\\User::<strong>orderBy('created_at', 'desc')<\/strong>-&gt;get();<\/pre>\n<p>\u3061\u306a\u307f\u306b\u3001\u30c7\u30d5\u30a9\u30eb\u30c8\u306f\u300ccreated_at\u300d\u3067\u3059\u304c\u3001\u4ee5\u4e0b\u306e\u3088\u3046\u306b\u597d\u304d\u306a\u30ab\u30e9\u30e0\u306b\u5909\u66f4\u3059\u308b\u3053\u3068\u3082\u3067\u304d\u307e\u3059\u3002<\/p>\n<pre>$users = \\App\\User::<strong>latest('updated_at')<\/strong>-&gt;get();<\/pre>\n<p>\u203b\u305f\u3060\u3057\u3001\u30c7\u30fc\u30bf\u6570\u304c\u591a\u3044\u5834\u5408\u3001\u30bd\u30fc\u30c8\u306f\u300cid\u300d\u3092\u4f7f\u3063\u305f\u65b9\u304c\u3044\u3044\u304b\u3082\u3057\u308c\u307e\u305b\u3093\u3002\u4f8b\u3048\u3070\u3001MySQL\u3067\u306f\u30d7\u30e9\u30a4\u30de\u30ea\u30fc\u30ad\u30fc\u306f\u81ea\u52d5\u3067\u30a4\u30f3\u30c7\u30c3\u30af\u30b9\u3092\u4f5c\u6210\u3059\u308b\u306e\u3067\u3001\u305d\u3061\u3089\u306e\u307b\u3046\u304c\u9ad8\u901f\u306b\u5b9f\u884c\u3067\u304d\u308b\u53ef\u80fd\u6027\u304c\u3042\u308b\u304b\u3089\u3067\u3059\u3002<\/p>\n<h3>oldest()\u3067\u53e4\u3044\u3082\u306e\u304b\u3089\u6607\u9806\u3067\u4e26\u3073\u66ff\u3048<\/h3>\n<p>latest()\u3068\u306f\u9006\u3067\u3001\u53e4\u3044\u3082\u306e\u304b\u3089\u6607\u9806\u3067\u30c7\u30fc\u30bf\u3092\u4e26\u3079\u66ff\u3048\u307e\u3059\u3002<\/p>\n<pre>$users = \\App\\User::<strong>oldest()<\/strong>-&gt;get();<\/pre>\n<p>\u306a\u304a\u3001\u3053\u3061\u3089\u3082\u30c7\u30d5\u30a9\u30eb\u30c8\u306e\u30ab\u30e9\u30e0\u306f\u300ccreated_at\u300d\u3067\u3059\u304c\u3001\u597d\u304d\u306a\u3082\u306e\u3092\u6307\u5b9a\u3059\u308b\u3053\u3068\u304c\u3067\u304d\u307e\u3059\u3002<\/p>\n<pre>$users = \\App\\User::<strong>oldest('updated_at')<\/strong>-&gt;get();<\/pre>\n<h2>inRandomOrder()\u3067\u30e9\u30f3\u30c0\u30e0\u306b\u4e26\u3079\u66ff\u3048<\/h2>\n<p>\u3042\u307e\u308a\u4f7f\u3046\u3053\u3068\u306f\u5c11\u306a\u3044\u304b\u3082\u3057\u308c\u307e\u305b\u3093\u304c\u3001\u30e9\u30f3\u30c0\u30e0\u3067\u4e26\u3079\u66ff\u3048\u3092\u3057\u305f\u3044\u5834\u5408\u306e\u30e1\u30bd\u30c3\u30c9\u3082\u3042\u308a\u307e\u3059\u3002SQL\u6587\u3067\u8a00\u3046\u3068\u3053\u308d\u306e\u300c<strong>ORDER BY RAND()<\/strong>\u300d\u3067\u3059\u306d\u3002<\/p>\n<pre>$users = \\App\\User::<strong>inRandomOrder()<\/strong>-&gt;get();<\/pre>\n<p>\u203b\u3053\u306e\u30e1\u30bd\u30c3\u30c9\u306f\u3001\u30c6\u30b9\u30c8\u30c7\u30fc\u30bf\u306a\u3069\u3067\u4f7f\u3046\u3068\u3044\u3044\u304b\u3082\u3057\u308c\u307e\u305b\u3093\u3002<\/p>\n<h1>\u30b0\u30eb\u30fc\u30d7\u5316<\/h1>\n<h2>groupBy()\u3067\u7279\u5b9a\u306e\u30ab\u30e9\u30e0\u3092\u30b0\u30eb\u30fc\u30d7\u5316\u3059\u308b<\/h2>\n<p>SQL\u6587\u3067\u8a00\u3046\u3001\u300cGROUP BY *****\u300d\u3067\u3059\u3002<\/p>\n<p>\u4f8b\u3092\u898b\u3066\u307f\u307e\u3057\u3087\u3046\u3002<\/p>\n<pre>$contacts = \\App\\Contact::select('user_id')-&gt;<strong>groupBy('user_id')<\/strong>-&gt;get();<\/pre>\n<p>\u3061\u306a\u307f\u306b\u4ee5\u4e0b\u306e\u3088\u3046\u306a\u30a8\u30e9\u30fc\u304c\u767a\u751f\u3059\u308b\u5834\u5408\u306f\u3001MySQL 5.7\u304b\u3089\u300cONLY_FULL_GROUP_BY\u300d\u3068\u3044\u3046\u30aa\u30d7\u30b7\u30e7\u30f3\u304c\u30aa\u30f3\u306b\u306a\u3063\u3066\u3044\u308b\u305f\u3081\u306b\u8d77\u3053\u3063\u3066\u3044\u308b\u30a8\u30e9\u30fc\u3067\u3001\u610f\u5473\u306f\u300c\u30b0\u30eb\u30fc\u30d7\u5316\u3057\u305f\u30ab\u30e9\u30e0\u3057\u304bselect\u306f\u3067\u304d\u306a\u3044\u3088\uff01\u300d\u3068\u306a\u308a\u307e\u3059\u3002<\/p>\n<pre><strong>SQLSTATE[42000]: Syntax error or access violation: 1055 Expression #1 of SELECT list is not in GROUP BY clause and contains nonaggregated column<\/strong><\/pre>\n<p>\u3064\u307e\u308a\u3001\u3053\u306e\u5834\u5408\u306f\u3001\u4ee5\u4e0b\u306e\u3088\u3046\u306bgroupBy\u3057\u3066\u3044\u308b\u300cuser_id\u300d\u3082\u3057\u304f\u306f\u3001COUNT()\u306a\u3069\u306e\u96c6\u7d04\u95a2\u6570\u3057\u304b\u4f7f\u3048\u306a\u3044\u3068\u3044\u3046\u3053\u3068\u306b\u306a\u308a\u307e\u3059\u3002<\/p>\n<pre>$contacts = \\App\\Contact::select(\r\n<strong>    'user_id',<\/strong>\r\n<strong>    \\DB::raw('COUNT(id) AS IDS_COUNT')<\/strong>\r\n)\r\n-&gt;groupBy('user_id')\r\n-&gt;get();<\/pre>\n<h2>having()\u3067\u30b0\u30eb\u30fc\u30d7\u5316\u3057\u305f\u5f8c\u306e\u30c7\u30fc\u30bf\u3092\u7d5e\u308a\u8fbc\u3080<\/h2>\n<p>where()\u3068\u540c\u3058\u3088\u3046\u306a\u4f7f\u3044\u65b9\u306b\u306a\u308a\u307e\u3059\u304c\u3001having()\u306f\u30b0\u30eb\u30fc\u30d7\u5316\u3057\u305f\u5f8c\u306e\u30c7\u30fc\u30bf\u3067\u7d5e\u308a\u8fbc\u3080\uff08\u691c\u7d22\u3059\u308b\uff09\u30e1\u30bd\u30c3\u30c9\u3067\u3059\u3002<\/p>\n<p>\u5b9f\u969b\u306e\u4f8b\u3092\u898b\u3066\u307f\u307e\u3057\u3087\u3046\u3002<\/p>\n<pre>$contacts = \\App\\Contact::select(\r\n    'user_id',\r\n    \\DB::raw('COUNT(id) AS IDS_COUNT')\r\n)\r\n-&gt;groupBy('user_id')\r\n<strong>-&gt;having('IDS_COUNT', '&gt;', 3)<\/strong>\r\n-&gt;get();<\/pre>\n<p>\u3053\u306e\u4f8b\u3067\u306f\u3001\u30b0\u30eb\u30fc\u30d7\u5316\u3057\u305f\u4ef6\u6570\u3092id\u3067\u6570\u3048\u3001\u3055\u3089\u306b\u305d\u306e\u4ef6\u6570\u304c3\u4ef6\u3088\u308a\u591a\u3044\u3082\u306e\u3060\u3051\u3092\u53d6\u5f97\u3059\u308b\u30b3\u30fc\u30c9\u3067\u3059\u3002<\/p>\n<h1>skip()\u3067\u53d6\u5f97\u3059\u308b\u4f4d\u7f6e\u3092\u6307\u5b9a\u3059\u308b<\/h1>\n<p>\u30c7\u30fc\u30bf\u30d9\u30fc\u30b9\u306b\u591a\u304f\u306e\u30c7\u30fc\u30bf\u304c\u3042\u308b\u5834\u5408\u3001\u300c\u59cb\u3081\u304b\u308910\u756a\u76ee\u4ee5\u964d\u306e\u30c7\u30fc\u30bf\u3092\u53d6\u5f97\u3057\u305f\u3044\u300d\u3068\u3044\u3046\u5834\u5408\u3082\u3067\u3066\u304d\u307e\u3059\u3002\u305d\u3093\u306a\u5834\u5408\u306fskip()\u3092\u4f7f\u3044\u307e\u3057\u3087\u3046\u3002<\/p>\n<pre>$user = \\App\\User::<strong>skip(10)<\/strong>-&gt;first();<\/pre>\n<p>skip()\u306e\u5f15\u6570\u306f\u3001\u300c\u3069\u308c\u3060\u3051\u30c7\u30fc\u30bf\u3092\u98db\u3070\u3059\u304b\u300d\u306e\u4ef6\u6570\u3067\u3059\u3002\u3064\u307e\u308a\u3001skip(1)\u3068\u3059\u308b\u3068\u3001id\u304c\uff12\u306e\u30c7\u30fc\u30bf\u304c\u53d6\u5f97\u3055\u308c\u308b\u3068\u3044\u3046\u610f\u5473\u306b\u306a\u308a\u307e\u3059\u3002<\/p>\n<p>\u203b\u3061\u306a\u307f\u306b\u3001offset()\u3082\u540c\u3058\u6a5f\u80fd\u3092\u3082\u3064\u30e1\u30bd\u30c3\u30c9\u3067\u3059\u3002<\/p>\n<h1>take()\u3067\u30c7\u30fc\u30bf\u30d9\u30fc\u30b9\u304b\u3089\u53d6\u5f97\u3059\u308b\u4ef6\u6570\u3092\u6307\u5b9a\u3059\u308b<\/h1>\n<p>SQL\u6587\u306b\u6163\u308c\u89aa\u3057\u3093\u3060\u4eba\u306a\u3089\u3088\u304f\u4f7f\u3063\u305f\u3053\u3068\u304c\u3042\u308b\u3001\u300cLIMIT\u6587\u300d\u306b\u306a\u308a\u307e\u3059\u3002<\/p>\n<pre>$user = \\App\\User::<strong>take(5)<\/strong>-&gt;get(); \/\/ 5\u4ef6\u3060\u3051\u53d6\u5f97<\/pre>\n<p>\u203b\u3061\u306a\u307f\u306blimit()\u3082\u540c\u3058\u610f\u5473\u306b\u306a\u308a\u307e\u3059\u3002\u79c1\u3082SQL\u5206\u306b\u6163\u308c\u3066\u3044\u305f\u306e\u3067\u59cb\u3081\u306f\u3053\u3061\u3089\u306e\u65b9\u304c\u76f4\u611f\u7684\u3067\u3057\u305f\u3002<\/p>\n<h1>when()\u3067SQL\u30af\u30a8\u30ea\u30fc\u3092\u6761\u4ef6\u5206\u5c90\u3055\u305b\u308b<\/h1>\n<p>\u3053\u306e\u30e1\u30bd\u30c3\u30c9\u306fif\u6587\u3088\u3046\u306e\u3088\u3046\u306a\u50cd\u304d\u3092\u3059\u308b\u30e1\u30bd\u30c3\u30c9\u3067<strong>true<\/strong> or <strong>false<\/strong>\u306e\uff12\u3064\u306e\u30af\u30a8\u30ea\u30fc\u3092\u5206\u5c90\u3055\u305b\u308b\u3053\u3068\u304c\u3067\u304d\u307e\u3059\u3002<\/p>\n<p>\u5b9f\u969b\u306b\u898b\u3066\u307f\u307e\u3057\u3087\u3046\u3002<\/p>\n<pre><strong>$and = true;<\/strong>\r\n$users = \\App\\User::<strong>when($and, function($q){<\/strong>\r\n\r\n<strong>    \/\/ AND\u691c\u7d22<\/strong>\r\n<strong>    $q-&gt;where('id', request('id'))<\/strong>\r\n<strong>        -&gt;where('name', request('name'));<\/strong>\r\n\r\n<strong>}, function($q){<\/strong>\r\n\r\n<strong>    \/\/ OR\u691c\u7d22<\/strong>\r\n<strong>    $q-&gt;where('id', request('id'))<\/strong>\r\n<strong>        -&gt;orWhere('name', request('name'));<\/strong>\r\n\r\n\r\n<strong>})<\/strong>-&gt;get();<\/pre>\n<p>\u3053\u306e\u4f8b\u306f\u3001\u300c$and\u300d\u304ctrue\u306e\u3068\u304d\u306fAND\u691c\u7d22\u3002\u305d\u3046\u3067\u306a\u3044\u306a\u3089OR\u691c\u7d22\u3092\u3059\u308b\u30b3\u30fc\u30c9\u306b\u306a\u308a\u307e\u3059\u3002<\/p>\n<p>\u3064\u307e\u308a\u3001if\u6587\u3092\u4f7f\u308f\u306a\u304f\u3066\u3082\u6761\u4ef6\u5206\u5c90\u304c\u3067\u304d\u308b\u306e\u3067\u3088\u308a\u53ef\u8aad\u6027\u304c\u9ad8\u3044\u30b3\u30fc\u30c9\u3092\u66f8\u304f\u3053\u3068\u304c\u3067\u304d\u308b\u308f\u3051\u3067\u3059\u306d\u3002<\/p>\n<h1>\u96c6\u7d04\u3057\u305f\u30c7\u30fc\u30bf\u3092\u53d6\u5f97\u3059\u308b<\/h1>\n<h2>count()\u3067\u53d6\u5f97\u30c7\u30fc\u30bf\u306e\u4ef6\u6570\u3092\u53d6\u5f97\u3059\u308b<\/h2>\n<p>\u53d6\u5f97\u3057\u305f\u30c7\u30fc\u30bf\u306e\u4ef6\u6570\u3092\u77e5\u308a\u305f\u3044\u5834\u5408\u306fcount()\u3092\u4f7f\u3044\u307e\u3059\u3002<\/p>\n<pre>$count = \\App\\User::<strong>count();<\/strong><\/pre>\n<h2>max()\u3067\u6700\u5927\u5024\u3092\u53d6\u5f97\u3059\u308b<\/h2>\n<p>max()\u3092\u4f7f\u3046\u3068\u6307\u5b9a\u3057\u305f\u30ab\u30e9\u30e0\u306e\u6700\u5927\u5024\u3092\u53d6\u5f97\u3059\u308b\u3053\u3068\u304c\u3067\u304d\u307e\u3059\u3002<br \/>\n\u4f8b\u3048\u3070\u3001\u300c\u80cc\u304c\u4e00\u756a\u9ad8\u3044\u4eba\u306e\u30c7\u30fc\u30bf\u300d\u3092\u53d6\u5f97\u3059\u308b\u5834\u5408\u306f\u4ee5\u4e0b\u306e\u3088\u3046\u306b\u306a\u308a\u307e\u3059\u3002<\/p>\n<pre>$max_height = \\App\\User::<strong>max('height')<\/strong>;<\/pre>\n<h2>avg()\u3067\u5e73\u5747\u5024\u3092\u53d6\u5f97\u3059\u308b<\/h2>\n<p>avg()\u3092\u4f7f\u3046\u3068\u5e73\u5747\u5024\u3092\u53d6\u5f97\u3059\u308b\u3053\u3068\u304c\u3067\u304d\u307e\u3059\u3002\u300caverage\u300d\u306e\u7701\u7565\u5f62\u3067\u3059\u306d\u3002<br \/>\n\u4f8b\u3048\u3070\u3001\u300c\u5e73\u5747\u5e74\u9f62\u300d\u3092\u77e5\u308a\u305f\u3044\u5834\u5408\u306f\u4ee5\u4e0b\u306e\u3088\u3046\u306b\u3057\u307e\u3059\u3002<\/p>\n<pre>$avg_age = \\App\\User::<strong>avg('age')<\/strong>;<\/pre>\n<h1>\u30c7\u30fc\u30bf\u304c\u5b58\u5728\u3057\u3066\u3044\u308b\u304b\u3069\u3046\u304b\u3092\u30c1\u30a7\u30c3\u30af\u3059\u308b<\/h1>\n<h2>exists()\u3067\u30c7\u30fc\u30bf\u306e\u5b58\u5728\u3092\u30c1\u30a7\u30c3\u30af<\/h2>\n<p>exists()\u3092\u4f7f\u3046\u3068\u3001\u30c7\u30fc\u30bf\u30d9\u30fc\u30b9\u5185\u306b\u30c7\u30fc\u30bf\u304c\u5b58\u5728\u3057\u3066\u3044\u308b\u304b\u3092\u30c1\u30a7\u30c3\u30af\u3059\u308b\u3053\u3068\u304c\u3067\u304d\u307e\u3059\u3002<\/p>\n<pre>$exists = \\App\\User::<strong>where('id', 1)-&gt;exists();<\/strong>\r\n\r\nif($exists) {\r\n\r\n    echo '\u5b58\u5728\u3059\u308b\uff01';\r\n\r\n}<\/pre>\n<p>\u3053\u308c\u306f\u3001id\u304c\uff11\u306e\u30c7\u30fc\u30bf\u304c\u5b58\u5728\u3057\u3066\u3044\u308b\u304b\u3069\u3046\u304b\u3092\u30c1\u30a7\u30c3\u30af\u3059\u308b\u30b3\u30fc\u30c9\u3067\u3001\u53d6\u5f97\u3067\u304d\u308b\u30c7\u30fc\u30bf\u306f\u300c<strong>true<\/strong>\u300d\u304b\u300c<strong>false<\/strong>\u300d\u3067\u3059\u3002<\/p>\n<p>\u3053\u3053\u3067\u91cd\u8981\u306a\u306e\u306f\u3001\u30c7\u30fc\u30bf\u304c\uff11\u4ef6\u3067\u3082\u5b58\u5728\u3057\u3066\u3044\u308c\u3070\u300ctrue\u300d\u306b\u306a\u308b\u3068\u3053\u308d\u3067\u3059\u3002<\/p>\n<p>\u4f8b\u3048\u3070\u3001\u6b21\u306e\u30b3\u30fc\u30c9\u3067\u3059\u3002\u4eca\u3001\u30c7\u30fc\u30bf\u30d9\u30fc\u30b9\u306b\u306fID\u300c1\u300d\u300c2\u300d\u300c3\u300d\u3057\u304b\u5b58\u5728\u3057\u3066\u3044\u306a\u3044\u3068\u3057\u307e\u3059\u3002whereIn()\u306e\u4e2d\u306b\u306f\u5b9f\u969b\u306b\u5b58\u5728\u3059\u308b\u300c3\u300d\u304c\u6307\u5b9a\u3055\u308c\u3066\u3044\u308b\u306e\u3067\u3053\u306e\u5834\u5408\u306f\u300c4\u300d\u3068\u300c5\u300d\u306f\u898b\u3064\u304b\u3089\u306a\u3044\u3051\u308c\u3069\u3082\u3001$exists\u306f\u300c<strong>true<\/strong>\u300d\u3068\u306a\u308a\u307e\u3059\u3002<\/p>\n<pre>$exists = \\App\\User::<strong>whereIn('id', [3,4,5])<\/strong>-&gt;exists();<\/pre>\n<h2>doesntExist()\u3067\u30c7\u30fc\u30bf\u304c\u5b58\u5728\u300c\u3057\u306a\u3044\u304b\u300d\u3092\u30c1\u30a7\u30c3\u30af<\/h2>\n<p>exists()\u3068\u306f\u9006\u306b\u5b58\u5728\u300c\u3057\u306a\u3044\u304b\u300d\u3069\u3046\u304b\u3092\u30c1\u30a7\u30c3\u30af\u3059\u308b\u305f\u3081\u306e\u30e1\u30bd\u30c3\u30c9\u3067\u3059\u3002<\/p>\n<pre>$doesnt_exists = \\App\\User::<strong>where('id', 1)-&gt;doesntExist()<\/strong>;\r\n\r\nif($doesnt_exists) {\r\n\r\n    echo '\u5b58\u5728\u3057\u306a\u3044\uff01';\r\n\r\n}<\/pre>\n<p>\u3053\u306e\u30b3\u30fc\u30c9\u306f\u30c7\u30fc\u30bf\u304c\u5b58\u5728\u3057\u306a\u3044\u5834\u5408\u306b\u300c\u5b58\u5728\u3057\u306a\u3044\uff01\u300d\u3068\u8868\u793a\u3055\u308c\u308b\u3053\u3068\u306b\u306a\u308a\u307e\u3059\u3002<\/p>\n<p>\u6587\u5b57\u3067\u306f\u5c11\u3057\u8aac\u660e\u304c\u96e3\u3057\u3044\u3067\u3059\u304c\u3001doesntExist()\u306f\u300c\u5b58\u5728\u3057\u306a\u3044\u300d\u5834\u5408\u300c<strong>true<\/strong>\u300d\u3092\u8fd4\u3057\u3001\u300c\u5b58\u5728\u3059\u308b\u300d\u5834\u5408\u306f\u300c<strong>false<\/strong>\u300d\u3092\u8fd4\u3057\u307e\u3059\u3002<\/p>\n<p>\u305f\u3060\u3001\u3053\u306e\u8fba\u306f\u5c11\u3057\u7406\u89e3\u304c\u3084\u3084\u3053\u3057\u3044\u306e\u3067\u3001\u3067\u304d\u308b\u3060\u3051\u76f4\u611f\u7684\u306b\u3064\u304b\u3048\u308b<strong>exists()<\/strong>\u3092\u4f7f\u3046\u3053\u3068\u3092\u304a\u3059\u3059\u3081\u3057\u307e\u3059\u3002<\/p>\n<h1>SQL\u6587\u3092\u305d\u306e\u307e\u307e\u6307\u5b9a\u3059\u308b<\/h1>\n<h2>\\DB::raw()\u3067SQL\u6587\u3092\u76f4\u66f8\u304d\u3059\u308b<\/h2>\n<p>Laravel\u3067\u306fDB\u30af\u30a8\u30ea\u30fc\u306e\u305f\u3081\u306e\u30e1\u30bd\u30c3\u30c9\u304c\u4e8b\u7d30\u304b\u304f\u63d0\u4f9b\u3055\u308c\u3066\u3044\u308b\u306e\u3067\u3001\u307b\u307c\u3069\u3093\u306a\u5834\u5408\u3067\u3082\u305d\u308c\u3089\u3067\u5bfe\u5fdc\u3067\u304d\u308b\u3088\u3046\u306b\u306a\u3063\u3066\u3044\u307e\u3059\u3002<\/p>\n<p>\u305f\u3060\u3001\u3084\u306f\u308a\u4f8b\u5916\u3068\u3044\u3046\u3082\u306e\u306f\u5b58\u5728\u3057\u3066\u3044\u3066\u3001\u307e\u308c\u306b\u76f4\u63a5SQL\u6587\u3092\u66f8\u304d\u305f\u3044\u5834\u5408\u3082\u3067\u3066\u304f\u308b\u3068\u601d\u3044\u307e\u3059\u3002\u305d\u3093\u306a\u5834\u5408\u306b\u306f<strong>\\DB::raw()<\/strong>\u3092\u4f7f\u3063\u3066SQL\u6587\u3092\u76f4\u66f8\u304d\u3057\u307e\u3057\u3087\u3046\u3002<\/p>\n<p>\u4f8b\u3048\u3070\u4ee5\u4e0b\u306e\u3088\u3046\u306a\u3001SQL\u306e\u4e2d\u306b\u300cCOUNT()\u300d\u306a\u3069\u306e\u96c6\u7d04\u95a2\u6570\u304c\u5fc5\u8981\u306a\u5834\u5408\u306a\u3069\u3067\u3059\u3002<\/p>\n<pre>$users = \\App\\User::select(<strong>\\DB::raw('COUNT(id) as id_count')<\/strong>)-&gt;get();<\/pre>\n<p>\u3061\u306a\u307f\u306b\u300craw\u300d\u3068\u306f\u300c\u751f\uff08\u306a\u307e\uff09\u306e\u300d\u3068\u3044\u3046\u610f\u5473\u3067\u3059\u3002\u300craw fish\u300d\u306f\u65e5\u672c\u8a9e\u3067\u300c\u751f\u9b5a\u300d\u3002\u3064\u307e\u308a\u3001SQL\u306e\u300c\u751f\u306e\u6587\u7ae0\u300d\u3068\u3044\u3046\u3053\u3068\u3067\u3059\u306d\u3002<\/p>\n<h2>selectRaw()\u3067select\u3067SQL\u6587\u3092\u4f7f\u3046<\/h2>\n<p>selectRaw()\u3092\u4f7f\u3046\u3068\u3001\\DB::raw()\u3092\u4f7f\u308f\u305a\u3088\u308a\u53ef\u8aad\u6027\u304c\u9ad8\u3044\u30b3\u30fc\u30c9\u3092\u66f8\u304f\u3053\u3068\u304c\u3067\u304d\u307e\u3059\u3002<\/p>\n<pre>$users = \\App\\User::<strong>selectRaw('COUNT(id) as id_count')<\/strong>-&gt;get();<\/pre>\n<h2>whereRaw()\u3067where\u53e5\u306bSQL\u6587\u3092\u4f7f\u3046<\/h2>\n<p>whereRaw()\u3092\u4f7f\u3048\u3070\u3001where\u53e5\u306bSQL\u6587\u3092\u76f4\u66f8\u304d\u3059\u308b\u3053\u3068\u304c\u3067\u304d\u307e\u3059\u3002<\/p>\n<p>\u4f8b\u3048\u3070\u3001\u540d\u524d\u304c\uff08\u30b7\u30f3\u30b0\u30eb\u30d0\u30a4\u30c8\u3067\uff0910\u6587\u5b57\u4ee5\u4e0a\u306e\u30e6\u30fc\u30b6\u30fc\u3092\u53d6\u5f97\u3059\u308b\u5834\u5408\u3067\u3059\u3002<\/p>\n<pre>$users = \\App\\User::<strong>whereRaw('LENGTH(name) &gt; 10')<\/strong>-&gt;get();<\/pre>\n<h2>orWhereRaw()\u3067where\u53e5\u306bSQL\u6587\u3092\u4f7f\u3046\uff08OR\u691c\u7d22\uff09<\/h2>\n<p>whereRaw()\u306eOR\u691c\u7d22\u30d0\u30fc\u30b8\u30e7\u30f3\u3067\u3059\u3002\u8a73\u3057\u304f\u306f\u4e0a\u306e\u9805\u76ee\u3092\u3054\u89a7\u304f\u3060\u3055\u3044\u3002<\/p>\n<pre>$users = \\App\\User::where('id', 1)\r\n    <strong>-&gt;orWhereRaw('LENGTH(name) &gt; 10')<\/strong>\r\n    -&gt;get();<\/pre>\n<h2>havingRaw()\u3067having\u53e5\u306bSQL\u6587\u3092\u4f7f\u3046<\/h2>\n<p>having\u53e5\u306f\u30b0\u30eb\u30fc\u30d7\u5316\u3057\u305f\u5f8c\u306e\u30c7\u30fc\u30bf\u3092\u7d5e\u308a\u8fbc\u3080\u3082\u306e\u3067\u3001\u3053\u308c\u306bSQL\u6587\u3092\u76f4\u66f8\u304d\u3057\u305f\u3044\u5834\u5408\u306bhavingRaw()\u3092\u4f7f\u3044\u307e\u3059\u3002<\/p>\n<p>\u4f8b\u3048\u3070\u3001\u300c\u9023\u7d61\u3092\u3057\u3066\u304d\u305f\u4ef6\u6570\u304c\uff15\u4ef6\u3088\u308a\u591a\u3044\u30e6\u30fc\u30b6\u30fcID\u300d\u3092\u53d6\u5f97\u3059\u308b\u5834\u5408\u306f\u4ee5\u4e0b\u306e\u3088\u3046\u306b\u306a\u308a\u307e\u3059\u3002<\/p>\n<pre>$contacts = \\App\\Contact::select('user_id')\r\n    -&gt;groupBy('user_id')\r\n    <strong>-&gt;havingRaw('COUNT(user_id) &gt; 5')<\/strong>\r\n    -&gt;get();<\/pre>\n<h2>orHavingRaw()\u3067having\u53e5\u306bSQL\u6587\u3092\u4f7f\u3046\uff08OR\u691c\u7d22\uff09<\/h2>\n<p>havingRaw()\u306eOR\u691c\u7d22\u30d0\u30fc\u30b8\u30e7\u30f3\u3067\u3059\u3002\u8a73\u3057\u304f\u306f\u4e0a\u306e\u9805\u76ee\u3092\u3054\u89a7\u304f\u3060\u3055\u3044\u3002<\/p>\n<pre>$contacts = \\App\\Contact::select('user_id')\r\n    -&gt;groupBy('user_id')\r\n    -&gt;havingRaw('COUNT(user_id) &lt; 3')\r\n    <strong>-&gt;orHavingRaw('COUNT(user_id) &gt; 10')<\/strong>\r\n    -&gt;get();<\/pre>\n<h2>orderByRaw()\u3067ORDER\u53e5\u306bSQL\u6587\u3092\u4f7f\u3046<\/h2>\n<p>orderByRaw()\u3092\u4f7f\u3046\u3068ORDER\u53e5\u306b\u76f4\u63a5SQL\u6587\u3092\u4f7f\u3046\u3053\u3068\u304c\u3067\u304d\u307e\u3059\u3002<\/p>\n<pre>$users = \\App\\User::<strong>orderByRaw('RAND()')<\/strong>-&gt;get();<\/pre>\n<h1>\u30c6\u30fc\u30d6\u30eb\u3092\u7d50\u5408\u3055\u305b\u308b<\/h1>\n<p>\u203b\u305b\u3063\u304b\u304fLaravel\u306b\u306f\u30ea\u30ec\u30fc\u30b7\u30e7\u30f3\u30b7\u30c3\u30d7\u6a5f\u80fd\u304c\u5099\u308f\u3063\u3066\u3044\u308b\u306e\u3067\u3001\u7279\u5225\u306a\u7406\u7531\u304c\u306a\u3044\u5834\u5408\u306f\u3001Eloquent\u30e2\u30c7\u30eb\u306e<strong>hasOne<\/strong>\u3084<strong>hasMany<\/strong>\u306a\u3069\u3092\u4f7f\u3046\u3053\u3068\u3092\u304a\u3059\u3059\u3081\u3057\u307e\u3059\u3002<\/p>\n<h2>join()\u3067\u5185\u90e8\u7d50\u5408\u3059\u308b<\/h2>\n<p>join()\u3092\u4f7f\u3046\u3068SQL\u6587\u3067\u8a00\u3046\u3068\u3053\u308d\u306e\u300cINNER JOIN\u300d\u3092\u5b9f\u88c5\u3059\u308b\u3053\u3068\u304c\u3067\u304d\u307e\u3059\u3002<\/p>\n<pre>$user_contacts = \\DB::table('users')\r\n            -&gt;select('contacts.id', 'users.name')\r\n            <strong>-&gt;join('contacts', 'users.id', '=', 'contacts.user_id')<\/strong>\r\n            -&gt;get();<\/pre>\n<p>\u5f15\u6570\u306e\u610f\u5473\u306f\u4ee5\u4e0b\u306e\u3068\u304a\u308a\u3067\u3059\u3002<\/p>\n<ul>\n<li><strong>\u7b2c\uff11\u5f15\u6570\u3000\u30fb\u30fb\u30fb\u3000\u7d50\u5408\u3057\u305f\u3044\u30c6\u30fc\u30d6\u30eb\u306e\u540d\u524d<\/strong><\/li>\n<li><strong>\u7b2c\uff12\u3001\uff14\u5f15\u6570\u3000\u30fb\u30fb\u30fb\u3000\u7d50\u5408\u3057\u305f\u3044\u30ab\u30e9\u30e0<\/strong><\/li>\n<li><strong>\u7b2c\uff13\u5f15\u6570\u3000\u30fb\u30fb\u30fb\u3000\u6bd4\u8f03\u6f14\u7b97\u5b50\uff08\u57fa\u672c\u7684\u306b\u306f\u300c=\u300d\u3067OK\u3067\u3059\uff09<\/strong><\/li>\n<\/ul>\n<h2>leftJoin()\u3067\u5916\u90e8\u7d50\u5408\u3059\u308b<\/h2>\n<p>SQL\u6587\u306e\u300cLEFT JOIN\u300d\u3092\u5b9f\u88c5\u3059\u308b\u30e1\u30bd\u30c3\u30c9\u3067\u3059\u3002\u5f15\u6570\u306e\u5185\u5bb9\u306fjoin()\u3068\u540c\u3058\u306a\u306e\u3067\u3001\u4e0a\u306e\u9805\u76ee\u3092\u3054\u89a7\u304f\u3060\u3055\u3044\u3002<\/p>\n<pre>$user_contacts = \\DB::table('users')\r\n            -&gt;select('contacts.id', 'users.name')\r\n            <strong>-&gt;leftJoin('contacts', 'users.id', '=', 'contacts.user_id')<\/strong>\r\n            -&gt;get();<\/pre>\n<h2>crossJoin()\u3067\u30af\u30ed\u30b9\u7d50\u5408\u3059\u308b<\/h2>\n<p>SQL\u5206\u306e\u300cCROSS JOIN\u300d\u3092\u5b9f\u884c\u3059\u308b\u30e1\u30bd\u30c3\u30c9\u3067\u3059\u3002<\/p>\n<pre>$user_contacts = \\DB::table('users')\r\n            -&gt;select('contacts.id', 'users.name')\r\n            <strong>-&gt;crossJoin('contacts')<\/strong>\r\n            -&gt;get();<\/pre>\n<h2>\u8907\u96d1\u306a\u7d50\u5408<\/h2>\n<p>\u3042\u307e\u308a\u5229\u7528\u3059\u308b\u983b\u5ea6\u306f\u306a\u3044\u304b\u3082\u3057\u308c\u307e\u305b\u3093\u304c\u3001\u8907\u96d1\u306a\u7d50\u5408\u3092\u3059\u308b\u306b\u306f\u4ee5\u4e0b\u306e\u3088\u3046\u306bjoin()\u5185\u306bfunction()\u3092\u6307\u5b9a\u3059\u308b\u3053\u3068\u3067\u5b9f\u88c5\u304c\u53ef\u80fd\u3067\u3059\u3002<\/p>\n<pre>$user_contacts = \\DB::table('users')\r\n    <strong>-&gt;join('contacts', function ($join) {<\/strong>\r\n\r\n<strong>        $join-&gt;on('users.id', '=', 'contacts.user_id')<\/strong>\r\n<strong>            -&gt;orOn('users.created_at', '=', 'contacts.created_at')<\/strong>\r\n<strong>            -&gt;where('contacts.created_at', '&gt; ', '2018-01-01 00:00:00');<\/strong>\r\n\r\n<strong>    })<\/strong>-&gt;get();<\/pre>\n<p>function()\u5185\u306b\u306f\u3001on(), orOn(), where(), orWhere\u306a\u3069\u304c\u4f7f\u3048\u307e\u3059\u3002<\/p>\n<h1>union()\u3067\uff12\u3064\u306e\u30c6\u30fc\u30d6\u30eb\u30c7\u30fc\u30bf\u3092\uff11\u3064\u306b\u3059\u308b<\/h1>\n<p>\uff12\u3064\u306e\u30c6\u30fc\u30d6\u30eb\u30c7\u30fc\u30bf\u3092\u4e00\u3064\u306b\u307e\u3068\u3081\u305f\u3044\u5834\u5408\u306f<strong>union()<\/strong>\u304c\u4f7f\u3048\u307e\u3059\u3002<\/p>\n<pre>$query_1 = \\App\\User::where('id', '&lt;', 3);\r\n$query_2 = \\App\\User::where('id', '&gt;', 5);\r\n$users = $query_2-&gt;<strong>union($query_1)<\/strong>-&gt;get();<\/pre>\n<p>\u3053\u3053\u3067\u91cd\u8981\u306a\u306e\u306f\u3001union()\u306e\u4e2d\u306b\u5165\u308c\u308b\u306e\u306f\u300c\u30af\u30a8\u30ea\u30fc\u300d\u3060\u3068\u3044\u3046\u3053\u3068\u3067\u3059\u3002<strong>\u3064\u307e\u308a\u3001get()\u3084first()\u306f\u4e0d\u8981\u3067\u3059\u3002<\/strong><\/p>\n<h1>\u30da\u30fc\u30b8\u3054\u3068\u306b\u30c7\u30fc\u30bf\u3092\u53d6\u5f97\u3059\u308b\uff08Pagination\uff09<\/h1>\n<h2>paginate()\u3067\u30da\u30fc\u30b8\u3054\u3068\u306e\u30c7\u30fc\u30bf\u3092\u53d6\u5f97<\/h2>\n<p>\u30c7\u30fc\u30bf\u30d9\u30fc\u30b9\u306e\u64cd\u4f5c\u3067\u300c\u7c21\u5358\u305d\u3046\u3067\u5b9f\u306f\u30ed\u30b8\u30c3\u30af\u304c\u96e3\u3057\u3044\u958b\u767a\u300d\u306e\u3072\u3068\u3064\u306b\u300cPagination\uff08\u30da\u30fc\u30b8\u7ba1\u7406\uff09\u300d\u304c\u3042\u308a\u307e\u3059\u3002<\/p>\n<p>Laravel\u3067\u306f\u3053\u306e\u30da\u30fc\u30b8\u7ba1\u7406\u3082\u76f4\u611f\u7684\u306b\u4f7f\u3048\u308b\u3088\u3046\u306a\u6a5f\u80fd\u304c\u5099\u308f\u3063\u3066\u3044\u308b\u306e\u3067\u4ee5\u4e0b\u306b\u7d39\u4ecb\u3057\u305f\u3044\u3068\u601d\u3044\u307e\u3059\u3002<\/p>\n<p>\u3067\u306f\u3001\u5b9f\u4f8b\u3067\u3059\u3002<\/p>\n<pre>$per_page = 5;\r\n$users = \\App\\User::<strong>paginate($per_page)<\/strong>;<\/pre>\n<p>\u3053\u306e\u4f8b\u3067\u306f\u3001\uff15\u4ef6\u305a\u3064\u30e6\u30fc\u30b6\u30fc\u30fb\u30c7\u30fc\u30bf\u3092\u53d6\u5f97\u3057\u3066\u3044\u307e\u3059\u3002<\/p>\n<p>\u306a\u304a\u3001paginate()\u3092\u4f7f\u3063\u3066\u53d6\u5f97\u3057\u305f\u30c7\u30fc\u30bf\u306fget()\u3067\u53d6\u5f97\u3057\u305f\u3082\u306e\u3068\u540c\u3058\u304f\u4ee5\u4e0b\u306e\u3088\u3046\u306bforeach()\u3067\u53d6\u5f97\u3059\u308b\u3053\u3068\u304c\u3067\u304d\u307e\u3059\u3002<\/p>\n<pre><strong>foreach ($users as $user) {<\/strong>\r\n\r\n<strong>    echo $user-&gt;name;<\/strong>\r\n\r\n<strong>}<\/strong><\/pre>\n<p>\u305f\u3060\u3001get()\u3068\u9055\u3046\u306e\u306f\u901a\u5e38\u306e\u30c7\u30fc\u30bf\u3060\u3051\u3067\u306f\u306a\u304f\u3001\u69d8\u3005\u306a\u30da\u30fc\u30b8\u60c5\u5831\u3092\u53d6\u5f97\u3067\u304d\u308b\u3068\u3053\u308d\u3067\u3059\u3002<\/p>\n<p>\u305d\u308c\u305e\u308c\u5b9f\u4f8b\u30b3\u30fc\u30c9\u3092\u898b\u3066\u3044\u304d\u307e\u3057\u3087\u3046\u3002<\/p>\n<h3>count()\u3067\u8868\u793a\u3057\u3066\u3044\u308b\u30da\u30fc\u30b8\u306e\u30c7\u30fc\u30bf\u4ef6\u6570\u3092\u53d6\u5f97\u3059\u308b<\/h3>\n<p>\u73fe\u5728\u8868\u793a\u3057\u3066\u3044\u308b\u30da\u30fc\u30b8\u306e\u30c7\u30fc\u30bf\u4ef6\u6570\u3067\u3059\u3002\u3082\u3057\u3001\u300c\u5168\u90e8\u3067\u4f55\u4ef6\u53d6\u5f97\u3057\u305f\u304b\u300d\u3092\u30c1\u30a7\u30c3\u30af\u3057\u305f\u3044\u5834\u5408\u306ftotal()\u3092\u4f7f\u3044\u307e\u3057\u3087\u3046\u3002<\/p>\n<pre>echo $users-&gt;<strong>count()<\/strong>;<\/pre>\n<h3>currentPage()\u3067\u73fe\u5728\u306e\u30da\u30fc\u30b8\u756a\u53f7\u3092\u53d6\u5f97\u3059\u308b<\/h3>\n<pre>echo $users-&gt;<strong>currentPage()<\/strong>;<\/pre>\n<h3>firstItem()\u3067\u6700\u521d\u306e\u30c7\u30fc\u30bf\u756a\u53f7\u3092\u53d6\u5f97\u3059\u308b<\/h3>\n<p>\u8868\u793a\u30da\u30fc\u30b8\u306e\u30c7\u30fc\u30bf\u306e\u4e2d\u304b\u3089\u4e00\u756a\u6700\u521d\u306e\u30c7\u30fc\u30bf\u756a\u53f7\u3092\u53d6\u5f97\u3059\u308b\u3002\u3064\u307e\u308a\u3001<strong>paginate(3);<\/strong>\u3068\u3057\u3066\u304a\u3044\u3066\u3001\u73fe\u5728\uff11\u30da\u30fc\u30b8\u76ee\u306b\u3044\u308b\u306a\u3089\u300c\uff11\u300d\u3001\uff12\u30da\u30fc\u30b8\u76ee\u306b\u3044\u308b\u306a\u3089\u300c\uff14\u300d\u3068\u306a\u308a\u307e\u3059\u3002\u30da\u30fc\u30b8from\u301cto\u306e\u300cfrom\u300d\u3002<\/p>\n<pre>echo $users-&gt;<strong>firstItem()<\/strong>;<\/pre>\n<h3>hasMorePages()\u3067\u307e\u3060\u8868\u793a\u3059\u3079\u304d\u30c7\u30fc\u30bf\u304c\u6b8b\u3063\u3066\u3044\u308b\u304b\u30c1\u30a7\u30c3\u30af\u3059\u308b<\/h3>\n<p>\u4f8b\u3048\u3070\u3001\u73fe\u5728\uff12\u30da\u30fc\u30b8\u76ee\u3092\u8868\u793a\u3057\u3066\u3044\u3066\u3001\u6b21\u306e\uff13\u30da\u30fc\u30b8\u76ee\u304c\u5b58\u5728\u3057\u3066\u3044\u308b\u304b\u3069\u3046\u304b\u3092\u30c1\u30a7\u30c3\u30af\u3059\u308b\u5834\u5408\u3067\u3059\u3002<\/p>\n<pre>if($users-&gt;<strong>hasMorePages()<\/strong>) {\r\n\r\n    echo '\u307e\u3060\u8868\u793a\u3059\u308b\u30da\u30fc\u30b8\u304c\u6b8b\u3063\u3066\u3044\u307e\u3059\uff01';\r\n\r\n}<\/pre>\n<h3>lastItem()\u3067\u6700\u5f8c\u306e\u30c7\u30fc\u30bf\u756a\u53f7\u3092\u53d6\u5f97\u3059\u308b<\/h3>\n<p>\u8868\u793a\u30da\u30fc\u30b8\u306e\u30c7\u30fc\u30bf\u306e\u4e2d\u304b\u3089\u4e00\u756a\u6700\u5f8c\u306e\u30c7\u30fc\u30bf\u756a\u53f7\u3092\u53d6\u5f97\u3059\u308b\u3002\u3064\u307e\u308a\u3001<strong>paginate(3);<\/strong>\u3068\u3057\u3066\u304a\u3044\u3066\u3001\u73fe\u5728\uff11\u30da\u30fc\u30b8\u76ee\u306b\u3044\u308b\u306a\u3089\u300c\uff13\u300d\u3001\uff12\u30da\u30fc\u30b8\u76ee\u306b\u3044\u308b\u306a\u3089\u300c\uff16\u300d\u3068\u306a\u308a\u307e\u3059\u3002\u30da\u30fc\u30b8from\u301cto\u306e\u300cto\u300d\u3002<\/p>\n<pre>echo $users-&gt;<strong>lastItem()<\/strong>;<\/pre>\n<h3>lastPage()\u3067\u6700\u5f8c\u306e\u30da\u30fc\u30b8\u756a\u53f7\u3092\u53d6\u5f97\u3059\u308b<\/h3>\n<p>\u3064\u307e\u308a\u3001\u3053\u306e\u6570\u5b57\u306f\u3069\u306e\u30da\u30fc\u30b8\u3067\u3082\u540c\u3058\u3082\u306e\u306b\u306a\u308a\u307e\u3059<\/p>\n<pre>echo $users-&gt;<strong>lastPage()<\/strong>;<\/pre>\n<h3>nextPageUrl()\u3067\u6b21\u30da\u30fc\u30b8\u306eURL\u3092\u53d6\u5f97\u3059\u308b<\/h3>\n<p>\u6b21\u30da\u30fc\u30b8\u306eURL\u3067\u3059\u3002\u3082\u3057\u6b21\u306e\u30da\u30fc\u30b8\u304c\u306a\u3044\u5834\u5408\u306f<strong>null<\/strong>\u306b\u306a\u308a\u307e\u3059\u3002<\/p>\n<pre>echo $users-&gt;<strong>nextPageUrl()<\/strong>;<\/pre>\n<h3>perPage()\u30da\u30fc\u30b8\u3054\u3068\u306b\u4f55\u4ef6\u306a\u306e\u304b\u3092\u53d6\u5f97\u3059\u308b<\/h3>\n<p>\u3064\u307e\u308a\u3001paginate()\u3067\u6307\u5b9a\u3057\u305f\u6570\u5b57\u3067\u3059\u3002<\/p>\n<pre>echo $users-&gt;<strong>perPage()<\/strong>;<\/pre>\n<h3>previousPageUrl()\u3067\u524d\u30da\u30fc\u30b8\u306eURL\u3092\u53d6\u5f97\u3059\u308b<\/h3>\n<p>\u524d\u30da\u30fc\u30b8\u306eURL\u3067\u3059\u3002\u3082\u3057\u6b21\u306e\u30da\u30fc\u30b8\u304c\u306a\u3044\u5834\u5408\u306f<strong>null<\/strong>\u306b\u306a\u308a\u307e\u3059\u3002<\/p>\n<pre>echo $users-&gt;<strong>previousPageUrl()<\/strong>;<\/pre>\n<h3>total()\u3067\u5168\u30c7\u30fc\u30bf\u4ef6\u6570\u3092\u53d6\u5f97\u3059\u308b<\/h3>\n<p>\u5168\u30c7\u30fc\u30bf\u4ef6\u6570\u3092\u53d6\u5f97\u3067\u304d\u307e\u3059\u3002\u8868\u793a\u30da\u30fc\u30b8\u306e\u4ef6\u6570\u3092\u53d6\u5f97\u3057\u305f\u3044\u5834\u5408\u306fcount()\u3092\u4f7f\u3044\u307e\u3057\u3087\u3046\u3002<\/p>\n<pre>echo $users-&gt;<strong>total();<\/strong><\/pre>\n<h3>url()\u3067\u7279\u5b9a\u30da\u30fc\u30b8\u756a\u53f7\u306eURL\u3092\u53d6\u5f97\u3059\u308b<\/h3>\n<p>\u4f8b\u3048\u3070\u3001\uff13\u30da\u30fc\u30b8\u76ee\u306eURL\u3092\u53d6\u5f97\u3059\u308b\u5834\u5408\u306f\u4ee5\u4e0b\u306e\u3088\u3046\u306b\u306a\u308a\u307e\u3059\u3002<\/p>\n<pre>echo $users-&gt;<strong>url(3)<\/strong>;<\/pre>\n<h2>links()\u3067\u30da\u30fc\u30b8\u30ea\u30f3\u30af\u3092\u4f5c\u6210\u3059\u308b<\/h2>\n<p>paginate()\u3092\u4f7f\u3063\u3066\u53d6\u5f97\u3057\u305f\u30c7\u30fc\u30bf\u306f\u30c6\u30f3\u30d7\u30ec\u30fc\u30c8\u5185\u3067\u4ee5\u4e0b\u306e\u3088\u3046\u306blinks()\u3092\u4f7f\u3046\u3068\u3001\u81ea\u52d5\u7684\u306bul\u30bf\u30b0\u3068li\u30bf\u30b0\u306e\u30da\u30fc\u30b8\u30ea\u30f3\u30af\u3092\u8868\u793a\u3057\u3066\u304f\u308c\u307e\u3059\u3002<\/p>\n<p>\u3061\u306a\u307f\u306bCSS\u30d5\u30ec\u30fc\u30e0\u30ef\u30fc\u30af\u3067\u6709\u540d\u306abootstrap\u3092\u8aad\u307f\u8fbc\u3080\u3068\u81ea\u52d5\u7684\u306b\u4ee5\u4e0b\u306e\u3088\u3046\u306a\u88c5\u98fe\u306b\u306a\u308a\u307e\u3059\u3002<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-694\" style=\"border: 3px solid #000;\" src=\"https:\/\/blog.capilano-fw.com\/wp-content\/uploads\/2018\/07\/l56-test-db-page-2.png\" alt=\"\" width=\"170\" height=\"60\" \/><\/p>\n<p>\u5b9f\u969b\u306e\u30b3\u30fc\u30c9\u306f\u4ee5\u4e0b\u306b\u306a\u308a\u307e\u3059\u3002<\/p>\n<p>\uff08\u30b3\u30f3\u30c8\u30ed\u30fc\u30e9\u30fc\u5074\uff09<\/p>\n<pre>class HomeController extends Controller\r\n{\r\n    public function index() {\r\n\r\n        $per_page = 3;\r\n        $users = \\App\\User::paginate($per_page);\r\n\r\n<strong>        return view('index')-&gt;with([<\/strong>\r\n<strong>            'users' =&gt; $users<\/strong>\r\n<strong>        ]);<\/strong>\r\n\r\n    }<\/pre>\n<p>\uff08Blade\u5074\uff09<\/p>\n<pre>&lt;html&gt;\r\n&lt;head&gt;\r\n    <strong>&lt;link href=\"https:\/\/stackpath.bootstrapcdn.com\/bootstrap\/4.1.1\/css\/bootstrap.min.css\" rel=\"stylesheet\"&gt;<\/strong>\r\n&lt;\/head&gt;\r\n&lt;body&gt;\r\n&lt;div&gt;\r\n    <strong>{{ $users-&gt;links() }}<\/strong>\r\n&lt;\/div&gt;\r\n&lt;\/body&gt;\r\n&lt;\/html&gt;<\/pre>\n<h3>withPath()\u3067\u30da\u30fc\u30b8\u30ea\u30f3\u30af\u306eURL\u3092\u5909\u66f4\u3059\u308b<\/h3>\n<p>\u30ea\u30f3\u30af\u306eURL\u3092\u5909\u66f4\u3057\u305f\u3044\u5834\u5408\u306fwithPath()\u3092\u4f7f\u3063\u3066\u304f\u3060\u3055\u3044\u3002<\/p>\n<pre>$users-&gt;withPath(<strong>'users\/detail'<\/strong>);<\/pre>\n<p>\u3053\u306e\u5834\u5408\u3001\u300c<strong>https:\/\/example.com\/users\/detail?page=\uff08\u30da\u30fc\u30b8\u756a\u53f7\uff09<\/strong>\u300d\u304cURL\u306b\u306a\u308a\u307e\u3059\u3002<\/p>\n<h3>appends()\u3067\u72ec\u81ea\u306e\u30d1\u30e9\u30e1\u30fc\u30bf\u3092URL\u306b\u8ffd\u52a0\u3059\u308b<\/h3>\n<p>\u30da\u30fc\u30b8\u30ea\u30f3\u30af\u306eURL\u306b\u72ec\u81ea\u306e\u30d1\u30e9\u30e1\u30fc\u30bf\u3092\u8ffd\u52a0\u3057\u305f\u3044\u5834\u5408\u306f\u4ee5\u4e0b\u306e\u3088\u3046\u306bappends()\u3092\u4f7f\u3044\u307e\u3059\u3002<\/p>\n<pre><strong>$users-&gt;appends([<\/strong>\r\n<strong>    'key_1' =&gt; 'value_1',<\/strong>\r\n<strong>    'key_2' =&gt; 'value_2',<\/strong>\r\n<strong>    'key_3' =&gt; 'value_3'<\/strong>\r\n<strong>]);<\/strong><\/pre>\n<p>\u3053\u308c\u3067\u3001\u30da\u30fc\u30b8\u306eURL\u306f\u300c<strong>https:\/\/example.com\/*****?key_1=value_1&amp;key_2=value_2&amp;key_3=value_3&amp;page=\uff08\u30da\u30fc\u30b8\u756a\u53f7\uff09<\/strong>\u300d\u3068\u306a\u308a\u307e\u3059\u3002<\/p>\n<h3>fragment()\u3067\u300c\uff03\u300d\u4ed8\u304d\u306eURL\u306b\u3059\u308b<\/h3>\n<pre>$users-&gt;<strong>fragment('bottom')<\/strong>;<\/pre>\n<p>\u3053\u306e\u5834\u5408\u306eURL\u306f\u3001\u300chttp:\/\/example.com\/***<strong>#bottom<\/strong>\u300d\u3068\u306a\u308a\u307e\u3059\u3002\u81ea\u52d5\u7684\u306b\u30da\u30fc\u30b8\u5185\u79fb\u52d5\u3057\u305f\u3044\u5834\u5408\u3084\u3001JavaScript\u306a\u3069\u3067state\u3092\u7ba1\u7406\u3057\u3066\u3044\u308b\u5834\u5408\u306b\u91cd\u5b9d\u3057\u307e\u3059\u3002<\/p>\n<h2>simplePaginate()\u3067\u7c21\u6613\u7684\u306b\u30da\u30fc\u30b8\u3054\u3068\u306e\u30c7\u30fc\u30bf\u3092\u53d6\u5f97\u3059\u308b<\/h2>\n<p>simplePaginate()\u306f\u3001\u307b\u307cpaginate()\u3068\u540c\u3058\u3067\u3059\u304c\u9055\u3046\u306e\u306f\u30da\u30fc\u30b8\u8868\u793a\u304c\u300c\u524d\u300d\u300c\u6b21\u300d\u3060\u3051\u306b\u306a\u308b\u3053\u3068\u3067\u3059\u3002<\/p>\n<p>\u5b9f\u969b\u306e\u8868\u793a\u306f\u3053\u3046\u306a\u308a\u307e\u3059\u3002<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-695\" style=\"border: 3px solid #000;\" src=\"https:\/\/blog.capilano-fw.com\/wp-content\/uploads\/2018\/07\/simplePaginate.png\" alt=\"\" width=\"145\" height=\"60\" \/><\/p>\n<p>\u305f\u3060\u3057\u3001\u4e0a\u306e\u3088\u3046\u306b\u65e5\u672c\u8a9e\u3092\u8868\u793a\u3059\u308b\u305f\u3081\u306b\u306f\u300c<strong>\/resouces\/lang\/ja\/pagination.php<\/strong>\u300d\u306b\u30d5\u30a1\u30a4\u30eb\u3092\u4f5c\u308a\u3001\u4ee5\u4e0b\u306e\u3088\u3046\u306a\u30c7\u30fc\u30bf\u3092\u8ffd\u52a0\u3059\u308b\u5fc5\u8981\u304c\u3042\u308a\u307e\u3059\u3002<\/p>\n<pre><strong>&lt;?php<\/strong>\r\n\r\n<strong>return [<\/strong>\r\n<strong>    'previous' =&gt; '&amp;laquo; \u524d',<\/strong>\r\n<strong>    'next' =&gt; '\u6b21 &amp;raquo;',<\/strong>\r\n<strong>];<\/strong><\/pre>\n<p>\u307e\u305f\u3001simplePaginate()\u306e\u7279\u5fb4\u304b\u3089\u3001\u4ee5\u4e0b\u306e\u30e1\u30bd\u30c3\u30c9\u306f\u4f7f\u3048\u306a\u304f\u306a\u3063\u3066\u3044\u307e\u3059\u3002<\/p>\n<ul>\n<li>lastPage()<\/li>\n<li>total()<\/li>\n<\/ul>\n<h1>\u30bd\u30d5\u30c8\u30c7\u30ea\u30fc\u30c8\uff08\u8ad6\u7406\u524a\u9664\uff09\u3055\u308c\u305f\u30c7\u30fc\u30bf\u3092\u53d6\u5f97\u3059\u308b<\/h1>\n<h2>withTrashed()\u3067\u30bd\u30d5\u30c8\u30c7\u30ea\u30fc\u30c8\u3055\u308c\u305f\u30c7\u30fc\u30bf\u3082\u542b\u3081\u3066\u30c7\u30fc\u30bf\u53d6\u5f97\u3059\u308b<\/h2>\n<p>withTrashed()\u3092\u4f7f\u3048\u3070\u3001\u3059\u3067\u306b\u30bd\u30d5\u30c8\u30c7\u30ea\u30fc\u30c8\u3055\u308c\u305f\u30c7\u30fc\u30bf\u3082\u542b\u3081\u3066\u53d6\u5f97\u3059\u308b\u3053\u3068\u304c\u3067\u304d\u307e\u3059\u3002\u7ba1\u7406\u8005\u5411\u3051\u30b3\u30f3\u30c6\u30f3\u30c4\u3092\u958b\u767a\u3059\u308b\u306e\u306b\u91cd\u5b9d\u3059\u308b\u304b\u3082\u3057\u308c\u307e\u305b\u3093\u3002<\/p>\n<pre>$items = \\App\\Item::<strong>withTrashed()<\/strong>-&gt;get();<\/pre>\n<h2>onlyTrashed()\u3067\u30bd\u30d5\u30c8\u30c7\u30ea\u30fc\u30c8\u3055\u308c\u305f\u30c7\u30fc\u30bf\u3060\u3051\u3092\u53d6\u5f97\u3059\u308b<\/h2>\n<p>withTrashed()\u3068\u306f\u9055\u3063\u3066\u3001\u30bd\u30d5\u30c8\u30c7\u30ea\u30fc\u30c8\u3055\u308c\u3066\u30c7\u30fc\u30bf\u300c\u3060\u3051\u300d\u3092\u53d6\u5f97\u3059\u308b\u65b9\u6cd5\u3067\u3059\u3002\u300c\u30b4\u30df\u7bb1\u300d\u30b3\u30f3\u30c6\u30f3\u30c4\u306a\u3069\u3092\u306b\u4f7f\u3048\u307e\u3059\u3002<\/p>\n<pre>$items = \\App\\Item::<strong>onlyTrashed()<\/strong>-&gt;get();<\/pre>\n<h1>\u30c7\u30fc\u30bf\u304c\u898b\u3064\u304b\u3089\u306a\u3044\u5834\u5408\u306f\u8003\u616e\u3057\u305f\u30c7\u30fc\u30bf\u53d6\u5f97<\/h1>\n<p>\u3082\u3057\u3042\u308b\u30c7\u30fc\u30bf\u304c\u898b\u3064\u304b\u3089\u306a\u3044\u5834\u5408\u306f\u30a8\u30e9\u30fc\u3092\u767a\u751f\u3055\u305b\u305f\u3044\u5834\u5408\u306e\u3084\u308a\u65b9\u3067\u3059\u3002\u3072\u3068\u3064\u305a\u3064\u898b\u3066\u3044\u304d\u307e\u3057\u3087\u3046\u3002<\/p>\n<h2>firstOrFail()\u3067\u30c7\u30fc\u30bf\u304c\u306a\u3044\u5834\u5408\u306f\u30a8\u30e9\u30fc\uff08\u4f8b\u5916\uff09\u3092\u767a\u751f\u3055\u305b\u308b<\/h2>\n<p>\u4f8b\u3048\u3070\u3001\u73fe\u5728ID\u304c\u300c\uff15\u300d\u306e\u30e6\u30fc\u30b6\u30fc\u306f\u5b58\u5728\u3057\u3066\u306a\u3044\u3068\u3057\u307e\u3059\u3002<\/p>\n<pre>\\App\\User::where('id', 5)-&gt;<strong>firstOrFail()<\/strong>;<\/pre>\n<p>\u3059\u308b\u3068\u3001\u4e0a\u8a18\u3092\u5b9f\u884c\u3057\u305f\u5834\u5408\u3001\u4ee5\u4e0b\u306e\u3088\u3046\u306b404\u30a8\u30e9\u30fc\u3068\u306a\u308a\u307e\u3059\u3002<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-725\" style=\"border: 3px solid #000;\" src=\"https:\/\/blog.capilano-fw.com\/wp-content\/uploads\/2018\/07\/laravel_404.png\" alt=\"\" width=\"935\" height=\"96\" srcset=\"https:\/\/blog.capilano-fw.com\/wp-content\/uploads\/2018\/07\/laravel_404.png 935w, https:\/\/blog.capilano-fw.com\/wp-content\/uploads\/2018\/07\/laravel_404-300x31.png 300w, https:\/\/blog.capilano-fw.com\/wp-content\/uploads\/2018\/07\/laravel_404-768x79.png 768w\" sizes=\"auto, (max-width: 767px) 89vw, (max-width: 1000px) 54vw, (max-width: 1071px) 543px, 580px\" \/><\/p>\n<p>\u3082\u3057\u3001404\u30ec\u30b9\u30dd\u30f3\u30b9\u3092\u767a\u751f\u3055\u305b\u305f\u304f\u306a\u3044\u5834\u5408\u306f<strong>try\u301ccatch<\/strong>\u3067\u4f8b\u5916\u3068\u3057\u3066\u51e6\u7406\u304c\u3067\u304d\u307e\u3059\u3002<\/p>\n<pre><strong>try {<\/strong>\r\n\r\n    $user = \\App\\User::where('id', 5)-&gt;firstOrFail();\r\n\r\n<strong>} catch (\\Exception $e) {<\/strong>\r\n\r\n    echo $e-&gt;getMessage();\r\n\r\n<strong>}<\/strong><\/pre>\n<h2>findOrFail()\u3067\u30c7\u30fc\u30bf\u304c\u898b\u3064\u304b\u3089\u306a\u3044\u5834\u5408\u306f\u30a8\u30e9\u30fc\uff08\u4f8b\u5916\uff09\u3092\u767a\u751f\u3055\u305b\u308b<\/h2>\n<p>\u3053\u308c\u3082\u524d\u9805\u76ee\u306efirstOrFail()\u3068\u307b\u307c\u540c\u3058\u3067\u3059\u304c\u3001\u5f15\u6570\u306f\u30c6\u30fc\u30d6\u30eb\u306eID\u3092\u6307\u5b9a\u3057\u307e\u3059\u3002<\/p>\n<pre>$id = 5;\r\n\\App\\User::<strong>findOrFail($id)<\/strong>;<\/pre>\n<h1>\u304a\u308f\u308a\u306b<\/h1>\n<p>\u3068\u3044\u3046\u3053\u3068\u3067\u4eca\u56de\u306f\u30c7\u30fc\u30bf\u30d9\u30fc\u30b9\u304b\u3089\u30c7\u30fc\u30bf\u3092\u53d6\u5f97\u3059\u308b\u65b9\u6cd5\u3092\u307e\u3068\u3081\u3066\u307f\u307e\u3057\u305f\u3002\u30fb\u30fb\u30fb\u304c\u3001\u7d50\u679c\u3068\u3057\u306620,000\u6587\u5b57\u3092\u8efd\u304f\u8d8a\u3048\u308b\u60c5\u5831\u91cf\u306b\u306a\u3063\u3066\u3057\u307e\u3044\u307e\u3057\u305f\u3002\u305d\u308c\u3060\u3051Laravel\u306e\u5b88\u5099\u7bc4\u56f2\u304c\u5e83\u3044\u3068\u3044\u3046\u3053\u3068\u3067\u3059\u306d\u3002<\/p>\n<p>\u3053\u308c\u304b\u3089\u3082\u75d2\u3044\u3068\u3053\u308d\u306b\u624b\u304c\u5c4a\u304f\u30d5\u30ec\u30fc\u30e0\u30ef\u30fc\u30af\u3067\u3042\u3063\u3066\u307b\u3057\u3044\u3067\u3059\u3002<\/p>\n<p>\uff08\u306a\u304a\u3001\u5168\u5b9f\u4f8b\u3068\u30bf\u30a4\u30c8\u30eb\u306b\u66f8\u3044\u3066\u307e\u3059\u304c\u3001\u3082\u3057\u629c\u3051\u3066\u3044\u308b\u3082\u306e\u304c\u3042\u3063\u305f\u3089\u305c\u3072\u3054\u9023\u7d61\u304f\u3060\u3055\u3044\u3002\u304a\u9858\u3044\u3044\u305f\u3057\u307e\u3059\u3002m_ _m\uff09<\/p>\n<p>\u3067\u306f\u3067\u306f\uff01<\/p>\n<p style=\"text-align: center;\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-444\" src=\"https:\/\/blog.capilano-fw.com\/wp-content\/uploads\/2018\/04\/16.png\" alt=\"\" width=\"370\" height=\"310\" srcset=\"https:\/\/blog.capilano-fw.com\/wp-content\/uploads\/2018\/04\/16.png 370w, https:\/\/blog.capilano-fw.com\/wp-content\/uploads\/2018\/04\/16-300x251.png 300w\" sizes=\"auto, (max-width: 370px) 100vw, 370px\" \/><\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u3055\u3066\u3055\u3066\u3001Laravel\u306e\u63d0\u4f9b\u3059\u308b\u6a5f\u80fd\u3067\u7279\u306b\u4fbf\u5229\u306a\u306e\u304c\u300cQuery Builder\u300d\u3001\u3064\u307e\u308a\u30c7\u30fc\u30bf\u30d9\u30fc\u30b9\u306e\u64cd\u4f5c\u3068\u3044\u3063\u3066\u3044\u3044\u3068\u601d\u3044\u307e\u3059\u3002 Laravel\u306b\u9650\u3089\u305a\u3067 &hellip; <\/p>\n<p class=\"link-more\"><a href=\"https:\/\/blog.capilano-fw.com\/?p=665\" class=\"more-link\"><span class=\"screen-reader-text\">&#8220;Laravel\u30fb\u30c7\u30fc\u30bf\u30d9\u30fc\u30b9\u304b\u3089\u30c7\u30fc\u30bf\u53d6\u5f97\u3059\u308b\u5168\u5b9f\u4f8b&#8221; \u306e<\/span>\u7d9a\u304d\u3092\u8aad\u3080<\/a><\/p>\n","protected":false},"author":1,"featured_media":672,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3],"tags":[60],"class_list":["post-665","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-laravel","tag-60"],"_links":{"self":[{"href":"https:\/\/blog.capilano-fw.com\/index.php?rest_route=\/wp\/v2\/posts\/665","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=665"}],"version-history":[{"count":30,"href":"https:\/\/blog.capilano-fw.com\/index.php?rest_route=\/wp\/v2\/posts\/665\/revisions"}],"predecessor-version":[{"id":726,"href":"https:\/\/blog.capilano-fw.com\/index.php?rest_route=\/wp\/v2\/posts\/665\/revisions\/726"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/blog.capilano-fw.com\/index.php?rest_route=\/wp\/v2\/media\/672"}],"wp:attachment":[{"href":"https:\/\/blog.capilano-fw.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=665"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.capilano-fw.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=665"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.capilano-fw.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=665"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}