{"id":9038,"date":"2026-02-17T11:56:44","date_gmt":"2026-02-17T02:56:44","guid":{"rendered":"https:\/\/www.gesource.jp\/weblog\/?p=9038"},"modified":"2026-02-15T15:59:51","modified_gmt":"2026-02-15T06:59:51","slug":"laravel-8%e4%bb%a5%e9%99%8d%e3%81%af-foreignidfor-%e3%82%92%e4%bd%bf%e3%81%86","status":"publish","type":"post","link":"https:\/\/www.gesource.jp\/weblog\/?p=9038","title":{"rendered":"Laravel 8\u4ee5\u964d\u306f foreignIdFor() \u3092\u4f7f\u3046"},"content":{"rendered":"<p>Laravel 8\u3067 <code>foreignIdFor()<\/code> \u30e1\u30bd\u30c3\u30c9\u304c\u5c0e\u5165\u3055\u308c\u307e\u3057\u305f\u3002<br \/>\n\u5916\u90e8\u30ad\u30fc\u30ab\u30e9\u30e0\u306e\u5b9a\u7fa9\u304c\u3088\u308a\u30b7\u30f3\u30d7\u30eb\u3067\u5b89\u5168\u306b\u306a\u308b\u6a5f\u80fd\u3067\u3059\u3002<\/p>\n<h2>foreignIdFor() \u3068\u306f<\/h2>\n<p><code>foreignIdFor()<\/code> \u306f\u3001\u30e2\u30c7\u30eb\u30af\u30e9\u30b9\u304b\u3089\u5916\u90e8\u30ad\u30fc\u30ab\u30e9\u30e0\u3092\u81ea\u52d5\u751f\u6210\u3059\u308b\u30e1\u30bd\u30c3\u30c9\u3067\u3059\u3002<\/p>\n<h3>Before: \u5f93\u6765\u306e\u66f8\u304d\u65b9<\/h3>\n<pre><code class=\"language-php\">$table-&gt;unsignedBigInteger('user_id');\n$table-&gt;foreign('user_id')\n    -&gt;references('id')\n    -&gt;on('users')\n    -&gt;onDelete('cascade');\n<\/code><\/pre>\n<h3>After: foreignIdFor() \u3092\u4f7f\u3063\u305f\u66f8\u304d\u65b9<\/h3>\n<pre><code class=\"language-php\">$table-&gt;foreignIdFor(User::class)\n    -&gt;constrained()\n    -&gt;onDelete('cascade');\n<\/code><\/pre>\n<p>\u308f\u305a\u304b1\u884c\u3067\u3001\u30ab\u30e9\u30e0\u306e\u4f5c\u6210\u304b\u3089\u5916\u90e8\u30ad\u30fc\u5236\u7d04\u306e\u8a2d\u5b9a\u307e\u3067\u5b8c\u7d50\u3057\u307e\u3059\u3002<\/p>\n<h2>constrained() \u30e1\u30bd\u30c3\u30c9\u306b\u3064\u3044\u3066<\/h2>\n<p><code>constrained()<\/code> \u306f\u5916\u90e8\u30ad\u30fc\u5236\u7d04\u3092\u81ea\u52d5\u7684\u306b\u8a2d\u5b9a\u3059\u308b\u30e1\u30bd\u30c3\u30c9\u3067\u3059\u3002<\/p>\n<pre><code class=\"language-php\">$table-&gt;foreignIdFor(User::class)-&gt;constrained();\n<\/code><\/pre>\n<p>\u3053\u306e\u30b3\u30fc\u30c9\u306f\u4ee5\u4e0b\u3092\u81ea\u52d5\u7684\u306b\u884c\u3044\u307e\u3059:<br \/>\n&#8211; \u30e2\u30c7\u30eb\u30af\u30e9\u30b9\u304b\u3089\u53c2\u7167\u5148\u306e\u30c6\u30fc\u30d6\u30eb\u540d\u3092\u63a8\u6e2c\uff08<code>User<\/code> \u2192 <code>users<\/code>\uff09<br \/>\n&#8211; \u4e3b\u30ad\u30fc\u30ab\u30e9\u30e0 <code>id<\/code> \u3078\u306e\u53c2\u7167\u3092\u4f5c\u6210<br \/>\n&#8211; \u5916\u90e8\u30ad\u30fc\u5236\u7d04\u3092\u8a2d\u5b9a<\/p>\n<p><code>constrained()<\/code> \u3092\u547c\u3070\u306a\u3051\u308c\u3070\u3001\u30ab\u30e9\u30e0\u306e\u307f\u304c\u4f5c\u6210\u3055\u308c\u3001\u5916\u90e8\u30ad\u30fc\u5236\u7d04\u306f\u8a2d\u5b9a\u3055\u308c\u307e\u305b\u3093\u3002<\/p>\n<pre><code class=\"language-php\">\/\/ \u30ab\u30e9\u30e0\u306e\u307f\u4f5c\u6210\uff08\u5236\u7d04\u306a\u3057\uff09\n$table-&gt;foreignIdFor(User::class);\n\n\/\/ \u30ab\u30e9\u30e0 + \u30a4\u30f3\u30c7\u30c3\u30af\u30b9\u306e\u307f\uff08\u5236\u7d04\u306a\u3057\uff09\n$table-&gt;foreignIdFor(User::class)-&gt;index();\n<\/code><\/pre>\n<h2>\u4e3b\u306a\u30e1\u30ea\u30c3\u30c8<\/h2>\n<h3>1. \u578b\u306e\u5b89\u5168\u6027\u304c\u5411\u4e0a<\/h3>\n<p>\u53c2\u7167\u5148\u306e\u30c6\u30fc\u30d6\u30eb\u306e\u4e3b\u30ad\u30fc\u578b\u3068\u81ea\u52d5\u7684\u306b\u4e00\u81f4\u3057\u307e\u3059\u3002<br \/>\n<code>users.id<\/code> \u304c <code>bigInteger<\/code> \u306a\u3089\u3001\u5916\u90e8\u30ad\u30fc\u3082 <code>unsignedBigInteger<\/code> \u3068\u3057\u3066\u4f5c\u6210\u3055\u308c\u307e\u3059\u3002<\/p>\n<h3>2. \u30bf\u30a4\u30d7\u30df\u30b9\u3092\u9632\u6b62<\/h3>\n<pre><code class=\"language-php\">\/\/ \u30e2\u30c7\u30eb\u30af\u30e9\u30b9\u304b\u3089\u81ea\u52d5\u751f\u6210\u3055\u308c\u308b\u305f\u3081\u3001\u30bf\u30a4\u30d7\u30df\u30b9\u304c\u8d77\u304d\u306a\u3044\n$table-&gt;foreignIdFor(User::class); \/\/ \u2192 user_id\n\n\/\/ \u5f93\u6765\u306e\u65b9\u6cd5\u3060\u3068\u30bf\u30a4\u30d7\u30df\u30b9\u306e\u30ea\u30b9\u30af\n$table-&gt;unsignedBigInteger('usr_id'); \/\/ \u3046\u3063\u304b\u308a\u30df\u30b9\n<\/code><\/pre>\n<h3>3. \u30b3\u30fc\u30c9\u306e\u610f\u56f3\u304c\u660e\u78ba<\/h3>\n<p>\u4e00\u76ee\u3067\u300c\u3053\u308c\u306f User \u30e2\u30c7\u30eb\u3078\u306e\u5916\u90e8\u30ad\u30fc\u3060\u300d\u3068\u5206\u304b\u308a\u307e\u3059\u3002<\/p>\n<h2>\u6ce8\u610f\u70b9<\/h2>\n<h3>\u30e2\u30c7\u30eb\u30af\u30e9\u30b9\u304c\u5fc5\u8981<\/h3>\n<p>\u5f53\u7136\u3067\u3059\u304c\u3001\u30e2\u30c7\u30eb\u30af\u30e9\u30b9\u304c\u5b58\u5728\u3057\u306a\u3044\u3068\u4f7f\u3048\u307e\u305b\u3093\u3002<\/p>\n<pre><code class=\"language-php\">\/\/ \u30e2\u30c7\u30eb\u304c\u306a\u3044\u5916\u90e8\u30b5\u30fc\u30d3\u30b9\u306e\u30c6\u30fc\u30d6\u30eb\u306a\u3069\n$table-&gt;unsignedBigInteger('external_service_id');\n<\/code><\/pre>\n<h3>\u30ab\u30b9\u30bf\u30e0\u30c6\u30fc\u30d6\u30eb\u540d\u306b\u6ce8\u610f<\/h3>\n<p>\u30e2\u30c7\u30eb\u540d\u3068\u30c6\u30fc\u30d6\u30eb\u540d\u304c\u898f\u5247\u901a\u308a\u3067\u306a\u3044\u5834\u5408\u306f\u3001\u660e\u793a\u7684\u306b\u6307\u5b9a\u304c\u5fc5\u8981\u3067\u3059\u3002<\/p>\n<pre><code class=\"language-php\">class Person extends Model {\n    protected $table = 'users';\n}\n\n\/\/ persons \u30c6\u30fc\u30d6\u30eb\u3092\u63a2\u3057\u3066\u3057\u307e\u3046\u305f\u3081\u3001\u660e\u793a\u7684\u306b\u6307\u5b9a\n$table-&gt;foreignIdFor(Person::class)-&gt;constrained('users');\n<\/code><\/pre>\n<h3>\u30ab\u30e9\u30e0\u540d\u306e\u30ab\u30b9\u30bf\u30de\u30a4\u30ba<\/h3>\n<p>\u30c7\u30d5\u30a9\u30eb\u30c8\u3067\u306f <code>{model}_id<\/code> \u3068\u3044\u3046\u540d\u524d\u306b\u306a\u308a\u307e\u3059\u3002<br \/>\n\u30ab\u30b9\u30bf\u30de\u30a4\u30ba\u3059\u308b\u5834\u5408\u306f\u7b2c2\u5f15\u6570\u3092\u4f7f\u3044\u307e\u3059\u3002<\/p>\n<pre><code class=\"language-php\">$table-&gt;foreignIdFor(User::class, 'author_id')-&gt;constrained('users');\n<\/code><\/pre>\n<h3>\u8907\u5408\u4e3b\u30ad\u30fc\u306b\u306f\u975e\u5bfe\u5fdc<\/h3>\n<p>\u8907\u5408\u4e3b\u30ad\u30fc\u3078\u306e\u53c2\u7167\u306b\u306f\u5f93\u6765\u306e\u65b9\u6cd5\u3092\u4f7f\u3046\u5fc5\u8981\u304c\u3042\u308a\u307e\u3059\u3002<\/p>\n<h2>\u3088\u304f\u4f7f\u3046\u30d1\u30bf\u30fc\u30f3<\/h2>\n<pre><code class=\"language-php\">\/\/ nullable \u306a\u5916\u90e8\u30ad\u30fc\n$table-&gt;foreignIdFor(User::class)-&gt;nullable()-&gt;constrained();\n\n\/\/ \u30ab\u30b9\u30b1\u30fc\u30c9\u524a\u9664\n$table-&gt;foreignIdFor(User::class)\n    -&gt;constrained()\n    -&gt;onDelete('cascade');\n\n\/\/ SET NULL\n$table-&gt;foreignIdFor(User::class)\n    -&gt;nullable()\n    -&gt;constrained()\n    -&gt;onDelete('set null');\n\n\/\/ \u30a4\u30f3\u30c7\u30c3\u30af\u30b9\u306e\u307f\uff08\u5916\u90e8\u30ad\u30fc\u5236\u7d04\u306a\u3057\uff09\n$table-&gt;foreignIdFor(User::class)-&gt;index();\n<\/code><\/pre>\n<h2>\u307e\u3068\u3081<\/h2>\n<p><code>foreignIdFor()<\/code> \u306f\u3001\u5916\u90e8\u30ad\u30fc\u5b9a\u7fa9\u3092\u3088\u308a\u5b89\u5168\u3067\u4fdd\u5b88\u3057\u3084\u3059\u304f\u3059\u308b\u512a\u308c\u305f\u6a5f\u80fd\u3067\u3059\u3002<br \/>\n\u7279\u5225\u306a\u7406\u7531\u304c\u306a\u3044\u9650\u308a\u3001Laravel 8\u4ee5\u964d\u306e\u30d7\u30ed\u30b8\u30a7\u30af\u30c8\u3067\u306f\u7a4d\u6975\u7684\u306b\u4f7f\u7528\u3059\u308b\u3053\u3068\u3092\u304a\u52e7\u3081\u3057\u307e\u3059\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Laravel 8\u3067 foreignIdFor() \u30e1\u30bd\u30c3\u30c9\u304c\u5c0e\u5165\u3055\u308c\u307e\u3057\u305f\u3002 \u5916\u90e8\u30ad\u30fc\u30ab\u30e9\u30e0\u306e\u5b9a\u7fa9\u304c\u3088\u308a\u30b7\u30f3\u30d7\u30eb\u3067\u5b89\u5168\u306b\u306a\u308b\u6a5f\u80fd\u3067\u3059\u3002 foreignIdFor() \u3068\u306f foreignIdFor() \u306f\u3001\u30e2\u30c7\u30eb\u30af\u30e9\u30b9 &#8230;<\/p>\n<p><a href=\"https:\/\/www.gesource.jp\/weblog\/?p=9038\" class=\"more-link\">Continue reading &lsquo;Laravel 8\u4ee5\u964d\u306f foreignIdFor() \u3092\u4f7f\u3046&rsquo; &raquo;<\/a><\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":true,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2}},"categories":[25],"tags":[186],"class_list":["post-9038","post","type-post","status-publish","format-standard","hentry","category-php","tag-laravel"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/www.gesource.jp\/weblog\/index.php?rest_route=\/wp\/v2\/posts\/9038","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.gesource.jp\/weblog\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.gesource.jp\/weblog\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.gesource.jp\/weblog\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.gesource.jp\/weblog\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=9038"}],"version-history":[{"count":0,"href":"https:\/\/www.gesource.jp\/weblog\/index.php?rest_route=\/wp\/v2\/posts\/9038\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.gesource.jp\/weblog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=9038"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.gesource.jp\/weblog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=9038"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.gesource.jp\/weblog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=9038"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}