{"id":2509,"date":"2025-03-24T08:47:08","date_gmt":"2025-03-23T23:47:08","guid":{"rendered":"https:\/\/dexall.co.jp\/articles\/?p=2509"},"modified":"2025-03-24T08:47:38","modified_gmt":"2025-03-23T23:47:38","slug":"%e3%80%90laravel%e5%85%a5%e9%96%80%e3%80%91belongsto%e3%83%aa%e3%83%ac%e3%83%bc%e3%82%b7%e3%83%a7%e3%83%b3%e3%81%ae%e5%ae%8c%e5%85%a8%e3%82%ac%e3%82%a4%e3%83%89%ef%bc%9a%e4%bd%bf%e3%81%84%e6%96%b9","status":"publish","type":"post","link":"https:\/\/dexall.co.jp\/articles\/?p=2509","title":{"rendered":"\u3010Laravel\u5165\u9580\u3011belongsTo\u30ea\u30ec\u30fc\u30b7\u30e7\u30f3\u306e\u5b8c\u5168\u30ac\u30a4\u30c9\uff1a\u4f7f\u3044\u65b9\u3068\u5b9f\u8df5\u4f8b7\u9078"},"content":{"rendered":"\n<div class=\"toc\"><br \/>\n<b>Warning<\/b>:  Undefined array key \"is_admin\" in <b>\/home\/xs392991\/dexall.co.jp\/public_html\/articles\/wp-content\/themes\/sango-theme\/library\/gutenberg\/dist\/classes\/Toc.php<\/b> on line <b>116<\/b><br \/>\n<br \/>\n<b>Warning<\/b>:  Undefined array key \"is_category_top\" in <b>\/home\/xs392991\/dexall.co.jp\/public_html\/articles\/wp-content\/themes\/sango-theme\/library\/gutenberg\/dist\/classes\/Toc.php<\/b> on line <b>121<\/b><br \/>\n<br \/>\n<b>Warning<\/b>:  Undefined array key \"is_top\" in <b>\/home\/xs392991\/dexall.co.jp\/public_html\/articles\/wp-content\/themes\/sango-theme\/library\/gutenberg\/dist\/classes\/Toc.php<\/b> on line <b>128<\/b><br \/>\n    <div id=\"toc_container\" class=\"sgb-toc--bullets js-smooth-scroll\" data-dialog-title=\"\u76ee\u6b21\">\n      <p class=\"toc_title\">\u76ee\u6b21 <\/p>\n      <ul class=\"toc_list\">  <li class=\"first\">    <a href=\"#i-0\">belongsTo\u30ea\u30ec\u30fc\u30b7\u30e7\u30f3\u3068\u306f\uff1f\u305d\u306e\u57fa\u672c\u6982\u5ff5\u3092\u7406\u89e3\u3057\u3088\u3046<\/a>    <ul class=\"menu_level_1\">      <li class=\"first\">        <a href=\"#i-1\">1\u5bfe\u591a\u306e\u9006\u30ea\u30ec\u30fc\u30b7\u30e7\u30f3\u3092\u5b9f\u73fe\u3059\u308bbelongsTo\u306e\u5f79\u5272<\/a>      <\/li>      <li>        <a href=\"#i-2\">hasOne\u3068belongsTo\u306e\u9055\u3044\u3092\u5fb9\u5e95\u89e3\u8aac<\/a>      <\/li>      <li class=\"last\">        <a href=\"#i-3\">\u91cd\u8981\u306a\u6ce8\u610f\u70b9<\/a>      <\/li>    <\/ul>  <\/li>  <li>    <a href=\"#i-4\">belongsTo\u30ea\u30ec\u30fc\u30b7\u30e7\u30f3\u306e\u5b9f\u88c5\u65b9\u6cd5\u3092\u89e3\u8aac<\/a>    <ul class=\"menu_level_1\">      <li class=\"first\">        <a href=\"#i-5\">\u30e2\u30c7\u30eb\u3067\u306ebelongsTo\u5b9a\u7fa9\u306e\u57fa\u672c\u69cb\u6587<\/a>      <\/li>      <li>        <a href=\"#i-8\">\u5916\u90e8\u30ad\u30fc\u3068\u30ed\u30fc\u30ab\u30eb\u30ad\u30fc\u306e\u30ab\u30b9\u30bf\u30de\u30a4\u30ba\u65b9\u6cd5<\/a>      <\/li>      <li>        <a href=\"#i-10\">\u30ea\u30ec\u30fc\u30b7\u30e7\u30f3\u540d\u547d\u540d\u898f\u5247\u3068\u30d9\u30b9\u30c8\u30d7\u30e9\u30af\u30c6\u30a3\u30b9<\/a>      <\/li>      <li class=\"last\">        <a href=\"#i-13\">\u5b9f\u88c5\u6642\u306e\u6ce8\u610f\u70b9\u3068Tips<\/a>      <\/li>    <\/ul>  <\/li>  <li>    <a href=\"#i-14\">\u5b9f\u8df5\u7684\u306abelongsTo\u6d3b\u7528\u4f8b7\u9078<\/a>    <ul class=\"menu_level_1\">      <li class=\"first\">        <a href=\"#i-15\">\u30e6\u30fc\u30b6\u30fc\u3068\u6295\u7a3f\u306e\u5b9f\u88c5\u4f8b<\/a>      <\/li>      <li>        <a href=\"#i-16\">\u5546\u54c1\u3068\u30ab\u30c6\u30b4\u30ea\u30fc\u306e\u5b9f\u88c5\u4f8b<\/a>      <\/li>      <li>        <a href=\"#i-17\">\u5f93\u696d\u54e1\u3068\u90e8\u7f72\u306e\u5b9f\u88c5\u4f8b<\/a>      <\/li>      <li>        <a href=\"#i-18\">\u30b3\u30e1\u30f3\u30c8\u3068\u8a18\u4e8b\u306e\u5b9f\u88c5\u4f8b<\/a>      <\/li>      <li>        <a href=\"#i-19\">\u6ce8\u6587\u3068\u9867\u5ba2\u306e\u5b9f\u88c5\u4f8b<\/a>      <\/li>      <li>        <a href=\"#i-20\">\u30d7\u30ed\u30d5\u30a3\u30fc\u30eb\u3068\u30e6\u30fc\u30b6\u30fc\u306e\u5b9f\u88c5\u4f8b<\/a>      <\/li>      <li class=\"last\">        <a href=\"#i-21\">\u4e88\u7d04\u3068\u65bd\u8a2d\u306e\u5b9f\u88c5\u4f8b<\/a>      <\/li>    <\/ul>  <\/li>  <li>    <a href=\"#i-22\">belongsTo\u30ea\u30ec\u30fc\u30b7\u30e7\u30f3\u306e\u9ad8\u5ea6\u306a\u4f7f\u3044\u65b9<\/a>    <ul class=\"menu_level_1\">      <li class=\"first\">        <a href=\"#i-23\">\u30c7\u30d5\u30a9\u30eb\u30c8\u5024\u306e\u8a2d\u5b9a\u65b9\u6cd5\u3068\u6d3b\u7528\u30b7\u30fc\u30f3<\/a>      <\/li>      <li>        <a href=\"#i-24\">Eager\u30ed\u30fc\u30c7\u30a3\u30f3\u30b0\u306b\u3088\u308b\u30d1\u30d5\u30a9\u30fc\u30de\u30f3\u30b9\u6700\u9069\u5316<\/a>      <\/li>      <li>        <a href=\"#i-28\">\u30bd\u30d5\u30c8\u30c7\u30ea\u30fc\u30c8\u6642\u306e\u6319\u52d5\u5236\u5fa1\u65b9\u6cd5<\/a>      <\/li>      <li>        <a href=\"#i-29\">\u9ad8\u5ea6\u306a\u30af\u30a8\u30ea\u30c6\u30af\u30cb\u30c3\u30af<\/a>      <\/li>      <li class=\"last\">        <a href=\"#i-30\">\u30d1\u30d5\u30a9\u30fc\u30de\u30f3\u30b9\u6700\u9069\u5316\u306e\u30d9\u30b9\u30c8\u30d7\u30e9\u30af\u30c6\u30a3\u30b9<\/a>      <\/li>    <\/ul>  <\/li>  <li>    <a href=\"#i-31\">belongsTo\u30ea\u30ec\u30fc\u30b7\u30e7\u30f3\u306e\u30c8\u30e9\u30d6\u30eb\u30b7\u30e5\u30fc\u30c6\u30a3\u30f3\u30b0<\/a>    <ul class=\"menu_level_1\">      <li class=\"first\">        <a href=\"#i-32\">\u3088\u304f\u3042\u308b\u30a8\u30e9\u30fc\u3068\u89e3\u6c7a\u65b9\u6cd5<\/a>      <\/li>      <li>        <a href=\"#i-35\">N+1\u554f\u984c\u306e\u56de\u907f\u65b9\u6cd5<\/a>      <\/li>      <li>        <a href=\"#i-39\">\u5faa\u74b0\u53c2\u7167\u306e\u9632\u6b62\u7b56<\/a>      <\/li>      <li>        <a href=\"#i-42\">\u30d1\u30d5\u30a9\u30fc\u30de\u30f3\u30b9\u95a2\u9023\u306e\u30c8\u30e9\u30d6\u30eb\u30b7\u30e5\u30fc\u30c6\u30a3\u30f3\u30b0<\/a>      <\/li>      <li class=\"last\">        <a href=\"#i-43\">\u30c7\u30d0\u30c3\u30b0\u3068\u30c8\u30e9\u30d6\u30eb\u30b7\u30e5\u30fc\u30c6\u30a3\u30f3\u30b0\u306e\u30d9\u30b9\u30c8\u30d7\u30e9\u30af\u30c6\u30a3\u30b9<\/a>      <\/li>    <\/ul>  <\/li>  <li class=\"last\">    <a href=\"#i-44\">\u307e\u3068\u3081\uff1a\u52b9\u679c\u7684\u306abelongsTo\u30ea\u30ec\u30fc\u30b7\u30e7\u30f3\u8a2d\u8a08\u6642\u306e\u30dd\u30a4\u30f3\u30c8<\/a>    <ul class=\"menu_level_1\">      <li class=\"first\">        <a href=\"#i-45\">belongsTo\u306e\u91cd\u8981\u306a\u6ce8\u610f\u70b9<\/a>      <\/li>      <li>        <a href=\"#i-49\">\u30d1\u30d5\u30a9\u30fc\u30de\u30f3\u30b9\u3068\u30e1\u30f3\u30c6\u30ca\u30f3\u30b9\u6027\u3092\u4e21\u7acb\u3059\u308b\u305f\u3081\u306e\u30d2\u30f3\u30c8<\/a>      <\/li>      <li>        <a href=\"#i-53\">\u5b9f\u88c5\u6642\u306e\u6700\u7d42\u30c1\u30a7\u30c3\u30af\u30ea\u30b9\u30c8<\/a>      <\/li>      <li class=\"last\">        <a href=\"#i-54\">\u307e\u3068\u3081<\/a>      <\/li>    <\/ul>  <\/li><\/ul>\n      <a href=\"#\" class=\"sgb-toc-button js-toc-button\" rel=\"nofollow\" data-open-dialog=\"true\"><i class=\"fa fa-list\"><\/i><span class=\"sgb-toc-button__text\">\u76ee\u6b21\u3078<\/span><\/a>\n    <\/div><\/div><h2 class=\"wp-block-heading\" id=\"i-0\">belongsTo\u30ea\u30ec\u30fc\u30b7\u30e7\u30f3\u3068\u306f\uff1f\u305d\u306e\u57fa\u672c\u6982\u5ff5\u3092\u7406\u89e3\u3057\u3088\u3046<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">\u30c7\u30fc\u30bf\u30d9\u30fc\u30b9\u8a2d\u8a08\u306b\u304a\u3044\u3066\u3001\u30c6\u30fc\u30d6\u30eb\u9593\u306e\u95a2\u4fc2\u6027\u3092\u9069\u5207\u306b\u5b9a\u7fa9\u3059\u308b\u3053\u3068\u306f\u975e\u5e38\u306b\u91cd\u8981\u3067\u3059\u3002Laravel\u3067\u306f\u3001\u3053\u306e\u95a2\u4fc2\u6027\u3092\u7c21\u5358\u306b\u5b9f\u88c5\u3067\u304d\u308b\u3088\u3046\u3001\u3055\u307e\u3056\u307e\u306a\u30ea\u30ec\u30fc\u30b7\u30e7\u30f3\u30e1\u30bd\u30c3\u30c9\u3092\u63d0\u4f9b\u3057\u3066\u3044\u307e\u3059\u3002\u305d\u306e\u4e2d\u3067\u3082<code>belongsTo<\/code>\u306f\u3001\u6700\u3082\u57fa\u672c\u7684\u304b\u3064\u91cd\u8981\u306a\u30ea\u30ec\u30fc\u30b7\u30e7\u30f3\u306e1\u3064\u3067\u3059\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"i-1\">1\u5bfe\u591a\u306e\u9006\u30ea\u30ec\u30fc\u30b7\u30e7\u30f3\u3092\u5b9f\u73fe\u3059\u308bbelongsTo\u306e\u5f79\u5272<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\"><code>belongsTo<\/code>\u30ea\u30ec\u30fc\u30b7\u30e7\u30f3\u306f\u30011\u5bfe\u591a\uff08One-to-Many\uff09\u95a2\u4fc2\u306b\u304a\u3051\u308b\u300c\u591a\u300d\u5074\u306e\u30e2\u30c7\u30eb\u3067\u5b9a\u7fa9\u3055\u308c\u308b\u30ea\u30ec\u30fc\u30b7\u30e7\u30f3\u3067\u3059\u3002\u4f8b\u3048\u3070\uff1a<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">\/\/ Post\u30e2\u30c7\u30eb\uff08\u300c\u591a\u300d\u5074\uff09\nclass Post extends Model\n{\n    public function user()\n    {\n        return $this-&gt;belongsTo(User::class);\n    }\n}\n\n\/\/ User\u30e2\u30c7\u30eb\uff08\u300c1\u300d\u5074\uff09\nclass User extends Model\n{\n    public function posts()\n    {\n        return $this-&gt;hasMany(Post::class);\n    }\n}<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u3053\u306e\u30ea\u30ec\u30fc\u30b7\u30e7\u30f3\u304c\u8868\u73fe\u3057\u3066\u3044\u308b\u306e\u306f\u4ee5\u4e0b\u306e\u3088\u3046\u306a\u95a2\u4fc2\u3067\u3059\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>1\u4eba\u306e\u30e6\u30fc\u30b6\u30fc\u306f\u8907\u6570\u306e\u6295\u7a3f\u3092\u6301\u3064\u3053\u3068\u304c\u3067\u304d\u308b\uff08hasMany\uff09<\/li>\n\n\n\n<li>1\u3064\u306e\u6295\u7a3f\u306f\u5fc5\u305a1\u4eba\u306e\u30e6\u30fc\u30b6\u30fc\u306b\u5c5e\u3057\u3066\u3044\u308b\uff08belongsTo\uff09<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">\u3064\u307e\u308a\u3001<code>belongsTo<\/code>\u306f\u300c\u3053\u306e\u8a18\u4e8b\u306f\u8ab0\u304b\u306b\u5c5e\u3057\u3066\u3044\u308b\u300d\u3068\u3044\u3046\u6240\u6709\u95a2\u4fc2\u3092\u8868\u73fe\u3059\u308b\u306e\u306b\u6700\u9069\u306a\u30ea\u30ec\u30fc\u30b7\u30e7\u30f3\u306a\u306e\u3067\u3059\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"i-2\">hasOne\u3068belongsTo\u306e\u9055\u3044\u3092\u5fb9\u5e95\u89e3\u8aac<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\"><code>hasOne<\/code>\u3068<code>belongsTo<\/code>\u306f\u3001\u4e00\u898b\u4f3c\u3066\u3044\u308b\u3088\u3046\u306b\u898b\u3048\u307e\u3059\u304c\u3001\u4ee5\u4e0b\u306e\u70b9\u3067\u5927\u304d\u304f\u7570\u306a\u308a\u307e\u3059\uff1a<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li>\u5916\u90e8\u30ad\u30fc\u306e\u4f4d\u7f6e<\/li>\n<\/ol>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>hasOne<\/code>: \u95a2\u9023\u5148\u306e\u30c6\u30fc\u30d6\u30eb\u306b\u5916\u90e8\u30ad\u30fc\u304c\u3042\u308b<\/li>\n\n\n\n<li><code>belongsTo<\/code>: \u81ea\u5206\u306e\u30c6\u30fc\u30d6\u30eb\u306b\u5916\u90e8\u30ad\u30fc\u304c\u3042\u308b<\/li>\n<\/ul>\n\n\n\n<ol class=\"wp-block-list\">\n<li>\u95a2\u4fc2\u6027\u306e\u65b9\u5411<\/li>\n<\/ol>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>hasOne<\/code>: \u300c\u79c1\u306f1\u3064\u306e\u3007\u3007\u3092\u6301\u3063\u3066\u3044\u308b\u300d<\/li>\n\n\n\n<li><code>belongsTo<\/code>: \u300c\u79c1\u306f\u3007\u3007\u306b\u5c5e\u3057\u3066\u3044\u308b\u300d<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">\u5177\u4f53\u4f8b\u3067\u898b\u3066\u307f\u307e\u3057\u3087\u3046\uff1a<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">\/\/ hasOne\u306e\u4f8b\uff1a\u30e6\u30fc\u30b6\u30fc\u3068\u30d7\u30ed\u30d5\u30a3\u30fc\u30eb\nclass User extends Model\n{\n    public function profile()\n    {\n        return $this-&gt;hasOne(Profile::class);\n    }\n}\n\n\/\/ belongsTo\u306e\u4f8b\uff1a\u30d7\u30ed\u30d5\u30a3\u30fc\u30eb\u3068\u30e6\u30fc\u30b6\u30fc\nclass Profile extends Model\n{\n    public function user()\n    {\n        return $this-&gt;belongsTo(User::class);\n    }\n}<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u3053\u306e\u5834\u5408\u306e\u30c6\u30fc\u30d6\u30eb\u69cb\u9020\uff1a<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">\/\/ users\u30c6\u30fc\u30d6\u30eb\nCREATE TABLE users (\n    id INT PRIMARY KEY,\n    name VARCHAR(255)\n);\n\n\/\/ profiles\u30c6\u30fc\u30d6\u30eb\uff08belongsTo\u5074\uff09\nCREATE TABLE profiles (\n    id INT PRIMARY KEY,\n    user_id INT,  \/\/ \u5916\u90e8\u30ad\u30fc\u306fbelongsTo\u5074\u306b\u3042\u308b\n    bio TEXT,\n    FOREIGN KEY (user_id) REFERENCES users(id)\n);<\/pre>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"i-3\">\u91cd\u8981\u306a\u6ce8\u610f\u70b9<\/h3>\n\n\n\n<ol class=\"wp-block-list\">\n<li>\u547d\u540d\u898f\u5247\uff1a<\/li>\n<\/ol>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>belongsTo<\/code>\u30e1\u30bd\u30c3\u30c9\u306e\u5f15\u6570\u306b\u306f\u3001\u95a2\u9023\u5148\u306e\u30e2\u30c7\u30eb\u30af\u30e9\u30b9\u3092\u6307\u5b9a<\/li>\n\n\n\n<li>\u30c7\u30d5\u30a9\u30eb\u30c8\u3067\u306f\u3001\u30e1\u30bd\u30c3\u30c9\u540d\u306b\u57fa\u3065\u3044\u3066\u5916\u90e8\u30ad\u30fc\u540d\u304c\u6c7a\u5b9a\u3055\u308c\u308b\uff08\u4f8b\uff1a<code>user<\/code>\u30e1\u30bd\u30c3\u30c9\u306a\u3089<code>user_id<\/code>\uff09<\/li>\n<\/ul>\n\n\n\n<ol class=\"wp-block-list\">\n<li>Nullable\u306a\u95a2\u4fc2\uff1a<\/li>\n<\/ol>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u5916\u90e8\u30ad\u30fc\u304c<code>NULL<\/code>\u3092\u8a31\u5bb9\u3059\u308b\u5834\u5408\u3001\u30aa\u30d7\u30b7\u30e7\u30ca\u30eb\u306a\u95a2\u4fc2\u3068\u3057\u3066\u5b9a\u7fa9\u3067\u304d\u308b<\/li>\n\n\n\n<li>\u3053\u306e\u5834\u5408\u3001\u95a2\u9023\u5148\u306e\u30e2\u30c7\u30eb\u304c\u5b58\u5728\u3057\u306a\u3044\u53ef\u80fd\u6027\u3092\u8003\u616e\u3057\u305f\u30b3\u30fc\u30c7\u30a3\u30f3\u30b0\u304c\u5fc5\u8981<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">\u3053\u306e\u3088\u3046\u306a\u57fa\u672c\u6982\u5ff5\u3092\u7406\u89e3\u3059\u308b\u3053\u3068\u3067\u3001\u3088\u308a\u8907\u96d1\u306a\u30ea\u30ec\u30fc\u30b7\u30e7\u30f3\u8a2d\u8a08\u3082\u5bb9\u6613\u306b\u306a\u308a\u307e\u3059\u3002\u6b21\u306e\u30bb\u30af\u30b7\u30e7\u30f3\u3067\u306f\u3001\u3053\u308c\u3089\u306e\u6982\u5ff5\u3092\u6d3b\u304b\u3057\u305f\u5177\u4f53\u7684\u306a\u5b9f\u88c5\u65b9\u6cd5\u306b\u3064\u3044\u3066\u8a73\u3057\u304f\u898b\u3066\u3044\u304d\u307e\u3057\u3087\u3046\u3002<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"i-4\">belongsTo\u30ea\u30ec\u30fc\u30b7\u30e7\u30f3\u306e\u5b9f\u88c5\u65b9\u6cd5\u3092\u89e3\u8aac<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"i-5\">\u30e2\u30c7\u30eb\u3067\u306ebelongsTo\u5b9a\u7fa9\u306e\u57fa\u672c\u69cb\u6587<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">belongsTo\u30ea\u30ec\u30fc\u30b7\u30e7\u30f3\u306e\u57fa\u672c\u7684\u306a\u5b9f\u88c5\u65b9\u6cd5\u304b\u3089\u3001\u3088\u308a\u9ad8\u5ea6\u306a\u4f7f\u3044\u65b9\u307e\u3067\u3001\u6bb5\u968e\u7684\u306b\u89e3\u8aac\u3057\u3066\u3044\u304d\u307e\u3059\u3002<\/p>\n\n\n\n<h4 class=\"wp-block-heading\" id=\"i-6\">\u57fa\u672c\u7684\u306a\u5b9f\u88c5\u65b9\u6cd5<\/h4>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">\/\/ \u57fa\u672c\u7684\u306a\u69cb\u6587\nclass Comment extends Model\n{\n    public function post()\n    {\n        return $this-&gt;belongsTo(Post::class);\n    }\n}\n\n\/\/ \u4f7f\u7528\u4f8b\n$comment = Comment::find(1);\n$post = $comment-&gt;post;  \/\/ \u95a2\u9023\u3059\u308bPost\u30e2\u30c7\u30eb\u3092\u53d6\u5f97<\/pre>\n\n\n\n<h4 class=\"wp-block-heading\" id=\"i-7\">\u30ab\u30b9\u30bf\u30de\u30a4\u30ba\u30aa\u30d7\u30b7\u30e7\u30f3\u4ed8\u304d\u306e\u5b9f\u88c5<\/h4>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">class Comment extends Model\n{\n    public function post()\n    {\n        return $this-&gt;belongsTo(\n            Post::class,         \/\/ \u95a2\u9023\u3059\u308b\u30e2\u30c7\u30eb\n            'post_id',          \/\/ \u5916\u90e8\u30ad\u30fc\n            'id',               \/\/ \u95a2\u9023\u5148\u306e\u4e3b\u30ad\u30fc\n            'post'              \/\/ \u30ea\u30ec\u30fc\u30b7\u30e7\u30f3\u540d\n        );\n    }\n}<\/pre>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"i-8\">\u5916\u90e8\u30ad\u30fc\u3068\u30ed\u30fc\u30ab\u30eb\u30ad\u30fc\u306e\u30ab\u30b9\u30bf\u30de\u30a4\u30ba\u65b9\u6cd5<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">\u30c7\u30d5\u30a9\u30eb\u30c8\u306e\u547d\u540d\u898f\u5247\u304b\u3089\u5916\u308c\u308b\u5834\u5408\u3084\u3001\u7279\u5225\u306a\u8981\u4ef6\u304c\u3042\u308b\u5834\u5408\u306e\u30ab\u30b9\u30bf\u30de\u30a4\u30ba\u65b9\u6cd5\u3092\u8aac\u660e\u3057\u307e\u3059\u3002<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">\/\/ \u6a19\u6e96\u7684\u306a\u30ad\u30fc\u540d\u3092\u4f7f\u7528\u3057\u306a\u3044\u5834\u5408\u306e\u4f8b\nclass Profile extends Model\n{\n    public function user()\n    {\n        return $this-&gt;belongsTo(\n            User::class,\n            'belongs_to_user_id',    \/\/ \u30ab\u30b9\u30bf\u30e0\u5916\u90e8\u30ad\u30fc\n            'custom_id'              \/\/ \u30ab\u30b9\u30bf\u30e0\u30ed\u30fc\u30ab\u30eb\u30ad\u30fc\n        );\n    }\n}\n\n\/\/ \u30c6\u30fc\u30d6\u30eb\u5b9a\u7fa9\u306e\u4f8b\nSchema::create('profiles', function (Blueprint $table) {\n    $table-&gt;id();\n    $table-&gt;unsignedBigInteger('belongs_to_user_id');  \/\/ \u30ab\u30b9\u30bf\u30e0\u5916\u90e8\u30ad\u30fc\n    $table-&gt;string('name');\n    $table-&gt;foreign('belongs_to_user_id')\n          -&gt;references('custom_id')\n          -&gt;on('users')\n          -&gt;onDelete('cascade');\n});<\/pre>\n\n\n\n<h4 class=\"wp-block-heading\" id=\"i-9\">\u30ad\u30fc\u306e\u30ab\u30b9\u30bf\u30de\u30a4\u30ba\u306b\u95a2\u3059\u308b\u91cd\u8981\u306a\u30dd\u30a4\u30f3\u30c8<\/h4>\n\n\n\n<ol class=\"wp-block-list\">\n<li>\u5916\u90e8\u30ad\u30fc\u306e\u547d\u540d\u898f\u5247\uff1a<\/li>\n<\/ol>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u30c7\u30d5\u30a9\u30eb\u30c8: \u95a2\u9023\u3059\u308b\u30e2\u30c7\u30eb\u540d\u306e\u5358\u6570\u5f62 + \u201c_id\u201d<\/li>\n\n\n\n<li>\u30ab\u30b9\u30bf\u30e0: \u4efb\u610f\u306e\u540d\u524d\u3092\u6307\u5b9a\u53ef\u80fd<\/li>\n<\/ul>\n\n\n\n<ol class=\"wp-block-list\">\n<li>\u53c2\u7167\u30ad\u30fc\u306e\u6307\u5b9a\uff1a<\/li>\n<\/ol>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u30c7\u30d5\u30a9\u30eb\u30c8: \u95a2\u9023\u3059\u308b\u30e2\u30c7\u30eb\u306e\u4e3b\u30ad\u30fc\uff08\u901a\u5e38\u306f\u2019id\u2019\uff09<\/li>\n\n\n\n<li>\u30ab\u30b9\u30bf\u30e0: \u4efb\u610f\u306e\u30e6\u30cb\u30fc\u30af\u306a\u30ab\u30e9\u30e0\u3092\u6307\u5b9a\u53ef\u80fd<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"i-10\">\u30ea\u30ec\u30fc\u30b7\u30e7\u30f3\u540d\u547d\u540d\u898f\u5247\u3068\u30d9\u30b9\u30c8\u30d7\u30e9\u30af\u30c6\u30a3\u30b9<\/h3>\n\n\n\n<h4 class=\"wp-block-heading\" id=\"i-11\">\u547d\u540d\u898f\u5247\u306e\u30d9\u30b9\u30c8\u30d7\u30e9\u30af\u30c6\u30a3\u30b9<\/h4>\n\n\n\n<ol class=\"wp-block-list\">\n<li>\u30e1\u30bd\u30c3\u30c9\u540d\u306e\u9078\u5b9a<\/li>\n<\/ol>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">\/\/ Good: \u95a2\u4fc2\u6027\u304c\u660e\u78ba\u306a\u547d\u540d\nclass Order extends Model\n{\n    public function customer()\n    {\n        return $this-&gt;belongsTo(Customer::class);\n    }\n}\n\n\/\/ Better: \u3088\u308a\u5177\u4f53\u7684\u306a\u95a2\u4fc2\u6027\u3092\u8868\u73fe\nclass Order extends Model\n{\n    public function purchasingCustomer()\n    {\n        return $this-&gt;belongsTo(Customer::class);\n    }\n}<\/pre>\n\n\n\n<ol start=\"2\" class=\"wp-block-list\">\n<li>\u8907\u6570\u306e\u30ea\u30ec\u30fc\u30b7\u30e7\u30f3\u306e\u5b9a\u7fa9<\/li>\n<\/ol>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">class Comment extends Model\n{\n    \/\/ \u6295\u7a3f\u8005\u306e\u30ea\u30ec\u30fc\u30b7\u30e7\u30f3\n    public function author()\n    {\n        return $this-&gt;belongsTo(User::class, 'author_id');\n    }\n\n    \/\/ \u6700\u7d42\u66f4\u65b0\u8005\u306e\u30ea\u30ec\u30fc\u30b7\u30e7\u30f3\n    public function lastModifiedBy()\n    {\n        return $this-&gt;belongsTo(User::class, 'last_modified_by_id');\n    }\n}<\/pre>\n\n\n\n<h4 class=\"wp-block-heading\" id=\"i-12\">\u5b9f\u88c5\u6642\u306e\u91cd\u8981\u306a\u30ac\u30a4\u30c9\u30e9\u30a4\u30f3<\/h4>\n\n\n\n<ol class=\"wp-block-list\">\n<li>\u30e2\u30c7\u30eb\u306e\u4e00\u8cab\u6027<\/li>\n<\/ol>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u95a2\u9023\u3059\u308b\u30e2\u30c7\u30eb\u9593\u3067\u547d\u540d\u898f\u5247\u3092\u7d71\u4e00\u3059\u308b<\/li>\n\n\n\n<li>\u30ea\u30ec\u30fc\u30b7\u30e7\u30f3\u540d\u306f\u76ee\u7684\u3092\u660e\u78ba\u306b\u8868\u73fe\u3059\u308b<\/li>\n<\/ul>\n\n\n\n<ol class=\"wp-block-list\">\n<li>\u30d1\u30d5\u30a9\u30fc\u30de\u30f3\u30b9\u3078\u306e\u914d\u616e<\/li>\n<\/ol>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">\/\/ Eager\u30ed\u30fc\u30c7\u30a3\u30f3\u30b0\u3092\u8003\u616e\u3057\u305f\u5b9f\u88c5\nclass Post extends Model\n{\n    \/\/ \u3088\u304f\u4e00\u7dd2\u306b\u4f7f\u7528\u3055\u308c\u308b\u30ea\u30ec\u30fc\u30b7\u30e7\u30f3\u3092\u5b9a\u7fa9\n    protected $with = ['author'];\n\n    public function author()\n    {\n        return $this-&gt;belongsTo(User::class);\n    }\n}<\/pre>\n\n\n\n<ol start=\"3\" class=\"wp-block-list\">\n<li>\u30c7\u30fc\u30bf\u306e\u6574\u5408\u6027<\/li>\n<\/ol>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">\/\/ \u524a\u9664\u6642\u306e\u52d5\u4f5c\u3092\u660e\u793a\u7684\u306b\u5b9a\u7fa9\nclass Comment extends Model\n{\n    public function post()\n    {\n        return $this-&gt;belongsTo(Post::class)\n                    -&gt;withTrashed();  \/\/ \u30bd\u30d5\u30c8\u30c7\u30ea\u30fc\u30c8\u5bfe\u5fdc\n    }\n}<\/pre>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"i-13\">\u5b9f\u88c5\u6642\u306e\u6ce8\u610f\u70b9\u3068Tips<\/h3>\n\n\n\n<ol class=\"wp-block-list\">\n<li>PHPDoc\u306e\u6d3b\u7528<\/li>\n<\/ol>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">\/**\n * Get the user that owns the profile.\n * \n * @return \\Illuminate\\Database\\Eloquent\\Relations\\BelongsTo\n *\/\npublic function user()\n{\n    return $this-&gt;belongsTo(User::class);\n}<\/pre>\n\n\n\n<ol start=\"2\" class=\"wp-block-list\">\n<li>\u30c7\u30d5\u30a9\u30eb\u30c8\u5024\u306e\u8a2d\u5b9a<\/li>\n<\/ol>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">class Comment extends Model\n{\n    public function post()\n    {\n        return $this-&gt;belongsTo(Post::class)-&gt;withDefault([\n            'title' =&gt; 'Deleted Post',\n            'content' =&gt; 'This post has been removed.'\n        ]);\n    }\n}<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u3053\u308c\u3089\u306e\u5b9f\u88c5\u65b9\u6cd5\u3092\u7406\u89e3\u3059\u308b\u3053\u3068\u3067\u3001\u3088\u308a\u67d4\u8edf\u3067\u4fdd\u5b88\u6027\u306e\u9ad8\u3044\u30ea\u30ec\u30fc\u30b7\u30e7\u30f3\u3092\u69cb\u7bc9\u3059\u308b\u3053\u3068\u304c\u3067\u304d\u307e\u3059\u3002\u6b21\u306e\u30bb\u30af\u30b7\u30e7\u30f3\u3067\u306f\u3001\u3053\u308c\u3089\u306e\u77e5\u8b58\u3092\u6d3b\u304b\u3057\u305f\u5177\u4f53\u7684\u306a\u5b9f\u88c5\u4f8b\u3092\u898b\u3066\u3044\u304d\u307e\u3057\u3087\u3046\u3002<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"i-14\">\u5b9f\u8df5\u7684\u306abelongsTo\u6d3b\u7528\u4f8b7\u9078<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">\u5b9f\u969b\u306e\u30a2\u30d7\u30ea\u30b1\u30fc\u30b7\u30e7\u30f3\u958b\u767a\u3067\u983b\u51fa\u3059\u308b\u30b7\u30ca\u30ea\u30aa\u306b\u57fa\u3065\u3044\u3066\u3001belongsTo\u30ea\u30ec\u30fc\u30b7\u30e7\u30f3\u306e\u5177\u4f53\u7684\u306a\u5b9f\u88c5\u4f8b\u3092\u7d39\u4ecb\u3057\u307e\u3059\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"i-15\">\u30e6\u30fc\u30b6\u30fc\u3068\u6295\u7a3f\u306e\u5b9f\u88c5\u4f8b<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">\u30d6\u30ed\u30b0\u30b7\u30b9\u30c6\u30e0\u3067\u306e\u6295\u7a3f\u3068\u30e6\u30fc\u30b6\u30fc\u306e\u95a2\u4fc2\u3092\u5b9f\u88c5\u3059\u308b\u4f8b\u3067\u3059\u3002<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">\/\/ \u30c7\u30fc\u30bf\u30d9\u30fc\u30b9\u306e\u30de\u30a4\u30b0\u30ec\u30fc\u30b7\u30e7\u30f3\nSchema::create('users', function (Blueprint $table) {\n    $table-&gt;id();\n    $table-&gt;string('name');\n    $table-&gt;string('email')-&gt;unique();\n    $table-&gt;timestamps();\n});\n\nSchema::create('posts', function (Blueprint $table) {\n    $table-&gt;id();\n    $table-&gt;foreignId('user_id')-&gt;constrained()-&gt;onDelete('cascade');\n    $table-&gt;string('title');\n    $table-&gt;text('content');\n    $table-&gt;timestamps();\n});\n\n\/\/ Post\u30e2\u30c7\u30eb\nclass Post extends Model\n{\n    protected $fillable = ['title', 'content', 'user_id'];\n\n    public function author()\n    {\n        return $this-&gt;belongsTo(User::class, 'user_id')\n                    -&gt;withDefault([\n                        'name' =&gt; '\u9000\u4f1a\u6e08\u307f\u30e6\u30fc\u30b6\u30fc'\n                    ]);\n    }\n}\n\n\/\/ \u4f7f\u7528\u4f8b\n$post = Post::find(1);\necho \"\u6295\u7a3f\u8005: \" . $post-&gt;author-&gt;name;<\/pre>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"i-16\">\u5546\u54c1\u3068\u30ab\u30c6\u30b4\u30ea\u30fc\u306e\u5b9f\u88c5\u4f8b<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">EC\u30b5\u30a4\u30c8\u3067\u306e\u5546\u54c1\u3068\u30ab\u30c6\u30b4\u30ea\u30fc\u306e\u95a2\u4fc2\u3092\u5b9f\u88c5\u3059\u308b\u4f8b\u3067\u3059\u3002<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">Schema::create('categories', function (Blueprint $table) {\n    $table-&gt;id();\n    $table-&gt;string('name');\n    $table-&gt;string('slug')-&gt;unique();\n    $table-&gt;timestamps();\n});\n\nSchema::create('products', function (Blueprint $table) {\n    $table-&gt;id();\n    $table-&gt;foreignId('category_id')-&gt;constrained();\n    $table-&gt;string('name');\n    $table-&gt;decimal('price', 10, 2);\n    $table-&gt;timestamps();\n});\n\nclass Product extends Model\n{\n    protected $fillable = ['name', 'price', 'category_id'];\n\n    public function category()\n    {\n        return $this-&gt;belongsTo(Category::class)\n                    -&gt;withDefault(['name' =&gt; '\u672a\u5206\u985e']);\n    }\n}\n\n\/\/ \u30ab\u30c6\u30b4\u30ea\u30fc\u5225\u5546\u54c1\u4e00\u89a7\u306e\u53d6\u5f97\u4f8b\n$products = Product::with('category')\n    -&gt;whereHas('category', function($query) {\n        $query-&gt;where('slug', 'electronics');\n    })-&gt;get();<\/pre>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"i-17\">\u5f93\u696d\u54e1\u3068\u90e8\u7f72\u306e\u5b9f\u88c5\u4f8b<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">\u793e\u5185\u30b7\u30b9\u30c6\u30e0\u3067\u306e\u5f93\u696d\u54e1\u3068\u90e8\u7f72\u306e\u95a2\u4fc2\u3092\u5b9f\u88c5\u3059\u308b\u4f8b\u3067\u3059\u3002<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">Schema::create('departments', function (Blueprint $table) {\n    $table-&gt;id();\n    $table-&gt;string('name');\n    $table-&gt;string('code')-&gt;unique();\n    $table-&gt;timestamps();\n});\n\nSchema::create('employees', function (Blueprint $table) {\n    $table-&gt;id();\n    $table-&gt;foreignId('department_id')-&gt;nullable()-&gt;constrained();\n    $table-&gt;string('employee_code')-&gt;unique();\n    $table-&gt;string('name');\n    $table-&gt;timestamps();\n});\n\nclass Employee extends Model\n{\n    protected $fillable = ['name', 'employee_code', 'department_id'];\n\n    public function department()\n    {\n        return $this-&gt;belongsTo(Department::class)\n                    -&gt;withDefault(function ($department, $employee) {\n                        $department-&gt;name = '\u90e8\u7f72\u672a\u914d\u5c5e';\n                        $department-&gt;code = 'NONE';\n                    });\n    }\n}\n\n\/\/ \u90e8\u7f72\u5225\u5f93\u696d\u54e1\u30ea\u30b9\u30c8\u306e\u53d6\u5f97\n$employees = Employee::with('department')\n    -&gt;orderBy('employee_code')\n    -&gt;get()\n    -&gt;groupBy('department.name');<\/pre>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"i-18\">\u30b3\u30e1\u30f3\u30c8\u3068\u8a18\u4e8b\u306e\u5b9f\u88c5\u4f8b<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">\u30d6\u30ed\u30b0\u3084\u30cb\u30e5\u30fc\u30b9\u30b5\u30a4\u30c8\u3067\u306e\u30b3\u30e1\u30f3\u30c8\u6a5f\u80fd\u3092\u5b9f\u88c5\u3059\u308b\u4f8b\u3067\u3059\u3002<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">Schema::create('articles', function (Blueprint $table) {\n    $table-&gt;id();\n    $table-&gt;string('title');\n    $table-&gt;text('content');\n    $table-&gt;timestamps();\n});\n\nSchema::create('comments', function (Blueprint $table) {\n    $table-&gt;id();\n    $table-&gt;foreignId('article_id')-&gt;constrained()-&gt;onDelete('cascade');\n    $table-&gt;foreignId('user_id')-&gt;constrained()-&gt;onDelete('cascade');\n    $table-&gt;text('content');\n    $table-&gt;timestamps();\n});\n\nclass Comment extends Model\n{\n    protected $fillable = ['content', 'article_id', 'user_id'];\n\n    public function article()\n    {\n        return $this-&gt;belongsTo(Article::class);\n    }\n\n    public function user()\n    {\n        return $this-&gt;belongsTo(User::class);\n    }\n}\n\n\/\/ \u30b3\u30e1\u30f3\u30c8\u4e00\u89a7\u53d6\u5f97\u306e\u4f8b\n$comments = Comment::with(['article', 'user'])\n    -&gt;latest()\n    -&gt;paginate(20);<\/pre>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"i-19\">\u6ce8\u6587\u3068\u9867\u5ba2\u306e\u5b9f\u88c5\u4f8b<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">EC\u30b5\u30a4\u30c8\u3067\u306e\u6ce8\u6587\u30b7\u30b9\u30c6\u30e0\u3092\u5b9f\u88c5\u3059\u308b\u4f8b\u3067\u3059\u3002<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">Schema::create('customers', function (Blueprint $table) {\n    $table-&gt;id();\n    $table-&gt;string('name');\n    $table-&gt;string('email')-&gt;unique();\n    $table-&gt;timestamps();\n});\n\nSchema::create('orders', function (Blueprint $table) {\n    $table-&gt;id();\n    $table-&gt;foreignId('customer_id')-&gt;constrained();\n    $table-&gt;string('order_number')-&gt;unique();\n    $table-&gt;decimal('total_amount', 10, 2);\n    $table-&gt;enum('status', ['pending', 'processing', 'completed', 'cancelled']);\n    $table-&gt;timestamps();\n});\n\nclass Order extends Model\n{\n    protected $fillable = ['order_number', 'total_amount', 'status', 'customer_id'];\n\n    public function customer()\n    {\n        return $this-&gt;belongsTo(Customer::class)\n                    -&gt;withTrashed(); \/\/ \u9867\u5ba2\u304c\u524a\u9664\u3055\u308c\u3066\u3082\u6ce8\u6587\u5c65\u6b74\u306f\u4fdd\u6301\n    }\n\n    \/\/ \u6ce8\u6587\u30b9\u30c6\u30fc\u30bf\u30b9\u306e\u66f4\u65b0\u6642\u306b\u9867\u5ba2\u306b\u30e1\u30fc\u30eb\u901a\u77e5\n    protected static function booted()\n    {\n        static::updated(function ($order) {\n            if ($order-&gt;isDirty('status')) {\n                $order-&gt;customer-&gt;notify(new OrderStatusUpdated($order));\n            }\n        });\n    }\n}<\/pre>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"i-20\">\u30d7\u30ed\u30d5\u30a3\u30fc\u30eb\u3068\u30e6\u30fc\u30b6\u30fc\u306e\u5b9f\u88c5\u4f8b<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">\u30bd\u30fc\u30b7\u30e3\u30eb\u30e1\u30c7\u30a3\u30a2\u3067\u306e\u30e6\u30fc\u30b6\u30fc\u30d7\u30ed\u30d5\u30a3\u30fc\u30eb\u3092\u5b9f\u88c5\u3059\u308b\u4f8b\u3067\u3059\u3002<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">Schema::create('profiles', function (Blueprint $table) {\n    $table-&gt;id();\n    $table-&gt;foreignId('user_id')-&gt;unique()-&gt;constrained();\n    $table-&gt;string('bio')-&gt;nullable();\n    $table-&gt;string('location')-&gt;nullable();\n    $table-&gt;date('birthday')-&gt;nullable();\n    $table-&gt;timestamps();\n});\n\nclass Profile extends Model\n{\n    protected $fillable = ['bio', 'location', 'birthday'];\n\n    public function user()\n    {\n        return $this-&gt;belongsTo(User::class)\n                    -&gt;withDefault(function ($user, $profile) {\n                        throw new \\Exception('\u95a2\u9023\u3059\u308b\u30e6\u30fc\u30b6\u30fc\u304c\u898b\u3064\u304b\u308a\u307e\u305b\u3093\u3002');\n                    });\n    }\n\n    \/\/ \u30a2\u30af\u30bb\u30b5\u306e\u6d3b\u7528\u4f8b\n    public function getAgeAttribute()\n    {\n        return $this-&gt;birthday ? Carbon::parse($this-&gt;birthday)-&gt;age : null;\n    }\n}<\/pre>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"i-21\">\u4e88\u7d04\u3068\u65bd\u8a2d\u306e\u5b9f\u88c5\u4f8b<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">\u4e88\u7d04\u30b7\u30b9\u30c6\u30e0\u3067\u306e\u4e88\u7d04\u3068\u65bd\u8a2d\u306e\u95a2\u4fc2\u3092\u5b9f\u88c5\u3059\u308b\u4f8b\u3067\u3059\u3002<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">Schema::create('facilities', function (Blueprint $table) {\n    $table-&gt;id();\n    $table-&gt;string('name');\n    $table-&gt;integer('capacity');\n    $table-&gt;boolean('is_available')-&gt;default(true);\n    $table-&gt;timestamps();\n});\n\nSchema::create('bookings', function (Blueprint $table) {\n    $table-&gt;id();\n    $table-&gt;foreignId('facility_id')-&gt;constrained();\n    $table-&gt;foreignId('user_id')-&gt;constrained();\n    $table-&gt;dateTime('start_time');\n    $table-&gt;dateTime('end_time');\n    $table-&gt;timestamps();\n});\n\nclass Booking extends Model\n{\n    protected $fillable = ['facility_id', 'user_id', 'start_time', 'end_time'];\n\n    protected $casts = [\n        'start_time' =&gt; 'datetime',\n        'end_time' =&gt; 'datetime'\n    ];\n\n    public function facility()\n    {\n        return $this-&gt;belongsTo(Facility::class)\n                    -&gt;withDefault(['name' =&gt; '\u524a\u9664\u3055\u308c\u305f\u65bd\u8a2d']);\n    }\n\n    public function user()\n    {\n        return $this-&gt;belongsTo(User::class);\n    }\n\n    \/\/ \u4e88\u7d04\u306e\u91cd\u8907\u30c1\u30a7\u30c3\u30af\n    public function scopeOverlapping($query, $start_time, $end_time, $facility_id)\n    {\n        return $query-&gt;where('facility_id', $facility_id)\n                     -&gt;where('start_time', '&lt;', $end_time)\n                     -&gt;where('end_time', '&gt;', $start_time);\n    }\n}\n\n\/\/ \u4e88\u7d04\u4f5c\u6210\u6642\u306e\u4f7f\u7528\u4f8b\n$booking = Booking::create([\n    'facility_id' =&gt; $request-&gt;facility_id,\n    'user_id' =&gt; Auth::id(),\n    'start_time' =&gt; $request-&gt;start_time,\n    'end_time' =&gt; $request-&gt;end_time,\n]);<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u5404\u5b9f\u88c5\u4f8b\u3067\u306f\u3001\u4ee5\u4e0b\u306e\u8981\u7d20\u3092\u8003\u616e\u3057\u3066\u3044\u307e\u3059\uff1a<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li>\u9069\u5207\u306a\u30c7\u30fc\u30bf\u30d9\u30fc\u30b9\u69cb\u9020\u306e\u8a2d\u8a08<\/li>\n\n\n\n<li>\u30e2\u30c7\u30eb\u9593\u306e\u95a2\u4fc2\u6027\u306e\u660e\u78ba\u306a\u5b9a\u7fa9<\/li>\n\n\n\n<li>\u30d3\u30b8\u30cd\u30b9\u30ed\u30b8\u30c3\u30af\u306e\u5b9f\u88c5<\/li>\n\n\n\n<li>\u30a8\u30e9\u30fc\u51e6\u7406\u3068\u30c7\u30d5\u30a9\u30eb\u30c8\u5024\u306e\u8a2d\u5b9a<\/li>\n\n\n\n<li>\u30d1\u30d5\u30a9\u30fc\u30de\u30f3\u30b9\u3092\u8003\u616e\u3057\u305fEager\u30ed\u30fc\u30c7\u30a3\u30f3\u30b0\u306e\u6d3b\u7528<\/li>\n\n\n\n<li>\u30bb\u30ad\u30e5\u30ea\u30c6\u30a3\u3068\u30c7\u30fc\u30bf\u6574\u5408\u6027\u306e\u78ba\u4fdd<\/li>\n<\/ol>\n\n\n\n<p class=\"wp-block-paragraph\">\u3053\u308c\u3089\u306e\u5b9f\u88c5\u4f8b\u3092\u53c2\u8003\u306b\u3001\u81ea\u8eab\u306e\u30d7\u30ed\u30b8\u30a7\u30af\u30c8\u306b\u9069\u3057\u305f\u5f62\u3067belongsTo\u30ea\u30ec\u30fc\u30b7\u30e7\u30f3\u3092\u6d3b\u7528\u3057\u3066\u304f\u3060\u3055\u3044\u3002<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"i-22\">belongsTo\u30ea\u30ec\u30fc\u30b7\u30e7\u30f3\u306e\u9ad8\u5ea6\u306a\u4f7f\u3044\u65b9<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"i-23\">\u30c7\u30d5\u30a9\u30eb\u30c8\u5024\u306e\u8a2d\u5b9a\u65b9\u6cd5\u3068\u6d3b\u7528\u30b7\u30fc\u30f3<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">belongsTo\u30ea\u30ec\u30fc\u30b7\u30e7\u30f3\u3067\u306f\u3001\u95a2\u9023\u5148\u306e\u30e2\u30c7\u30eb\u304c\u5b58\u5728\u3057\u306a\u3044\u5834\u5408\u306e\u6319\u52d5\u3092\u30ab\u30b9\u30bf\u30de\u30a4\u30ba\u3067\u304d\u307e\u3059\u3002<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">class Article extends Model\n{\n    public function author()\n    {\n        \/\/ \u57fa\u672c\u7684\u306a\u30c7\u30d5\u30a9\u30eb\u30c8\u5024\u306e\u8a2d\u5b9a\n        return $this-&gt;belongsTo(User::class)\n                    -&gt;withDefault([\n                        'name' =&gt; '\u533f\u540d\u30e6\u30fc\u30b6\u30fc',\n                        'email' =&gt; 'anonymous@example.com'\n                    ]);\n    }\n\n    \/\/ \u30af\u30ed\u30fc\u30b8\u30e3\u3092\u4f7f\u7528\u3057\u305f\u52d5\u7684\u306a\u30c7\u30d5\u30a9\u30eb\u30c8\u5024\n    public function category()\n    {\n        return $this-&gt;belongsTo(Category::class)\n                    -&gt;withDefault(function ($category, $article) {\n                        $category-&gt;name = '\u30ab\u30c6\u30b4\u30ea\u30fc\u672a\u8a2d\u5b9a';\n                        $category-&gt;slug = 'uncategorized';\n                        $category-&gt;setRelation('articles', collect([$article]));\n                    });\n    }\n}\n\n\/\/ \u4f7f\u7528\u4f8b\n$article = Article::find(1);\n\/\/ \u8457\u8005\u304c\u524a\u9664\u3055\u308c\u3066\u3044\u3066\u3082\u5b89\u5168\u306b\u30a2\u30af\u30bb\u30b9\u53ef\u80fd\necho $article-&gt;author-&gt;name;  \/\/ \u51fa\u529b: \u533f\u540d\u30e6\u30fc\u30b6\u30fc<\/pre>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"i-24\">Eager\u30ed\u30fc\u30c7\u30a3\u30f3\u30b0\u306b\u3088\u308b\u30d1\u30d5\u30a9\u30fc\u30de\u30f3\u30b9\u6700\u9069\u5316<\/h3>\n\n\n\n<h4 class=\"wp-block-heading\" id=\"i-25\">\u57fa\u672c\u7684\u306aEager\u30ed\u30fc\u30c7\u30a3\u30f3\u30b0<\/h4>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">\/\/ N+1\u554f\u984c\u3092\u56de\u907f\u3059\u308b\u57fa\u672c\u7684\u306aEager\u30ed\u30fc\u30c7\u30a3\u30f3\u30b0\n$comments = Comment::with('post')-&gt;get();\n\n\/\/ \u8907\u6570\u306e\u30ea\u30ec\u30fc\u30b7\u30e7\u30f3\u3092Eager\u30ed\u30fc\u30c9\n$comments = Comment::with(['post', 'user'])-&gt;get();\n\n\/\/ \u30cd\u30b9\u30c8\u3055\u308c\u305f\u30ea\u30ec\u30fc\u30b7\u30e7\u30f3\u306eEager\u30ed\u30fc\u30c9\n$comments = Comment::with('post.author')-&gt;get();<\/pre>\n\n\n\n<h4 class=\"wp-block-heading\" id=\"i-26\">\u6761\u4ef6\u4ed8\u304dEager\u30ed\u30fc\u30c7\u30a3\u30f3\u30b0<\/h4>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">class Post extends Model\n{\n    public function comments()\n    {\n        \/\/ \u7279\u5b9a\u306e\u6761\u4ef6\u3092\u6e80\u305f\u3059\u30b3\u30e1\u30f3\u30c8\u306e\u307f\u3092Eager\u30ed\u30fc\u30c9\n        return $this-&gt;hasMany(Comment::class)\n                    -&gt;where('is_approved', true);\n    }\n\n    public function latestComment()\n    {\n        return $this-&gt;belongsTo(Comment::class)\n                    -&gt;latest()\n                    -&gt;limit(1);\n    }\n}\n\n\/\/ \u4f7f\u7528\u4f8b\uff1a\u627f\u8a8d\u6e08\u307f\u30b3\u30e1\u30f3\u30c8\u306e\u307f\u3092Eager\u30ed\u30fc\u30c9\n$posts = Post::with(['comments' =&gt; function ($query) {\n    $query-&gt;where('is_approved', true)\n          -&gt;orderBy('created_at', 'desc');\n}])-&gt;get();<\/pre>\n\n\n\n<h4 class=\"wp-block-heading\" id=\"i-27\">\u30d1\u30d5\u30a9\u30fc\u30de\u30f3\u30b9\u6700\u9069\u5316\u306e\u305f\u3081\u306e\u30c6\u30af\u30cb\u30c3\u30af<\/h4>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">class Article extends Model\n{\n    \/\/ \u3088\u304f\u4f7f\u7528\u3059\u308b\u30ea\u30ec\u30fc\u30b7\u30e7\u30f3\u3092\u81ea\u52d5\u7684\u306bEager\u30ed\u30fc\u30c9\n    protected $with = ['author', 'category'];\n\n    \/\/ \u7279\u5b9a\u306e\u30ab\u30e9\u30e0\u306e\u307f\u3092\u53d6\u5f97\n    public function author()\n    {\n        return $this-&gt;belongsTo(User::class)\n                    -&gt;select(['id', 'name', 'email']);\n    }\n}\n\n\/\/ \u5fc5\u8981\u306a\u30ab\u30e9\u30e0\u306e\u307f\u3092\u53d6\u5f97\u3059\u308b\u4f8b\n$articles = Article::with(['author:id,name', 'category:id,name'])\n                  -&gt;select(['id', 'title', 'author_id', 'category_id'])\n                  -&gt;get();<\/pre>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"i-28\">\u30bd\u30d5\u30c8\u30c7\u30ea\u30fc\u30c8\u6642\u306e\u6319\u52d5\u5236\u5fa1\u65b9\u6cd5<\/h3>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">class Comment extends Model\n{\n    public function post()\n    {\n        \/\/ \u30bd\u30d5\u30c8\u30c7\u30ea\u30fc\u30c8\u3055\u308c\u305f\u6295\u7a3f\u3082\u53d6\u5f97\n        return $this-&gt;belongsTo(Post::class)\n                    -&gt;withTrashed();\n    }\n\n    public function author()\n    {\n        \/\/ \u30bd\u30d5\u30c8\u30c7\u30ea\u30fc\u30c8\u3055\u308c\u305f\u30e6\u30fc\u30b6\u30fc\u306e\u307f\u53d6\u5f97\n        return $this-&gt;belongsTo(User::class)\n                    -&gt;onlyTrashed();\n    }\n\n    \/\/ \u8907\u96d1\u306a\u6761\u4ef6\u3067\u306e\u30bd\u30d5\u30c8\u30c7\u30ea\u30fc\u30c8\u5bfe\u5fdc\n    public function category()\n    {\n        return $this-&gt;belongsTo(Category::class)\n                    -&gt;withTrashed()\n                    -&gt;withDefault(function ($category) {\n                        if ($category-&gt;trashed()) {\n                            $category-&gt;name = '\u524a\u9664\u3055\u308c\u305f\u30ab\u30c6\u30b4\u30ea\u30fc';\n                        } else {\n                            $category-&gt;name = '\u30ab\u30c6\u30b4\u30ea\u30fc\u306a\u3057';\n                        }\n                    });\n    }\n}\n\n\/\/ \u4f7f\u7528\u4f8b\n$comment = Comment::find(1);\n$trashedPost = $comment-&gt;post;  \/\/ \u30bd\u30d5\u30c8\u30c7\u30ea\u30fc\u30c8\u3055\u308c\u305f\u6295\u7a3f\u3082\u53d6\u5f97\u53ef\u80fd<\/pre>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"i-29\">\u9ad8\u5ea6\u306a\u30af\u30a8\u30ea\u30c6\u30af\u30cb\u30c3\u30af<\/h3>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">class Order extends Model\n{\n    public function customer()\n    {\n        return $this-&gt;belongsTo(Customer::class)\n                    -&gt;where(function ($query) {\n                        \/\/ \u8907\u96d1\u306a\u6761\u4ef6\u3092\u8a2d\u5b9a\n                        $query-&gt;where('status', 'active')\n                              -&gt;orWhere(function ($q) {\n                                  $q-&gt;where('status', 'pending')\n                                    -&gt;where('created_at', '&gt;', now()-&gt;subDays(30));\n                              });\n                    });\n    }\n\n    \/\/ \u95a2\u9023\u30c6\u30fc\u30d6\u30eb\u3092\u7d50\u5408\u3057\u305f\u30b9\u30b3\u30fc\u30d7\n    public function scopeWithActiveCustomer($query)\n    {\n        return $query-&gt;join('customers', 'orders.customer_id', '=', 'customers.id')\n                     -&gt;where('customers.status', 'active')\n                     -&gt;select('orders.*');\n    }\n}\n\n\/\/ \u9ad8\u5ea6\u306a\u30af\u30a8\u30ea\u306e\u4f7f\u7528\u4f8b\n$orders = Order::withActiveCustomer()\n               -&gt;with(['customer' =&gt; function ($query) {\n                   $query-&gt;withCount('orders')\n                         -&gt;with('latestOrder');\n               }])\n               -&gt;paginate(20);<\/pre>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"i-30\">\u30d1\u30d5\u30a9\u30fc\u30de\u30f3\u30b9\u6700\u9069\u5316\u306e\u30d9\u30b9\u30c8\u30d7\u30e9\u30af\u30c6\u30a3\u30b9<\/h3>\n\n\n\n<ol class=\"wp-block-list\">\n<li>\u30a4\u30f3\u30c7\u30c3\u30af\u30b9\u306e\u9069\u5207\u306a\u8a2d\u5b9a<\/li>\n<\/ol>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">\/\/ \u30de\u30a4\u30b0\u30ec\u30fc\u30b7\u30e7\u30f3\u3067\u306e\u30a4\u30f3\u30c7\u30c3\u30af\u30b9\u8a2d\u5b9a\nSchema::create('posts', function (Blueprint $table) {\n    $table-&gt;id();\n    $table-&gt;foreignId('author_id')\n          -&gt;constrained('users')\n          -&gt;index();  \/\/ \u5916\u90e8\u30ad\u30fc\u306b\u30a4\u30f3\u30c7\u30c3\u30af\u30b9\u3092\u8ffd\u52a0\n    $table-&gt;string('title');\n    $table-&gt;timestamps();\n\n    \/\/ \u8907\u5408\u30a4\u30f3\u30c7\u30c3\u30af\u30b9\u306e\u4f5c\u6210\n    $table-&gt;index(['author_id', 'created_at']);\n});<\/pre>\n\n\n\n<ol start=\"2\" class=\"wp-block-list\">\n<li>\u30ad\u30e3\u30c3\u30b7\u30e5\u306e\u6d3b\u7528<\/li>\n<\/ol>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">class Post extends Model\n{\n    public function author()\n    {\n        return $this-&gt;belongsTo(User::class)\n                    -&gt;remember(60);  \/\/ 60\u79d2\u9593\u30ad\u30e3\u30c3\u30b7\u30e5\n    }\n}\n\n\/\/ \u30ad\u30e3\u30c3\u30b7\u30e5\u30bf\u30b0\u306e\u4f7f\u7528\u4f8b\n$posts = Cache::tags(['posts', 'authors'])-&gt;remember('latest_posts', 3600, function () {\n    return Post::with('author')-&gt;latest()-&gt;take(10)-&gt;get();\n});<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u3053\u308c\u3089\u306e\u9ad8\u5ea6\u306a\u30c6\u30af\u30cb\u30c3\u30af\u3092\u9069\u5207\u306b\u7d44\u307f\u5408\u308f\u305b\u308b\u3053\u3068\u3067\u3001\u30d1\u30d5\u30a9\u30fc\u30de\u30f3\u30b9\u3068\u4fdd\u5b88\u6027\u3092\u4e21\u7acb\u3057\u305f\u30a2\u30d7\u30ea\u30b1\u30fc\u30b7\u30e7\u30f3\u3092\u69cb\u7bc9\u3067\u304d\u307e\u3059\u3002\u305f\u3060\u3057\u3001\u904e\u5ea6\u306a\u6700\u9069\u5316\u306f\u907f\u3051\u3001\u5fc5\u8981\u306b\u5fdc\u3058\u3066\u9069\u5207\u306a\u624b\u6cd5\u3092\u9078\u629e\u3059\u308b\u3053\u3068\u304c\u91cd\u8981\u3067\u3059\u3002<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"i-31\">belongsTo\u30ea\u30ec\u30fc\u30b7\u30e7\u30f3\u306e\u30c8\u30e9\u30d6\u30eb\u30b7\u30e5\u30fc\u30c6\u30a3\u30f3\u30b0<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"i-32\">\u3088\u304f\u3042\u308b\u30a8\u30e9\u30fc\u3068\u89e3\u6c7a\u65b9\u6cd5<\/h3>\n\n\n\n<h4 class=\"wp-block-heading\" id=\"i-33\">1. Undefined relationship \u30a8\u30e9\u30fc<\/h4>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">\/\/ \u30a8\u30e9\u30fc\u306e\u4f8b\nSQLSTATE[42S22]: Column not found: 1054 Unknown column 'user_id' in 'where clause'\n\n\/\/ \u539f\u56e01: \u5916\u90e8\u30ad\u30fc\u304c\u6b63\u3057\u304f\u5b9a\u7fa9\u3055\u308c\u3066\u3044\u306a\u3044\nclass Post extends Model\n{\n    \/\/ \u8aa4\u3063\u305f\u5b9f\u88c5\n    public function author()\n    {\n        return $this-&gt;belongsTo(User::class);  \/\/ \u30c7\u30d5\u30a9\u30eb\u30c8\u3067user_id\u3092\u63a2\u3057\u306b\u884c\u304f\n    }\n\n    \/\/ \u6b63\u3057\u3044\u5b9f\u88c5\n    public function author()\n    {\n        return $this-&gt;belongsTo(User::class, 'author_id');  \/\/ \u5b9f\u969b\u306e\u30ab\u30e9\u30e0\u540d\u3092\u6307\u5b9a\n    }\n}\n\n\/\/ \u539f\u56e02: \u30de\u30a4\u30b0\u30ec\u30fc\u30b7\u30e7\u30f3\u306e\u4e0d\u5099\n\/\/ \u8aa4\u3063\u305f\u5b9f\u88c5\nSchema::create('posts', function (Blueprint $table) {\n    $table-&gt;id();\n    $table-&gt;string('author_id');  \/\/ \u578b\u304c\u4e0d\u9069\u5207\n});\n\n\/\/ \u6b63\u3057\u3044\u5b9f\u88c5\nSchema::create('posts', function (Blueprint $table) {\n    $table-&gt;id();\n    $table-&gt;foreignId('author_id')-&gt;constrained('users');\n});<\/pre>\n\n\n\n<h4 class=\"wp-block-heading\" id=\"i-34\">2. \u30ea\u30ec\u30fc\u30b7\u30e7\u30f3\u5148\u304c\u898b\u3064\u304b\u3089\u306a\u3044\u5834\u5408\u306e\u30a8\u30e9\u30fc<\/h4>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">\/\/ \u30a8\u30e9\u30fc\u306e\u4f8b\nTrying to get property of non-object\n\n\/\/ \u89e3\u6c7a\u65b9\u6cd51: null\u5b89\u5168\u306a\u30a2\u30af\u30bb\u30b9\nclass Post extends Model\n{\n    public function author()\n    {\n        return $this-&gt;belongsTo(User::class)\n                    -&gt;withDefault([\n                        'name' =&gt; '\u4e0d\u660e\u306a\u30e6\u30fc\u30b6\u30fc'\n                    ]);\n    }\n}\n\n\/\/ \u89e3\u6c7a\u65b9\u6cd52: null\u5224\u5b9a\u3092\u884c\u3046\n$authorName = $post-&gt;author?-&gt;name ?? '\u4e0d\u660e\u306a\u30e6\u30fc\u30b6\u30fc';\n\n\/\/ \u89e3\u6c7a\u65b9\u6cd53: \u30ea\u30ec\u30fc\u30b7\u30e7\u30f3\u5b58\u5728\u78ba\u8a8d\nif ($post-&gt;author()-&gt;exists()) {\n    \/\/ \u30ea\u30ec\u30fc\u30b7\u30e7\u30f3\u5148\u304c\u5b58\u5728\u3059\u308b\u5834\u5408\u306e\u51e6\u7406\n}<\/pre>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"i-35\">N+1\u554f\u984c\u306e\u56de\u907f\u65b9\u6cd5<\/h3>\n\n\n\n<h4 class=\"wp-block-heading\" id=\"i-36\">1. N+1\u554f\u984c\u306e\u691c\u51fa<\/h4>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">\/\/ N+1\u554f\u984c\u3092\u5f15\u304d\u8d77\u3053\u3059\u30b3\u30fc\u30c9\n$posts = Post::all();\nforeach ($posts as $post) {\n    echo $post-&gt;author-&gt;name;  \/\/ \u5404\u6295\u7a3f\u3054\u3068\u306bSQL\u30af\u30a8\u30ea\u304c\u767a\u884c\u3055\u308c\u308b\n}\n\n\/\/ \u30af\u30a8\u30ea\u30ed\u30b0\u3067\u306e\u78ba\u8a8d\u65b9\u6cd5\n\\DB::enableQueryLog();\n\/\/ \u30b3\u30fc\u30c9\u306e\u5b9f\u884c\ndump(\\DB::getQueryLog());  \/\/ \u767a\u884c\u3055\u308c\u305fSQL\u30af\u30a8\u30ea\u306e\u78ba\u8a8d<\/pre>\n\n\n\n<h4 class=\"wp-block-heading\" id=\"i-37\">2. Eager\u30ed\u30fc\u30c7\u30a3\u30f3\u30b0\u306b\u3088\u308b\u89e3\u6c7a<\/h4>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">\/\/ \u57fa\u672c\u7684\u306aEager\u30ed\u30fc\u30c7\u30a3\u30f3\u30b0\n$posts = Post::with('author')-&gt;get();\n\n\/\/ \u8907\u6570\u306e\u30ea\u30ec\u30fc\u30b7\u30e7\u30f3\u306eEager\u30ed\u30fc\u30c7\u30a3\u30f3\u30b0\n$posts = Post::with(['author', 'category'])-&gt;get();\n\n\/\/ \u6761\u4ef6\u4ed8\u304dEager\u30ed\u30fc\u30c7\u30a3\u30f3\u30b0\n$posts = Post::with(['author' =&gt; function ($query) {\n    $query-&gt;select('id', 'name', 'email')\n          -&gt;where('status', 'active');\n}])-&gt;get();\n\n\/\/ \u30cd\u30b9\u30c8\u3055\u308c\u305f\u30ea\u30ec\u30fc\u30b7\u30e7\u30f3\u306eEager\u30ed\u30fc\u30c7\u30a3\u30f3\u30b0\n$posts = Post::with('author.profile')-&gt;get();<\/pre>\n\n\n\n<h4 class=\"wp-block-heading\" id=\"i-38\">3. LazyEager\u30ed\u30fc\u30c7\u30a3\u30f3\u30b0\u306e\u6d3b\u7528<\/h4>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">\/\/ \u3059\u3067\u306b\u53d6\u5f97\u3057\u305f\u30e2\u30c7\u30eb\u306b\u5f8c\u304b\u3089Eager\u30ed\u30fc\u30c9\u3059\u308b\n$posts = Post::all();\nif ($needsAuthorInfo) {\n    $posts-&gt;load('author');\n}\n\n\/\/ \u6761\u4ef6\u4ed8\u304dLazyEager\u30ed\u30fc\u30c7\u30a3\u30f3\u30b0\n$posts-&gt;load(['author' =&gt; function ($query) {\n    $query-&gt;where('role', 'admin');\n}]);<\/pre>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"i-39\">\u5faa\u74b0\u53c2\u7167\u306e\u9632\u6b62\u7b56<\/h3>\n\n\n\n<h4 class=\"wp-block-heading\" id=\"i-40\">1. \u5faa\u74b0\u53c2\u7167\u304c\u767a\u751f\u3059\u308b\u4f8b<\/h4>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">\/\/ \u5faa\u74b0\u53c2\u7167\u3092\u5f15\u304d\u8d77\u3053\u3059\u5b9f\u88c5\nclass User extends Model\n{\n    public function latestPost()\n    {\n        return $this-&gt;hasOne(Post::class)-&gt;latest();\n    }\n}\n\nclass Post extends Model\n{\n    public function user()\n    {\n        return $this-&gt;belongsTo(User::class)-&gt;with('latestPost');\n    }\n}\n\n\/\/ \u7121\u9650\u30eb\u30fc\u30d7\u304c\u767a\u751f\u3059\u308b\u53ef\u80fd\u6027\u306e\u3042\u308b\u547c\u3073\u51fa\u3057\n$user = User::with('latestPost.user.latestPost')-&gt;first();<\/pre>\n\n\n\n<h4 class=\"wp-block-heading\" id=\"i-41\">2. \u5faa\u74b0\u53c2\u7167\u306e\u89e3\u6c7a\u65b9\u6cd5<\/h4>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">\/\/ \u65b9\u6cd51: \u30ea\u30ec\u30fc\u30b7\u30e7\u30f3\u306e\u6df1\u3055\u3092\u5236\u9650\nclass User extends Model\n{\n    public function latestPost()\n    {\n        return $this-&gt;hasOne(Post::class)\n                    -&gt;latest()\n                    -&gt;without('user');  \/\/ user\u30ea\u30ec\u30fc\u30b7\u30e7\u30f3\u3092\u9664\u5916\n    }\n}\n\n\/\/ \u65b9\u6cd52: Eager\u30ed\u30fc\u30c7\u30a3\u30f3\u30b0\u6642\u306b\u5236\u9650\u3092\u8a2d\u5b9a\n$user = User::with(['latestPost' =&gt; function ($query) {\n    $query-&gt;without('user');\n}])-&gt;first();\n\n\/\/ \u65b9\u6cd53: \u30ea\u30ec\u30fc\u30b7\u30e7\u30f3\u5b9a\u7fa9\u6642\u306b\u5236\u9650\u3092\u8a2d\u5b9a\nclass Post extends Model\n{\n    protected $with = ['category'];  \/\/ \u81ea\u52d5\u7684\u306bEager\u30ed\u30fc\u30c9\u3059\u308b\u30ea\u30ec\u30fc\u30b7\u30e7\u30f3\n    protected $without = ['user'];   \/\/ \u81ea\u52d5\u7684\u306bEager\u30ed\u30fc\u30c9\u304b\u3089\u9664\u5916\u3059\u308b\u30ea\u30ec\u30fc\u30b7\u30e7\u30f3\n}<\/pre>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"i-42\">\u30d1\u30d5\u30a9\u30fc\u30de\u30f3\u30b9\u95a2\u9023\u306e\u30c8\u30e9\u30d6\u30eb\u30b7\u30e5\u30fc\u30c6\u30a3\u30f3\u30b0<\/h3>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">\/\/ \u30e1\u30e2\u30ea\u4f7f\u7528\u91cf\u306e\u6700\u9069\u5316\nclass Post extends Model\n{\n    \/\/ \u5fc5\u8981\u306a\u30ab\u30e9\u30e0\u306e\u307f\u3092\u53d6\u5f97\n    public function author()\n    {\n        return $this-&gt;belongsTo(User::class)\n                    -&gt;select(['id', 'name', 'email']);\n    }\n\n    \/\/ \u30c1\u30e3\u30f3\u30af\u51e6\u7406\u3067\u306e\u5927\u91cf\u30c7\u30fc\u30bf\u51e6\u7406\n    public static function updateAuthors()\n    {\n        static::with('author')\n              -&gt;orderBy('id')\n              -&gt;chunk(100, function ($posts) {\n                  foreach ($posts as $post) {\n                      \/\/ \u51e6\u7406\n                  }\n              });\n    }\n}\n\n\/\/ \u30af\u30a8\u30ea\u306e\u6700\u9069\u5316\nclass Comment extends Model\n{\n    public function post()\n    {\n        return $this-&gt;belongsTo(Post::class)\n                    -&gt;whereHas('author', function ($query) {\n                        $query-&gt;where('status', 'active');\n                    })\n                    -&gt;latest();\n    }\n}<\/pre>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"i-43\">\u30c7\u30d0\u30c3\u30b0\u3068\u30c8\u30e9\u30d6\u30eb\u30b7\u30e5\u30fc\u30c6\u30a3\u30f3\u30b0\u306e\u30d9\u30b9\u30c8\u30d7\u30e9\u30af\u30c6\u30a3\u30b9<\/h3>\n\n\n\n<ol class=\"wp-block-list\">\n<li>\u30af\u30a8\u30ea\u30ed\u30b0\u306e\u6d3b\u7528<\/li>\n<\/ol>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">\/\/ \u958b\u767a\u74b0\u5883\u3067\u306e\u30af\u30a8\u30ea\u30ed\u30b0\u78ba\u8a8d\n\\DB::enableQueryLog();\n\/\/ \u30b3\u30fc\u30c9\u306e\u5b9f\u884c\n$posts = Post::with('author')-&gt;get();\n\/\/ \u30ed\u30b0\u306e\u78ba\u8a8d\ndd(\\DB::getQueryLog());<\/pre>\n\n\n\n<ol start=\"2\" class=\"wp-block-list\">\n<li>\u30ea\u30ec\u30fc\u30b7\u30e7\u30f3\u306e\u5b58\u5728\u78ba\u8a8d<\/li>\n<\/ol>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">\/\/ \u30c7\u30fc\u30bf\u6574\u5408\u6027\u306e\u30c1\u30a7\u30c3\u30af\n$posts = Post::whereDoesntHave('author')\n            -&gt;orWhereHas('author', function ($query) {\n                $query-&gt;whereNull('email');\n            })\n            -&gt;get();<\/pre>\n\n\n\n<ol start=\"3\" class=\"wp-block-list\">\n<li>\u30e2\u30c7\u30eb\u30a4\u30d9\u30f3\u30c8\u3067\u306e\u30c7\u30d0\u30c3\u30b0<\/li>\n<\/ol>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">class Post extends Model\n{\n    protected static function booted()\n    {\n        static::retrieved(function ($post) {\n            \\Log::info('Post retrieved:', [\n                'id' =&gt; $post-&gt;id,\n                'has_author' =&gt; $post-&gt;author()-&gt;exists()\n            ]);\n        });\n    }\n}<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u3053\u308c\u3089\u306e\u30c8\u30e9\u30d6\u30eb\u30b7\u30e5\u30fc\u30c6\u30a3\u30f3\u30b0\u624b\u6cd5\u3092\u7406\u89e3\u3057\u3001\u9069\u5207\u306b\u9069\u7528\u3059\u308b\u3053\u3068\u3067\u3001\u591a\u304f\u306e\u4e00\u822c\u7684\u306a\u554f\u984c\u3092\u52b9\u7387\u7684\u306b\u89e3\u6c7a\u3067\u304d\u307e\u3059\u3002\u307e\u305f\u3001\u4e8b\u524d\u306b\u554f\u984c\u3092\u4e88\u9632\u3059\u308b\u3053\u3068\u3082\u53ef\u80fd\u306b\u306a\u308a\u307e\u3059\u3002<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"i-44\">\u307e\u3068\u3081\uff1a\u52b9\u679c\u7684\u306abelongsTo\u30ea\u30ec\u30fc\u30b7\u30e7\u30f3\u8a2d\u8a08\u6642\u306e\u30dd\u30a4\u30f3\u30c8<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"i-45\">belongsTo\u306e\u91cd\u8981\u306a\u6ce8\u610f\u70b9<\/h3>\n\n\n\n<h4 class=\"wp-block-heading\" id=\"i-46\">1. \u8a2d\u8a08\u6bb5\u968e\u3067\u306e\u8003\u616e\u4e8b\u9805<\/h4>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">\/\/ \u9069\u5207\u306a\u5916\u90e8\u30ad\u30fc\u5236\u7d04\u306e\u8a2d\u5b9a\nSchema::create('posts', function (Blueprint $table) {\n    $table-&gt;id();\n    $table-&gt;foreignId('author_id')\n          -&gt;constrained('users')\n          -&gt;onDelete('cascade')  \/\/ \u53c2\u7167\u6574\u5408\u6027\u306e\u5236\u5fa1\n          -&gt;index();            \/\/ \u30d1\u30d5\u30a9\u30fc\u30de\u30f3\u30b9\u306e\u8003\u616e\n});\n\n\/\/ \u30ea\u30ec\u30fc\u30b7\u30e7\u30f3\u5b9a\u7fa9\u306e\u30d9\u30b9\u30c8\u30d7\u30e9\u30af\u30c6\u30a3\u30b9\nclass Post extends Model\n{\n    public function author()\n    {\n        return $this-&gt;belongsTo(User::class)\n                    -&gt;withDefault()      \/\/ null\u5bfe\u7b56\n                    -&gt;withTrashed()      \/\/ \u8ad6\u7406\u524a\u9664\u5bfe\u5fdc\n                    -&gt;select(['id', 'name']); \/\/ \u5fc5\u8981\u6700\u5c0f\u9650\u306e\u30ab\u30e9\u30e0\n    }\n}<\/pre>\n\n\n\n<h4 class=\"wp-block-heading\" id=\"i-47\">2. \u547d\u540d\u898f\u5247\u3068\u30b3\u30fc\u30c7\u30a3\u30f3\u30b0\u898f\u7d04<\/h4>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">\/\/ \u63a8\u5968\u3055\u308c\u308b\u547d\u540d\u30d1\u30bf\u30fc\u30f3\nclass Order extends Model\n{\n    \/\/ \u5358\u6570\u5f62\u3067\u3001\u95a2\u4fc2\u6027\u3092\u660e\u78ba\u306b\u8868\u73fe\n    public function customer()\n    {\n        return $this-&gt;belongsTo(Customer::class);\n    }\n\n    \/\/ \u540c\u3058\u30e2\u30c7\u30eb\u3078\u306e\u8907\u6570\u306e\u30ea\u30ec\u30fc\u30b7\u30e7\u30f3\n    public function assignedEmployee()\n    {\n        return $this-&gt;belongsTo(User::class, 'assigned_to');\n    }\n\n    public function createdByEmployee()\n    {\n        return $this-&gt;belongsTo(User::class, 'created_by');\n    }\n}<\/pre>\n\n\n\n<h4 class=\"wp-block-heading\" id=\"i-48\">3. \u30bb\u30ad\u30e5\u30ea\u30c6\u30a3\u3068\u30d0\u30ea\u30c7\u30fc\u30b7\u30e7\u30f3<\/h4>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">\/\/ \u30ea\u30ec\u30fc\u30b7\u30e7\u30f3\u5148\u306e\u5b58\u5728\u78ba\u8a8d\nclass OrderController extends Controller\n{\n    public function store(Request $request)\n    {\n        $validated = $request-&gt;validate([\n            'customer_id' =&gt; [\n                'required',\n                Rule::exists('customers', 'id')-&gt;where(function ($query) {\n                    $query-&gt;where('status', 'active');\n                }),\n            ],\n        ]);\n\n        $order = Order::create($validated);\n    }\n}<\/pre>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"i-49\">\u30d1\u30d5\u30a9\u30fc\u30de\u30f3\u30b9\u3068\u30e1\u30f3\u30c6\u30ca\u30f3\u30b9\u6027\u3092\u4e21\u7acb\u3059\u308b\u305f\u3081\u306e\u30d2\u30f3\u30c8<\/h3>\n\n\n\n<h4 class=\"wp-block-heading\" id=\"i-50\">1. \u30af\u30a8\u30ea\u306e\u6700\u9069\u5316<\/h4>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">\/\/ Eager\u30ed\u30fc\u30c7\u30a3\u30f3\u30b0\u306e\u52b9\u679c\u7684\u306a\u4f7f\u7528\nclass OrderController extends Controller\n{\n    public function index()\n    {\n        \/\/ \u5fc5\u8981\u306a\u30ea\u30ec\u30fc\u30b7\u30e7\u30f3\u306e\u307f\u3092\u30ed\u30fc\u30c9\n        return Order::with(['customer:id,name', 'products:id,name,price'])\n                   -&gt;latest()\n                   -&gt;paginate(20);\n    }\n\n    public function show(Order $order)\n    {\n        \/\/ \u5fc5\u8981\u306b\u5fdc\u3058\u3066\u8ffd\u52a0\u306e\u30ea\u30ec\u30fc\u30b7\u30e7\u30f3\u3092\u30ed\u30fc\u30c9\n        $order-&gt;load([\n            'customer.preferences',\n            'products.category'\n        ]);\n\n        return $order;\n    }\n}<\/pre>\n\n\n\n<h4 class=\"wp-block-heading\" id=\"i-51\">2. \u30ad\u30e3\u30c3\u30b7\u30e5\u6226\u7565<\/h4>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">class Order extends Model\n{\n    public function customer()\n    {\n        \/\/ \u983b\u7e41\u306b\u5909\u66f4\u3055\u308c\u306a\u3044\u30c7\u30fc\u30bf\u306e\u30ad\u30e3\u30c3\u30b7\u30e5\n        return $this-&gt;belongsTo(Customer::class)\n                    -&gt;remember(now()-&gt;addHours(24));\n    }\n\n    \/\/ \u30ad\u30e3\u30c3\u30b7\u30e5\u306e\u81ea\u52d5\u30af\u30ea\u30a2\n    protected static function booted()\n    {\n        static::updated(function ($order) {\n            Cache::tags(['orders', 'customers'])-&gt;flush();\n        });\n    }\n}<\/pre>\n\n\n\n<h4 class=\"wp-block-heading\" id=\"i-52\">3. \u30e1\u30f3\u30c6\u30ca\u30f3\u30b9\u6027\u306e\u5411\u4e0a<\/h4>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">class Post extends Model\n{\n    \/\/ \u30c7\u30d5\u30a9\u30eb\u30c8\u5024\u306e\u4e00\u5143\u7ba1\u7406\n    protected $attributes = [\n        'status' =&gt; 'draft',\n    ];\n\n    \/\/ \u30ea\u30ec\u30fc\u30b7\u30e7\u30f3\u306e\u4e00\u5143\u7ba1\u7406\n    protected $with = ['author'];\n    protected $withCount = ['comments'];\n\n    \/\/ \u30b9\u30b3\u30fc\u30d7\u306e\u6d3b\u7528\n    public function scopeWithActiveAuthor($query)\n    {\n        return $query-&gt;whereHas('author', function ($q) {\n            $q-&gt;where('status', 'active');\n        });\n    }\n}<\/pre>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"i-53\">\u5b9f\u88c5\u6642\u306e\u6700\u7d42\u30c1\u30a7\u30c3\u30af\u30ea\u30b9\u30c8<\/h3>\n\n\n\n<ol class=\"wp-block-list\">\n<li>\u30c7\u30fc\u30bf\u30d9\u30fc\u30b9\u8a2d\u8a08<\/li>\n<\/ol>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u9069\u5207\u306a\u5916\u90e8\u30ad\u30fc\u5236\u7d04\u304c\u8a2d\u5b9a\u3055\u308c\u3066\u3044\u308b\u304b<\/li>\n\n\n\n<li>\u30a4\u30f3\u30c7\u30c3\u30af\u30b9\u304c\u5fc5\u8981\u306a\u7b87\u6240\u306b\u8a2d\u5b9a\u3055\u308c\u3066\u3044\u308b\u304b<\/li>\n\n\n\n<li>\u30ab\u30e9\u30e0\u306e\u578b\u304c\u9069\u5207\u304b<\/li>\n<\/ul>\n\n\n\n<ol class=\"wp-block-list\">\n<li>\u30e2\u30c7\u30eb\u8a2d\u8a08<\/li>\n<\/ol>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u30ea\u30ec\u30fc\u30b7\u30e7\u30f3\u540d\u306f\u610f\u56f3\u3092\u9069\u5207\u306b\u8868\u73fe\u3057\u3066\u3044\u308b\u304b<\/li>\n\n\n\n<li>\u30c7\u30d5\u30a9\u30eb\u30c8\u5024\u306f\u9069\u5207\u306b\u8a2d\u5b9a\u3055\u308c\u3066\u3044\u308b\u304b<\/li>\n\n\n\n<li>Eager\u30ed\u30fc\u30c7\u30a3\u30f3\u30b0\u306e\u8a2d\u5b9a\u306f\u6700\u9069\u304b<\/li>\n<\/ul>\n\n\n\n<ol class=\"wp-block-list\">\n<li>\u30d1\u30d5\u30a9\u30fc\u30de\u30f3\u30b9<\/li>\n<\/ol>\n\n\n\n<ul class=\"wp-block-list\">\n<li>N+1\u554f\u984c\u306f\u56de\u907f\u3055\u308c\u3066\u3044\u308b\u304b<\/li>\n\n\n\n<li>\u5fc5\u8981\u6700\u5c0f\u9650\u306e\u30ab\u30e9\u30e0\u306e\u307f\u3092\u53d6\u5f97\u3057\u3066\u3044\u308b\u304b<\/li>\n\n\n\n<li>\u30ad\u30e3\u30c3\u30b7\u30e5\u6226\u7565\u306f\u9069\u5207\u304b<\/li>\n<\/ul>\n\n\n\n<ol class=\"wp-block-list\">\n<li>\u30bb\u30ad\u30e5\u30ea\u30c6\u30a3<\/li>\n<\/ol>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u30d0\u30ea\u30c7\u30fc\u30b7\u30e7\u30f3\u306f\u9069\u5207\u306b\u5b9f\u88c5\u3055\u308c\u3066\u3044\u308b\u304b<\/li>\n\n\n\n<li>SQL\u30a4\u30f3\u30b8\u30a7\u30af\u30b7\u30e7\u30f3\u5bfe\u7b56\u306f\u5341\u5206\u304b<\/li>\n\n\n\n<li>\u8a8d\u53ef\u306e\u78ba\u8a8d\u306f\u5b9f\u88c5\u3055\u308c\u3066\u3044\u308b\u304b<\/li>\n<\/ul>\n\n\n\n<ol class=\"wp-block-list\">\n<li>\u30e1\u30f3\u30c6\u30ca\u30f3\u30b9\u6027<\/li>\n<\/ol>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u30b3\u30fc\u30c9\u306f\u9069\u5207\u306b\u30c9\u30ad\u30e5\u30e1\u30f3\u30c8\u5316\u3055\u308c\u3066\u3044\u308b\u304b<\/li>\n\n\n\n<li>\u547d\u540d\u898f\u5247\u306f\u4e00\u8cab\u3057\u3066\u3044\u308b\u304b<\/li>\n\n\n\n<li>\u30c6\u30b9\u30c8\u306f\u5341\u5206\u306b\u66f8\u304b\u308c\u3066\u3044\u308b\u304b<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"i-54\">\u307e\u3068\u3081<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">belongsTo\u30ea\u30ec\u30fc\u30b7\u30e7\u30f3\u306f\u3001Laravel\u30a2\u30d7\u30ea\u30b1\u30fc\u30b7\u30e7\u30f3\u306b\u304a\u3051\u308b\u57fa\u672c\u7684\u304b\u3064\u91cd\u8981\u306a\u6a5f\u80fd\u3067\u3059\u3002\u9069\u5207\u306b\u5b9f\u88c5\u3059\u308b\u3053\u3068\u3067\u3001\u4ee5\u4e0b\u306e\u30e1\u30ea\u30c3\u30c8\u304c\u5f97\u3089\u308c\u307e\u3059\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u30c7\u30fc\u30bf\u306e\u6574\u5408\u6027\u306e\u78ba\u4fdd<\/li>\n\n\n\n<li>\u30b3\u30fc\u30c9\u306e\u53ef\u8aad\u6027\u3068\u4fdd\u5b88\u6027\u306e\u5411\u4e0a<\/li>\n\n\n\n<li>\u30a2\u30d7\u30ea\u30b1\u30fc\u30b7\u30e7\u30f3\u306e\u30d1\u30d5\u30a9\u30fc\u30de\u30f3\u30b9\u6700\u9069\u5316<\/li>\n\n\n\n<li>\u30bb\u30ad\u30e5\u30ea\u30c6\u30a3\u306e\u5f37\u5316<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">\u672c\u8a18\u4e8b\u3067\u7d39\u4ecb\u3057\u305f\u5b9f\u88c5\u4f8b\u3084\u30d9\u30b9\u30c8\u30d7\u30e9\u30af\u30c6\u30a3\u30b9\u3092\u53c2\u8003\u306b\u3001\u30d7\u30ed\u30b8\u30a7\u30af\u30c8\u306e\u8981\u4ef6\u306b\u5408\u308f\u305b\u3066\u6700\u9069\u306a\u5b9f\u88c5\u3092\u9078\u629e\u3057\u3066\u304f\u3060\u3055\u3044\u3002\u307e\u305f\u3001\u5e38\u306b\u30d1\u30d5\u30a9\u30fc\u30de\u30f3\u30b9\u3068\u30e1\u30f3\u30c6\u30ca\u30f3\u30b9\u6027\u306e\u30d0\u30e9\u30f3\u30b9\u3092\u8003\u616e\u3057\u306a\u304c\u3089\u3001\u7d99\u7d9a\u7684\u306a\u6539\u5584\u3092\u5fc3\u304c\u3051\u308b\u3053\u3068\u304c\u91cd\u8981\u3067\u3059\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Warning: Undefined array key &#8220;is_admin&#8221; in \/home\/xs392991\/dexall.co.jp\/public_html\/articles\/wp-content\/themes\/ &#8230; <\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[12],"tags":[],"class_list":["post-2509","post","type-post","status-publish","format-standard","category-php","nothumb"],"_links":{"self":[{"href":"https:\/\/dexall.co.jp\/articles\/index.php?rest_route=\/wp\/v2\/posts\/2509","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/dexall.co.jp\/articles\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/dexall.co.jp\/articles\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/dexall.co.jp\/articles\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/dexall.co.jp\/articles\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=2509"}],"version-history":[{"count":2,"href":"https:\/\/dexall.co.jp\/articles\/index.php?rest_route=\/wp\/v2\/posts\/2509\/revisions"}],"predecessor-version":[{"id":2511,"href":"https:\/\/dexall.co.jp\/articles\/index.php?rest_route=\/wp\/v2\/posts\/2509\/revisions\/2511"}],"wp:attachment":[{"href":"https:\/\/dexall.co.jp\/articles\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=2509"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/dexall.co.jp\/articles\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=2509"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/dexall.co.jp\/articles\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=2509"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}