{"id":4615,"date":"2022-07-07T20:37:15","date_gmt":"2022-07-07T11:37:15","guid":{"rendered":"https:\/\/www.wetch.co.jp\/?p=4615"},"modified":"2022-07-07T20:37:17","modified_gmt":"2022-07-07T11:37:17","slug":"laravel%e3%81%a7%e3%81%aesql%e5%ae%9f%e8%a1%8c%e5%86%85%e5%ae%b9%e3%81%ae%e5%87%ba%e5%8a%9b%e3%81%ab%e3%81%a4%e3%81%84%e3%81%a6","status":"publish","type":"post","link":"https:\/\/www.wetch.co.jp\/note\/laravel%e3%81%a7%e3%81%aesql%e5%ae%9f%e8%a1%8c%e5%86%85%e5%ae%b9%e3%81%ae%e5%87%ba%e5%8a%9b%e3%81%ab%e3%81%a4%e3%81%84%e3%81%a6\/","title":{"rendered":"laravel\u3067\u306eSQL\u5b9f\u884c\u5185\u5bb9\u306e\u51fa\u529b\u306b\u3064\u3044\u3066"},"content":{"rendered":"<p>laravel\u3067Eloquent\u3092\u4f7f\u7528\u3057\u305f\u30c7\u30fc\u30bf\u53d6\u5f97\u306eSQL\u5185\u5bb9\u3092\u78ba\u8a8d\u3059\u308b\u969b\u306b\u3001\u81ea\u5206\u304c\u3044\u3064\u3082\u5fd8\u308c\u3066\u3057\u307e\u3046\u306e\u3067\u5099\u5fd8\u306e\u305f\u3081\u306b\u66f8\u3044\u3066\u304a\u308a\u307e\u3059\u3002<\/p>\n<p>\u5b9f\u969b\u306b\u767a\u884c\u3055\u308c\u308bSQL\u304c\u3069\u3046\u306a\u3063\u3066\u3044\u308b\u304b\u3092\u78ba\u8a8d\u3059\u308b\u65b9\u6cd5\u3068\u3057\u3066<\/p>\n<p>\uff11\uff09<a href=\"https:\/\/laravel.com\/api\/master\/Illuminate\/Database\/Query\/Builder.html#method_toSql\" target=\"_blank\" rel=\"nofollow noopener\"><code>toSql()<\/code><\/a> \u30e1\u30bd\u30c3\u30c9\u3092\u547c\u3073\u51fa\u3057\u3066\u3001SQL\u6587\u3092\u53d6\u5f97\u3059\u308b<\/p>\n<p>$query = Model\u540d::where( \u30fb\u30fb\u30fb )-&gt;where( \u30fb\u30fb\u30fb )-&gt;where( \u30fb\u30fb\u30fb );<\/p>\n<p>$query-&gt;orWhere(function($query) use( \u5909\u6570A, \u5909\u6570B, \u5909\u6570C ){<\/p>\n<div>\u3000\u3000\u3000\u3000\u3000\u3000\u3000\u3000\u3000\u3000\u3000\u3000\u30fb<\/div>\n<div>\n<div>\u3000\u3000\u3000\u3000\u3000\u3000\u3000\u3000\u3000\u3000\u3000\u3000\u30fb<\/div>\n<div>\n<div>\u3000\u3000\u3000\u3000\u3000\u3000\u3000\u3000\u3000\u3000\u3000\u3000\u30fb<\/div>\n<div>\u00a0<\/div>\n<\/div>\n<\/div>\n<div>\n<div>});<\/div>\n<div>\u00a0<\/div>\n<\/div>\n<p><strong><span style=\"color: #000000;\">dd( $query-&gt;toSql(), $query-&gt;getBindings() );<\/span><\/strong><\/p>\n<p>\u00a0<\/p>\n<p>\uff12\uff09SQL\u5b9f\u884c\u524d\u306b <a href=\"https:\/\/laravel.com\/api\/master\/Illuminate\/Database\/Connection.html#method_enableQueryLog\" target=\"_blank\" rel=\"nofollow noopener\"><code>DB::enableQueryLog()<\/code><\/a> \u3067\u30af\u30a8\u30ea\u30ed\u30b0\u3092\u6709\u52b9\u5316\u3057\u3066\u3001SQL\u5b9f\u884c\u5f8c\u306b <a href=\"https:\/\/laravel.com\/api\/master\/Illuminate\/Database\/Connection.html#method_getQueryLog\" target=\"_blank\" rel=\"nofollow noopener\"><code>DB::getQueryLog()<\/code><\/a> \u30e1\u30bd\u30c3\u30c9\u3092\u5229\u7528\u3059\u308b\u3002<\/p>\n<p><strong>\\DB::enableQueryLog();<\/strong><\/p>\n<p>\u00a0<\/p>\n<p>\u5909\u6570A = Model\u540d::where( \u30fb\u30fb\u30fb )-&gt;get();<\/p>\n<div>\u3000\u3000\u3000\u3000\u3000\u3000\u3000\u3000\u3000\u30fb<\/div>\n<div>\n<div>\u3000\u3000\u3000\u3000\u3000\u3000\u3000\u3000\u3000\u30fb<\/div>\n<div>\n<div>\u3000\u3000\u3000\u3000\u3000\u3000\u3000\u3000\u3000\u30fb<\/div>\n<\/div>\n<\/div>\n<p>\u5909\u6570B = Model\u540d::where( \u30fb\u30fb\u30fb )-&gt;get();<\/p>\n<div>\u3000\u3000\u3000\u3000\u3000\u3000\u3000\u3000\u3000\u30fb<\/div>\n<div>\n<div>\u3000\u3000\u3000\u3000\u3000\u3000\u3000\u3000\u3000\u30fb<\/div>\n<div>\n<div>\u3000\u3000\u3000\u3000\u3000\u3000\u3000\u3000\u3000\u30fb<\/div>\n<\/div>\n<\/div>\n<p>\u5909\u6570C = Model\u540d::where( \u30fb\u30fb\u30fb )-&gt;get();<\/p>\n<div>\u3000\u3000\u3000\u3000\u3000\u3000\u3000\u3000\u3000\u30fb<\/div>\n<div>\n<div>\u3000\u3000\u3000\u3000\u3000\u3000\u3000\u3000\u3000\u30fb<\/div>\n<div>\n<div>\u3000\u3000\u3000\u3000\u3000\u3000\u3000\u3000\u3000\u30fb<\/div>\n<\/div>\n<\/div>\n<div>\u00a0<\/div>\n<div>\n<div>\n<div>\u00a0<\/div>\n<\/div>\n<\/div>\n<div>\n<div>\n<div>\n<div><strong>dd( \\DB:: getQueryLog() );<\/strong><\/div>\n<\/div>\n<\/div>\n<\/div>\n<p>\u00a0<\/p>\n<p>\uff11\uff09\u306e\u5834\u5408\u306f\u5358\u4e00\u306eSQL\u3092\u30c7\u30d0\u30c3\u30b0\u51fa\u529b\u3001\uff12\uff09\u306e\u5834\u5408\u306f\u56f2\u3093\u3060\u4e2d\u3067\u767a\u884c\u3057\u305f\u30af\u30a8\u30ea\u6570\u306e\u30c7\u30d0\u30c3\u30b0\u51fa\u529b\u304c\u884c\u308f\u308c\u307e\u3059\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>laravel\u3067Eloquent\u3092\u4f7f\u7528\u3057\u305f\u30c7\u30fc\u30bf\u53d6\u5f97\u306eSQL\u5185\u5bb9\u3092\u78ba\u8a8d\u3059\u308b\u969b\u306b\u3001\u81ea\u5206\u304c\u3044\u3064\u3082\u5fd8\u308c\u3066\u3057\u307e\u3046\u306e\u3067\u5099\u5fd8\u306e\u305f\u3081\u306b\u66f8\u3044\u3066\u304a\u308a\u307e\u3059\u3002 \u5b9f\u969b\u306b\u767a\u884c\u3055\u308c\u308bSQL\u304c\u3069\u3046\u306a\u3063\u3066\u3044\u308b\u304b\u3092\u78ba\u8a8d\u3059\u308b\u65b9\u6cd5\u3068\u3057\u3066 \uff11\uff09toSql()  [&hellip;]<\/p>\n","protected":false},"author":3,"featured_media":4631,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":"","_wp_rev_ctl_limit":""},"categories":[210],"tags":[],"class_list":["post-4615","post","type-post","status-publish","format-standard","has-post-thumbnail","category-210"],"_links":{"self":[{"href":"https:\/\/www.wetch.co.jp\/note\/wp-json\/wp\/v2\/posts\/4615","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.wetch.co.jp\/note\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.wetch.co.jp\/note\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.wetch.co.jp\/note\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/www.wetch.co.jp\/note\/wp-json\/wp\/v2\/comments?post=4615"}],"version-history":[{"count":0,"href":"https:\/\/www.wetch.co.jp\/note\/wp-json\/wp\/v2\/posts\/4615\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.wetch.co.jp\/note\/wp-json\/wp\/v2\/media\/4631"}],"wp:attachment":[{"href":"https:\/\/www.wetch.co.jp\/note\/wp-json\/wp\/v2\/media?parent=4615"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wetch.co.jp\/note\/wp-json\/wp\/v2\/categories?post=4615"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wetch.co.jp\/note\/wp-json\/wp\/v2\/tags?post=4615"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}