{"id":9225,"date":"2020-11-06T10:00:02","date_gmt":"2020-11-06T01:00:02","guid":{"rendered":"https:\/\/www.gigas-jp.com\/appnews\/?p=9225"},"modified":"2020-11-05T16:46:37","modified_gmt":"2020-11-05T07:46:37","slug":"laravel-eloquent%e3%81%a8%e3%83%9a%e3%83%bc%e3%82%b8%e3%83%8d%e3%83%bc%e3%82%b7%e3%83%a7%e3%83%b32","status":"publish","type":"post","link":"https:\/\/www.gigas-jp.com\/appnews\/archives\/9225","title":{"rendered":"[Laravel] Eloquent\u3068\u30da\u30fc\u30b8\u30cd\u30fc\u30b7\u30e7\u30f3(2)"},"content":{"rendered":"\n<p>\u4eca\u56de\u306fLaravel\u6a19\u6e96\u306eO\/R\u30de\u30c3\u30d1\u30fc\u300cEloquent\u300d\u3092\u4f7f\u7528\u3057\u305f\u30c7\u30fc\u30bf\u53d6\u5f97\u65b9\u6cd5\u3092\u7d39\u4ecb\u3057\u305f\u3044\u3068\u601d\u3044\u307e\u3059\u3002\u672c\u8a18\u4e8b\u306f\u524d\u56de\u306e\u300c<a rel=\"noreferrer noopener\" aria-label=\"[Laravel] Eloquent\u3068\u30da\u30fc\u30b8\u30cd\u30fc\u30b7\u30e7\u30f3(1) (\u65b0\u3057\u3044\u30bf\u30d6\u3067\u958b\u304f)\" href=\"\/appnews\/archives\/9212\" target=\"_blank\">[Laravel] Eloquent\u3068\u30da\u30fc\u30b8\u30cd\u30fc\u30b7\u30e7\u30f3(1)<\/a>\u300d\u306e\u7d9a\u304d\u306b\u306a\u308a\u307e\u3059\u3002<\/p>\n\n\n\n<!--more-->\n\n\n\n<h4>\u8907\u6570\u306ewhere\u6761\u4ef6\u3092\u6307\u5b9a\u3059\u308b\u5834\u5408<\/h4>\n\n\n\n<p>where\u30e1\u30bd\u30c3\u30c9\u3092\u9023\u7d9a\u3057\u3066\u547c\u3073\u51fa\u3059\u3053\u3068\u3067\u8907\u6570\u306ewhere\u6761\u4ef6\u3092and\u691c\u7d22\u3067\u8a2d\u5b9a\u3059\u308b\u3053\u3068\u304c\u53ef\u80fd\u3067\u3059\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Student::where('name','\u5c71\u7530')->where('age','&lt;','20')->get();<\/code><\/pre>\n\n\n\n<p>\u4e0a\u8a18\u306e\u5834\u5408\u306f\u3001\u300c\u540d\u524d\u300d\u304c\u300c\u5c71\u7530\u300d\u3067\u304b\u3064\u5e74\u9f62\u304c20\u6b73\u4ee5\u4e0b\u306e\u30ec\u30b3\u30fc\u30c9\u3092\u62bd\u51fa\u3057\u307e\u3059\u3002<\/p>\n\n\n\n<p>and\u6761\u4ef6\u3067\u306f\u306a\u304for\u6761\u4ef6\u3092\u8a2d\u5b9a\u3057\u305f\u3044\u5834\u5408\u306forWhere\u30e1\u30bd\u30c3\u30c9\u3092\u4f7f\u7528\u3057\u307e\u3059\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Student::where('name','\u5c71\u7530')->orWhere('age','&lt;','20')->get();<\/code><\/pre>\n\n\n\n<p>\u4e0a\u8a18\u306e\u5834\u5408\u306f\u3001\u300c\u540d\u524d\u300d\u304c\u300c\u5c71\u7530\u300d\u307e\u305f\u306f\u5e74\u9f62\u304c20\u6b73\u4ee5\u4e0b\u306e\u30ec\u30b3\u30fc\u30c9\u3092\u62bd\u51fa\u3057\u307e\u3059\u3002<\/p>\n\n\n\n<h4>where\u6761\u4ef6\u90e8\u5206\u3092\u4efb\u610f\u306eSQL\u3067\u8a18\u8ff0\u3057\u305f\u3044\u5834\u5408<\/h4>\n\n\n\n<p>whereRaw\u30e1\u30bd\u30c3\u30c9\u3092\u4f7f\u7528\u3057\u3066\u4efb\u610f\u306eSQL\u3092\u6761\u4ef6\u306b\u8a2d\u5b9a\u3059\u308b\u3053\u3068\u304c\u3067\u304d\u307e\u3059\u3002<br> \u30d7\u30ec\u30fc\u30b9\u30db\u30eb\u30c0\u30fc\u306f\u300c?\u300d\u3067\u8a2d\u5b9a\u3057\u3066\u3001\u8a2d\u5b9a\u3059\u308b\u5024\u3092\u914d\u5217\u3067\u6e21\u3059\u3053\u3068\u304c\u3067\u304d\u307e\u3059\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>$param = ['\u5c71\u7530','20'];\n$res = Student::whereRaw('name = ? and age &lt; ?', $param)->get();<\/code><\/pre>\n\n\n\n<h4>\u30ec\u30b3\u30fc\u30c9\u306e\u4e26\u3073\u66ff\u3048<\/h4>\n\n\n\n<p>SQL\u306eorder by\u3067\u4e26\u3073\u66ff\u3048\u304c\u304a\u3053\u306a\u3048\u308b\u3088\u3046\u306b\u3001Eloquent\u3067\u306forderBy\u30e1\u30bd\u30c3\u30c9\u3067\u4e26\u3073\u66ff\u3048\u304c\u304a\u3053\u306a\u3046\u3053\u3068\u304c\u53ef\u80fd\u3067\u3059\u3002<br> \u5f15\u6570\u306b\u30bd\u30fc\u30c8\u3092\u304a\u3053\u306a\u3046\u30ab\u30e9\u30e0\u304a\u3088\u3073\u4e26\u3073\u9806(asc or desc)\u3092\u6307\u5b9a\u3057\u307e\u3059\u3002<br> \u8907\u6570\u306e\u30bd\u30fc\u30c8\u9806\u304c\u3042\u308b\u5834\u5408\u306f\u3001orderBy\u30e1\u30bd\u30c3\u30c9\u3092\u9023\u7d50\u3057\u3066\u4f7f\u7528\u3059\u308b\u3053\u3068\u3082\u53ef\u80fd\u3067\u3059\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Student::orderBy('age', 'desc')->get();<\/code><\/pre>\n\n\n\n<p>\u307e\u305f\u3001offset\u3084limit\u3068\u4f75\u7528\u3059\u308b\u3053\u3068\u306b\u3088\u308a\u3001\u62bd\u51fa\u3059\u308b\u30ec\u30b3\u30fc\u30c9\u306e\u7bc4\u56f2\u3092\u6307\u5b9a\u3059\u308b\u3053\u3068\u304c\u3067\u304d\u307e\u3059\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Student::orderBy('age', 'desc')->offset(10)->limit(5)->get();<\/code><\/pre>\n\n\n\n<p>\u4e0a\u8a18\u306e\u4f8b\u3067\u306f\u3001offset\u3084limit\u3092\u6307\u5b9a\u3059\u308b\u3053\u3068\u3067\u3001\u5148\u982d\u304b\u3089\u4e0a\u4f4d10\u4ef6\u306e\u30ec\u30b3\u30fc\u30c9\u3092\u30b9\u30ad\u30c3\u30d7\u3057\u306611\u4ef6\u76ee\u304b\u30895\u4ef6\u306e\u30ec\u30b3\u30fc\u30c9\u3092\u62bd\u51fa\u3057\u3066\u3044\u307e\u3059\u3002<\/p>\n\n\n\n<p>\u3053\u308c\u307e\u3067\u306b\u3042\u3052\u305f\u4f8b\u3067\u306fselect\u3059\u308b\u30ab\u30e9\u30e0\u3092\u7279\u306b\u660e\u793a\u3057\u307e\u305b\u3093\u3067\u3057\u305f\u306e\u3067\u3001SQL\u3067*\u3092\u4f7f\u7528\u3059\u308b\u3088\u3046\u306b\u3001\u5bfe\u8c61\u30c6\u30fc\u30d6\u30eb\u306b\u5b58\u5728\u3059\u308b\u3059\u3079\u3066\u306e\u30ab\u30e9\u30e0\u3092\u62bd\u51fa\u5bfe\u8c61\u3068\u3057\u3066\u304d\u307e\u3057\u305f\u304c\u3001select\u30e1\u30bd\u30c3\u30c9\u3067\u30ab\u30e9\u30e0\u3092\u6307\u5b9a\u3059\u308b\u3053\u3068\u3067\u53d6\u5f97\u3059\u308b\u30ab\u30e9\u30e0\u3092\u5236\u9650\u3059\u308b\u3053\u3068\u3082\u53ef\u80fd\u3067\u3059\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>$res = Student::select('name', 'age')->get();<\/code><\/pre>\n\n\n\n<h4>\u30ec\u30b3\u30fc\u30c9\u306e\u30b0\u30eb\u30fc\u30d7\u5316\u3068\u96c6\u8a08<\/h4>\n\n\n\n<p>SQL\u3067\u306fgroup by\u3067\u30b0\u30eb\u30fc\u30d7\u5316\u304c\u304a\u3053\u306a\u3048\u308b\u3088\u3046\u306b\u3001Eloquent\u3067\u306fgroupBy\u30e1\u30bd\u30c3\u30c9\u3067\u30b0\u30eb\u30fc\u30d7\u5316\u304c\u304a\u3053\u306a\u3048\u307e\u3059\u3002<br> \u4ee5\u4e0b\u306e\u4f8b\u3067\u306f\u300c\u90e8\u6d3b\u52d5\u306e\u5358\u4f4d\u3067\u5b66\u751f\u306e\u5e73\u5747\u5e74\u9f62\u300d\u3092\u6c42\u3081\u3066\u3044\u307e\u3059\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>$res = Student::groupBy('club')\n->selectRaw('club, avg(age) as avg_age')->get();\n<\/code><\/pre>\n\n\n\n<p>whereRaw\u30e1\u30bd\u30c3\u30c9\u3068\u540c\u69d8\u306b\u7d20\u306eSQL\u3092\u90e8\u5206\u7684\u306b\u4f75\u7528\u3057\u305f\u3044\u5834\u5408\u306fselectRaw\u304c\u4f7f\u7528\u3067\u304d\u307e\u3059\u3002<\/p>\n\n\n\n<p>\u6b21\u56de\u306f Eloquent \u3092\u4f7f\u7528\u3057\u305f\u30da\u30fc\u30b8\u30cd\u30fc\u30b7\u30e7\u30f3\u306e\u65b9\u6cd5\u306b\u3064\u3044\u3066\u7d39\u4ecb\u3057\u305f\u3044\u3068\u601d\u3044\u307e\u3059\u3002<\/p>\n\n\n\n<p>\u91d1\u66dc\u65e5\u62c5\u5f53\uff1anishida<\/p>\n<div class='wp_social_bookmarking_light'>\n            <div class=\"wsbl_google_plus_one\"><g:plusone size=\"medium\" annotation=\"none\" href=\"https:\/\/www.gigas-jp.com\/appnews\/archives\/9225\" ><\/g:plusone><\/div>\n            <div class=\"wsbl_hatena_button\"><a href=\"\/\/b.hatena.ne.jp\/entry\/https:\/\/www.gigas-jp.com\/appnews\/archives\/9225\" class=\"hatena-bookmark-button\" data-hatena-bookmark-title=\"[Laravel] Eloquent\u3068\u30da\u30fc\u30b8\u30cd\u30fc\u30b7\u30e7\u30f3(2)\" data-hatena-bookmark-layout=\"standard\" title=\"\u3053\u306e\u30a8\u30f3\u30c8\u30ea\u30fc\u3092\u306f\u3066\u306a\u30d6\u30c3\u30af\u30de\u30fc\u30af\u306b\u8ffd\u52a0\"> <img src=\"\/\/b.hatena.ne.jp\/images\/entry-button\/button-only@2x.png\" alt=\"\u3053\u306e\u30a8\u30f3\u30c8\u30ea\u30fc\u3092\u306f\u3066\u306a\u30d6\u30c3\u30af\u30de\u30fc\u30af\u306b\u8ffd\u52a0\" width=\"20\" height=\"20\" style=\"border: none;\" \/><\/a><script type=\"text\/javascript\" src=\"\/\/b.hatena.ne.jp\/js\/bookmark_button.js\" charset=\"utf-8\" async=\"async\"><\/script><\/div>\n            <div class=\"wsbl_twitter\"><a href=\"https:\/\/twitter.com\/share\" class=\"twitter-share-button\" data-url=\"https:\/\/www.gigas-jp.com\/appnews\/archives\/9225\" data-text=\"[Laravel] Eloquent\u3068\u30da\u30fc\u30b8\u30cd\u30fc\u30b7\u30e7\u30f3(2)\" data-via=\"GIGASJAPAN_APPS\" data-lang=\"ja\">Tweet<\/a><\/div>\n            <div class=\"wsbl_facebook_like\"><div id=\"fb-root\"><\/div><fb:like href=\"https:\/\/www.gigas-jp.com\/appnews\/archives\/9225\" layout=\"button_count\" action=\"like\" width=\"100\" share=\"false\" show_faces=\"false\" ><\/fb:like><\/div>\n            <div class=\"wsbl_facebook_send\"><div id=\"fb-root\"><\/div><fb:send href=\"https:\/\/www.gigas-jp.com\/appnews\/archives\/9225\" colorscheme=\"light\" ><\/fb:send><\/div>\n    <\/div>\n<br class='wp_social_bookmarking_light_clear' \/>\n","protected":false},"excerpt":{"rendered":"<p>\u4eca\u56de\u306fLaravel\u6a19\u6e96\u306eO\/R\u30de\u30c3\u30d1\u30fc\u300cEloquent\u300d\u3092\u4f7f\u7528\u3057\u305f\u30c7\u30fc\u30bf\u53d6\u5f97\u65b9\u6cd5\u3092\u7d39\u4ecb\u3057\u305f\u3044\u3068\u601d\u3044\u307e\u3059\u3002\u672c\u8a18\u4e8b\u306f\u524d\u56de\u306e\u300c[Laravel] Eloquent\u3068\u30da\u30fc\u30b8\u30cd\u30fc\u30b7\u30e7\u30f3(1)\u300d\u306e\u7d9a\u304d\u306b\u306a\u308a\u307e\u3059\u3002<\/p>\n","protected":false},"author":16,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[100],"tags":[],"acf":[],"_links":{"self":[{"href":"https:\/\/www.gigas-jp.com\/appnews\/wp-json\/wp\/v2\/posts\/9225"}],"collection":[{"href":"https:\/\/www.gigas-jp.com\/appnews\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.gigas-jp.com\/appnews\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.gigas-jp.com\/appnews\/wp-json\/wp\/v2\/users\/16"}],"replies":[{"embeddable":true,"href":"https:\/\/www.gigas-jp.com\/appnews\/wp-json\/wp\/v2\/comments?post=9225"}],"version-history":[{"count":5,"href":"https:\/\/www.gigas-jp.com\/appnews\/wp-json\/wp\/v2\/posts\/9225\/revisions"}],"predecessor-version":[{"id":9233,"href":"https:\/\/www.gigas-jp.com\/appnews\/wp-json\/wp\/v2\/posts\/9225\/revisions\/9233"}],"wp:attachment":[{"href":"https:\/\/www.gigas-jp.com\/appnews\/wp-json\/wp\/v2\/media?parent=9225"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.gigas-jp.com\/appnews\/wp-json\/wp\/v2\/categories?post=9225"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.gigas-jp.com\/appnews\/wp-json\/wp\/v2\/tags?post=9225"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}