{"id":1299,"date":"2019-09-26T10:23:40","date_gmt":"2019-09-26T01:23:40","guid":{"rendered":"https:\/\/qol-kk.com\/wp2\/?p=1299"},"modified":"2020-07-08T09:37:13","modified_gmt":"2020-07-08T00:37:13","slug":"post-1299","status":"publish","type":"post","link":"https:\/\/qol-kk.com\/wp2\/blog\/2019\/09\/26\/post-1299\/","title":{"rendered":"Laravel\uff1aEloquent\u30672\u3064\u306e\u30c6\u30fc\u30d6\u30eb\u306e\u4e0d\u4e00\u81f4\u3092\u62bd\u51fa\u3059\u308b"},"content":{"rendered":"<h2>Laravel\uff1aEloquent\u30672\u3064\u306e\u30c6\u30fc\u30d6\u30eb\u306e\u4e0d\u4e00\u81f4\u3092\u62bd\u51fa\u3059\u308b<\/h2>\n<p>\u79c1\u306f\u3082\u3068\u3082\u3068\u30d5\u30ec\u30fc\u30e0\u30ef\u30fc\u30af\u3092\u4f7f\u308f\u305aPHP\u3092\u66f8\u3044\u3066\u3044\u305f\u306e\u3067\u4f5c\u6210\u3057\u305f\u81ea\u4f5c\u30b7\u30b9\u30c6\u30e0\u3092\u4f5c\u308b\u307e\u3067DB\u306e\u62bd\u51fa\u306fSQL\u3092\u76f4\u63a5\u66f8\u3044\u3066\u3044\u307e\u3057\u305f\u3002\u305d\u306e\u70ba\u3001\u81ea\u4f5c\u30b7\u30b9\u30c6\u30e0\u4f5c\u6210\u521d\u671f\u306b\u66f8\u3044\u305f\u30b3\u30fc\u30c9\u306f(Eloquent\u3067\u306e\u6587\u304c\u5206\u304b\u3089\u305a)DB::\u3068\u66f8\u304d\u51fa\u3057\u305f\u3082\u306e\u304c\u3044\u304f\u3064\u304b\u3042\u308a\u307e\u3059\u3002\u3067\u3082\u3053\u308c\u3001Laravel\u306e\u4e2d\u3060\u3068\u51e6\u7406\u304c\u591a\u5c11\u9045\u3044\u3088\u3046\u3067\u73fe\u5728Eloquent\u306b\u66f8\u63db\u3092\u3057\u3066\u3044\u307e\u3059\u3002<\/p>\n<p>\u305d\u3093\u306a\u4e2d\u300c\u4e0d\u4e00\u81f4Query\u304c\u8d85\u697d\u3058\u3083\u3093\uff01\u300d\u3068\u306e\u611f\u60f3\u3068\u300c\uff1f\uff1f\uff1f\u300d\u3068\u306e\u611f\u60f3\u3068\u5171\u306b\u3042\u3063\u305f\u70ba\u3001\u5099\u5fd8\u9332\u3068\u3057\u3066\u3053\u3053\u306b\u8a18\u8f09\u3057\u307e\u3059\u3002<\/p>\n<h3>\u8d85\u7c21\u5358\u306a\u4e0d\u4e00\u81f4Query\u306eEloquent\u30b3\u30fc\u30c9\u3010doesntHave\u3011<\/h3>\n<p>\u30ce\u30fc\u30de\u30eb\u306aSQL\u3067\u306f\u3053\u3093\u306a\u611f\u3058\u3067\u3057\u3087\u3046\u3002<\/p>\n<pre class=\"lang:default decode:true\">select * From tableA\r\nLeft Join tableB On (tableA.connect_id = tableB.id)\r\nWhere tableB.id IS NULL<\/pre>\n<p>\u3084\u3063\u3066\u3044\u308b\u4e8b\u306f\u6b21\u306e\u901a\u308a\u3067\u3059\u3002<\/p>\n<div class=\"borderBox\">\n<ol>\n<li>tableA\u306btableB\u3092\u898b\u3048\u308b\u5f62(Left Join)\u3067\u7d50\u5408\u3059\u308b<\/li>\n<li>Join\u3057\u305f\u306e\u306btableB\u304c\u95a2\u9023\u3065\u3044\u3066\u3044\u306a\u3044(NULL\u3067\u3042\u308b)tableA\u3092\u62bd\u51fa\u3059\u308b<\/li>\n<li>\u7d50\u679c\u3001tableB\u306b\u5b58\u5728\u3057\u306a\u3044tableA\u306e\u30ec\u30b3\u30fc\u30c9\u304c\u53d6\u308a\u51fa\u3055\u308c\u308b<\/li>\n<\/ol>\n<\/div>\n<p>\u3053\u308c\u304c\u3001\u306a\u3093\u3068Eloquent\u3060\u3068\u4e00\u767a\uff01<\/p>\n<pre class=\"lang:default decode:true\"># 'tableB'\u306b\u306f\u30c6\u30fc\u30d6\u30eb\u540d\u3067\u306f\u306a\u304ftableB \u306eModel\u540d\u3092\u8a18\u8f09\r\n\r\n$query = tableA::doesntHave('tableB')-&gt;get();<\/pre>\n<p>\u3053\u308c\u306f\u8d85\u697d\u3002\u7834\u58ca\u529b\u534a\u7aef\u306a\u3044\u3002<\/p>\n<h3>\u3082\u3046\u3061\u3087\u3063\u3068\u8907\u96d1\u306b\u53d6\u5f97\u3059\u308b\u306a\u3089\u3010whereDoesntHave\u3011<\/h3>\n<p>JOIN\u5148\u306e\u30c6\u30fc\u30d6\u30eb\u3067\u62bd\u51fa\u6761\u4ef6\u3092\u4ed8\u3051\u3044\u305f\u3044\u6642\u304c\u3042\u308a\u307e\u3059\u3002\u305d\u306e\u6642\u306f\u3010whereDosentHave\u3011\u304c\u91cd\u5b9d\u3057\u307e\u3059\u3002<\/p>\n<pre class=\"lang:default decode:true\">#tableB.number = 1\u3001tableB.shop_id = NULL \u306e\u6642\u306e\u5024\u3068\u306e\u4e0d\u4e00\u81f4\u3092\u62bd\u51fa\r\n\r\n$query1 = tableA::whereDoesntHave('tableB', function ($a){\r\n    $a-&gt;where('number',1)\r\n      -&gt;whereNull('shop_id');\r\n})-&gt;get();<\/pre>\n<p>\u304a\uff5e\uff5e\u3001\u3053\u308c\u306fSQL\u66f8\u304f\u3088\u308a\u3060\u3044\u3076\u697d\u3067\u3059\u3002<\/p>\n<p>\u4f46\u3057\u3001\u4e88\u5b9a\u3068\u9055\u3046\u5024\u304c\u62bd\u51fa\u3055\u308c\u3066\u3057\u307e\u3046\u3053\u3068\u304c\u3042\u308a\u3001\u5c11\u3057\u60a9\u307f\u307e\u3057\u305f\u3002<\/p>\n<h3>\u62bd\u51fa\u6761\u4ef6 whereNotNull\u304c\u3082\u3046\u4e00\u3064\u7d61\u3080\u3068\u4f55\u6545\u304b\u6b63\u3057\u3044\u5024\u304c\u53d6\u5f97\u3067\u304d\u306a\u3044\u3002<\/h3>\n<pre class=\"lang:default decode:true\">$query1 = tableA::whereDoesntHave('tableB', function ($a){\r\n    $a-&gt;where('number',1)\r\n      -&gt;whereNull('shop_id')\r\n      -&gt;whereNotNull('item_id');\r\n})-&gt;get();<\/pre>\n<p>\u4f55\u3092\u3057\u305f\u304b\u3063\u305f\u304b\u3068\u8a00\u3046\u3068\u6b21\u306e\u901a\u308a\u3067\u3059\u3002<\/p>\n<div class=\"borderBox\">\n<ol>\n<li>\u5546\u54c1ID(tableB.item_id)\u304c\u5b58\u5728\u3059\u308b<\/li>\n<li>\u5e97\u8217ID(tableB.shop_id)\u304c\u7a7a\u767d<\/li>\n<li>tableB.number=1<\/li>\n<li>1\uff5e3\u306e\u6761\u4ef6\u3092\u6e80\u305f\u3059\u30ec\u30b3\u30fc\u30c9\u304c\u7121\u3044tableA\u306e\u30ea\u30b9\u30c8\u3092\u4f5c\u6210<\/li>\n<\/ol>\n<\/div>\n<p>\u4e0a\u306eEloquent\u3067\u306f\u4f55\u6545\u304b1\uff5e3\u3092\u3068\u3082\u306b\u6e80\u305f\u3059\u6761\u4ef6\u3067\u306f\u306a\u304f\u3010\u5546\u54c1ID(tableB.item_id)\u304c\u5b58\u5728\u3059\u308b\u3011\u5546\u54c1\u304c\u62bd\u51fa\u3055\u308c\u3066\u3057\u307e\u3044\u307e\u3057\u305f\u3002\u3053\u308c\u306f\u6b21\u306e\u3088\u3046\u306b\u5206\u3051\u3066\u66f8\u304f\u4e8b\u3067\u6539\u5584\u3057\u307e\u3057\u305f\u3002<\/p>\n<pre class=\"lang:default decode:true \">$query = tableA::orderBy('id','asc');\r\n\r\n    $query-&gt;whereDoesntHave('tableB', function ($a){\r\n        $a-&gt;where('number',1)\r\n          -&gt;whereNull('shop_id');\r\n    });\r\n    $query-&gt;whereHas('tableB', function ($b){\r\n        $b-&gt;whereNotNull('item_id');\r\n    });\r\n\r\n$query1 = $query-&gt;get();<\/pre>\n<p>whereNotNULL\u304c orWhereNotNULL\u306e\u69d8\u306b\u6271\u308f\u308c\u305f\u70b9\u306f\u8b0e\u3067\u3059\u3002\u304c\u89e3\u6c7a\u7b56\u3092\u898b\u3064\u3051\u305f\u306e\u3067\u826f\u3057\u3068\u3057\u307e\u3057\u3087\u3046\u3002<\/p>\n<p>\u4f55\u3088\u308a\u3082DB::\u3088\u308a\u3082\u660e\u3089\u304b\u306b\u51e6\u7406\u304c\u65e9\u3044\uff0140\u4e07\u30ec\u30b3\u30fc\u30c9\u3042\u3063\u3066\u3082\u5f85\u3064\u306e\u306f\u4e00\u77ac\u3067\u3059\u3002<\/p>\n<h3>\u307e\u3068\u3081<\/h3>\n<p>\u3044\u3084\u3041\uff5e\u3001Eloquent\u3063\u3066\u3059\u3054\u3044\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Laravel\uff1aEloquent\u30672\u3064\u306e\u30c6\u30fc\u30d6\u30eb\u306e\u4e0d\u4e00\u81f4\u3092\u62bd\u51fa\u3059\u308b \u79c1\u306f\u3082\u3068\u3082\u3068\u30d5\u30ec\u30fc\u30e0\u30ef\u30fc\u30af\u3092\u4f7f\u308f\u305aPHP\u3092\u66f8\u3044\u3066\u3044\u305f\u306e\u3067\u4f5c\u6210\u3057\u305f\u81ea\u4f5c\u30b7\u30b9\u30c6\u30e0\u3092\u4f5c\u308b\u307e\u3067DB\u306e\u62bd\u51fa\u306fSQL\u3092\u76f4\u63a5\u66f8\u3044\u3066\u3044\u307e\u3057\u305f\u3002\u305d\u306e\u70ba\u3001\u81ea\u4f5c\u30b7\u30b9\u30c6\u30e0\u4f5c\u6210\u521d [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":944,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"sns_share_botton_hide":"","vkExUnit_sns_title":"","_vk_print_noindex":"","sitemap_hide":"","_veu_custom_css":"","veu_display_promotion_alert":"","vkexunit_cta_each_option":"","_jetpack_feature_clip_id":0,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":false,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2},"jetpack_post_was_ever_published":false},"categories":[34],"tags":[422,153,99,98,53,423,49],"class_list":["post-1299","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-php-memorandum","tag-eloquent","tag-laravel","tag-mysql","tag-php","tag-53","tag-423","tag-49"],"veu_head_title_object":{"title":"","add_site_title":""},"jetpack_publicize_connections":[],"jetpack-related-posts":[],"jetpack_sharing_enabled":true,"jetpack_featured_media_url":"https:\/\/qol-kk.com\/wp2\/wp-content\/uploads\/2018\/11\/laravel_logo.png","_links":{"self":[{"href":"https:\/\/qol-kk.com\/wp2\/wp-json\/wp\/v2\/posts\/1299","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/qol-kk.com\/wp2\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/qol-kk.com\/wp2\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/qol-kk.com\/wp2\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/qol-kk.com\/wp2\/wp-json\/wp\/v2\/comments?post=1299"}],"version-history":[{"count":0,"href":"https:\/\/qol-kk.com\/wp2\/wp-json\/wp\/v2\/posts\/1299\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/qol-kk.com\/wp2\/wp-json\/wp\/v2\/media\/944"}],"wp:attachment":[{"href":"https:\/\/qol-kk.com\/wp2\/wp-json\/wp\/v2\/media?parent=1299"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/qol-kk.com\/wp2\/wp-json\/wp\/v2\/categories?post=1299"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/qol-kk.com\/wp2\/wp-json\/wp\/v2\/tags?post=1299"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}