{"id":2663,"date":"2025-03-24T08:46:50","date_gmt":"2025-03-23T23:46:50","guid":{"rendered":"https:\/\/dexall.co.jp\/articles\/?p=2663"},"modified":"2025-03-24T08:47:18","modified_gmt":"2025-03-23T23:47:18","slug":"laravel%e3%83%a2%e3%83%87%e3%83%ab%e3%82%92%e3%83%9e%e3%82%b9%e3%82%bf%e3%83%bc%e3%81%99%e3%82%8b%e3%81%9f%e3%82%81%e3%81%aeno-1%e3%82%ac%e3%82%a4%e3%83%89%ef%bc%9a13%e3%81%ae%e5%bf%85%e9%a0%88","status":"publish","type":"post","link":"https:\/\/dexall.co.jp\/articles\/?p=2663","title":{"rendered":"Laravel\u30e2\u30c7\u30eb\u3092\u30de\u30b9\u30bf\u30fc\u3059\u308b\u305f\u3081\u306eNo.1\u30ac\u30a4\u30c9\uff1a13\u306e\u5fc5\u9808\u30c6\u30af\u30cb\u30c3\u30af\u3068\u5b9f\u8df5\u4f8b"},"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\">Laravel\u30e2\u30c7\u30eb\u306e\u57fa\u790e\u77e5\u8b58\u3068\u30d9\u30b9\u30c8\u30d7\u30e9\u30af\u30c6\u30a3\u30b9<\/a>    <ul class=\"menu_level_1\">      <li class=\"first\">        <a href=\"#i-1\">\u30e2\u30c7\u30eb\u3068\u306f\u4f55\u304b\uff1aMVC\u30a2\u30fc\u30ad\u30c6\u30af\u30c1\u30e3\u306b\u304a\u3051\u308b\u5f79\u5272<\/a>      <\/li>      <li>        <a href=\"#i-4\">Eloquent ORM\u306e\u7279\u5fb4\u3068\u5229\u70b9<\/a>      <\/li>      <li class=\"last\">        <a href=\"#i-6\">\u30e2\u30c7\u30eb\u30d5\u30a1\u30a4\u30eb\u306e\u57fa\u672c\u69cb\u9020\u3068\u547d\u540d\u898f\u5247<\/a>      <\/li>    <\/ul>  <\/li>  <li>    <a href=\"#i-9\">\u5b9f\u8df5\u3067\u4f7f\u3048\u308b\uff01Laravel\u30e2\u30c7\u30eb\u306e\u8a2d\u5b9a\u65b9\u6cd5<\/a>    <ul class=\"menu_level_1\">      <li class=\"first\">        <a href=\"#i-10\">\u30c6\u30fc\u30d6\u30eb\u540d\u3068\u30d7\u30e9\u30a4\u30de\u30ea\u30fc\u30ad\u30fc\u306e\u30ab\u30b9\u30bf\u30de\u30a4\u30ba<\/a>      <\/li>      <li>        <a href=\"#i-14\">\u30bf\u30a4\u30e0\u30b9\u30bf\u30f3\u30d7\u3068\u65e5\u4ed8\u30df\u30e5\u30fc\u30c6\u30bf\u30fc\u306e\u6d3b\u7528\u6cd5<\/a>      <\/li>      <li class=\"last\">        <a href=\"#i-17\">\u30e2\u30c7\u30eb\u30a4\u30d9\u30f3\u30c8\u3092\u4f7f\u3063\u305f\u81ea\u52d5\u51e6\u7406\u306e\u5b9f\u88c5<\/a>      <\/li>    <\/ul>  <\/li>  <li>    <a href=\"#i-21\">\u30c7\u30fc\u30bf\u30d9\u30fc\u30b9\u30ea\u30ec\u30fc\u30b7\u30e7\u30f3\u3092\u6975\u3081\u308b<\/a>    <ul class=\"menu_level_1\">      <li class=\"first\">        <a href=\"#i-22\">hasMany\u30fbbelongsTo\u30671\u5bfe\u591a\u306e\u95a2\u4fc2\u3092\u69cb\u7bc9<\/a>      <\/li>      <li>        <a href=\"#i-25\">\u591a\u5bfe\u591a\u30ea\u30ec\u30fc\u30b7\u30e7\u30f3\u3068\u30d4\u30dc\u30c3\u30c8\u30c6\u30fc\u30d6\u30eb\u306e\u4f7f\u3044\u65b9<\/a>      <\/li>      <li class=\"last\">        <a href=\"#i-28\">\u30dd\u30ea\u30e2\u30fc\u30d5\u30a3\u30c3\u30af\u30ea\u30ec\u30fc\u30b7\u30e7\u30f3\u306e\u5b9f\u88c5\u30c6\u30af\u30cb\u30c3\u30af<\/a>      <\/li>    <\/ul>  <\/li>  <li>    <a href=\"#i-31\">\u30af\u30a8\u30ea\u30d3\u30eb\u30c0\u3068\u30b9\u30b3\u30fc\u30d7\u306e\u5fdc\u7528\u30c6\u30af\u30cb\u30c3\u30af<\/a>    <ul class=\"menu_level_1\">      <li class=\"first\">        <a href=\"#i-32\">\u30ed\u30fc\u30ab\u30eb\u30b9\u30b3\u30fc\u30d7\u3067\u5171\u901a\u306e\u30af\u30a8\u30ea\u3092\u518d\u5229\u7528<\/a>      <\/li>      <li>        <a href=\"#i-35\">\u30b0\u30ed\u30fc\u30d0\u30eb\u30b9\u30b3\u30fc\u30d7\u3067\u30e2\u30c7\u30eb\u5168\u4f53\u306e\u52d5\u4f5c\u3092\u30ab\u30b9\u30bf\u30de\u30a4\u30ba<\/a>      <\/li>      <li class=\"last\">        <a href=\"#i-38\">Eager\u30ed\u30fc\u30c7\u30a3\u30f3\u30b0\u3067\u30d1\u30d5\u30a9\u30fc\u30de\u30f3\u30b9\u3092\u6700\u9069\u5316<\/a>      <\/li>    <\/ul>  <\/li>  <li>    <a href=\"#i-42\">\u5b9f\u8df5\u7684\u306aLaravel\u30e2\u30c7\u30eb\u6d3b\u7528\u4e8b\u4f8b<\/a>    <ul class=\"menu_level_1\">      <li class=\"first\">        <a href=\"#i-43\">\u30bd\u30d5\u30c8\u30c7\u30ea\u30fc\u30c8\u3092\u4f7f\u3063\u305f\u8ad6\u7406\u524a\u9664\u306e\u5b9f\u88c5<\/a>      <\/li>      <li>        <a href=\"#i-46\">\u30e2\u30c7\u30eb\u30d5\u30a1\u30af\u30c8\u30ea\u3068\u30b7\u30fc\u30c0\u30fc\u3067\u30c6\u30b9\u30c8\u30c7\u30fc\u30bf\u3092\u751f\u6210<\/a>      <\/li>      <li class=\"last\">        <a href=\"#i-48\">API\u30ea\u30bd\u30fc\u30b9\u3068\u30e2\u30c7\u30eb\u306e\u9023\u643a\u65b9\u6cd5<\/a>      <\/li>    <\/ul>  <\/li>  <li class=\"last\">    <a href=\"#i-51\">Laravel\u30e2\u30c7\u30eb\u306e\u30c8\u30e9\u30d6\u30eb\u30b7\u30e5\u30fc\u30c6\u30a3\u30f3\u30b0<\/a>    <ul class=\"menu_level_1\">      <li class=\"first\">        <a href=\"#i-52\">\u3088\u304f\u3042\u308bN+1\u554f\u984c\u306e\u89e3\u6c7a\u65b9\u6cd5<\/a>      <\/li>      <li>        <a href=\"#i-55\">\u5927\u898f\u6a21\u30c7\u30fc\u30bf\u51e6\u7406\u6642\u306e\u30e1\u30e2\u30ea\u7ba1\u7406\u30c6\u30af\u30cb\u30c3\u30af<\/a>      <\/li>      <li class=\"last\">        <a href=\"#i-57\">\u30e2\u30c7\u30eb\u30a4\u30d9\u30f3\u30c8\u306e\u30c7\u30d0\u30c3\u30b0\u65b9\u6cd5<\/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\">Laravel\u30e2\u30c7\u30eb\u306e\u57fa\u790e\u77e5\u8b58\u3068\u30d9\u30b9\u30c8\u30d7\u30e9\u30af\u30c6\u30a3\u30b9<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"i-1\">\u30e2\u30c7\u30eb\u3068\u306f\u4f55\u304b\uff1aMVC\u30a2\u30fc\u30ad\u30c6\u30af\u30c1\u30e3\u306b\u304a\u3051\u308b\u5f79\u5272<\/h3>\n\n\n\n<p>Laravel\u306f\u3001MVC\u30a2\u30fc\u30ad\u30c6\u30af\u30c1\u30e3\u3092\u63a1\u7528\u3057\u3066\u3044\u308b\u30d5\u30ec\u30fc\u30e0\u30ef\u30fc\u30af\u3067\u3059\u3002\u305d\u306e\u4e2d\u3067\u3082\u30e2\u30c7\u30eb\uff08Model\uff09\u306f\u3001\u30a2\u30d7\u30ea\u30b1\u30fc\u30b7\u30e7\u30f3\u306e\u30d3\u30b8\u30cd\u30b9\u30ed\u30b8\u30c3\u30af\u3068\u30c7\u30fc\u30bf\u64cd\u4f5c\u3092\u62c5\u3046\u91cd\u8981\u306a\u8981\u7d20\u3067\u3059\u3002<\/p>\n\n\n\n<h4 class=\"wp-block-heading\" id=\"i-2\">\u30e2\u30c7\u30eb\u306e\u4e3b\u306a\u5f79\u5272<\/h4>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>\u30c7\u30fc\u30bf\u306e\u6c38\u7d9a\u5316\u5c64\u3068\u306e\u30a4\u30f3\u30bf\u30fc\u30d5\u30a7\u30fc\u30b9<\/strong><\/li>\n<\/ol>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u30c7\u30fc\u30bf\u30d9\u30fc\u30b9\u3068\u306e\u901a\u4fe1\u3092\u62bd\u8c61\u5316<\/li>\n\n\n\n<li>SQL\u3092\u76f4\u63a5\u66f8\u304b\u305a\u306b\u30c7\u30fc\u30bf\u64cd\u4f5c\u304c\u53ef\u80fd<\/li>\n\n\n\n<li>\u30c7\u30fc\u30bf\u306e\u6574\u5408\u6027\u3092\u4fdd\u8b77<\/li>\n<\/ul>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>\u30d3\u30b8\u30cd\u30b9\u30ed\u30b8\u30c3\u30af\u306e\u5b9f\u88c5<\/strong><\/li>\n<\/ol>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u30c7\u30fc\u30bf\u306e\u691c\u8a3c\u30eb\u30fc\u30eb<\/li>\n\n\n\n<li>\u8a08\u7b97\u30ed\u30b8\u30c3\u30af<\/li>\n\n\n\n<li>\u30c9\u30e1\u30a4\u30f3\u30eb\u30fc\u30eb\u306e\u5b9f\u88c5<\/li>\n<\/ul>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>\u30c7\u30fc\u30bf\u306e\u95a2\u9023\u4ed8\u3051<\/strong><\/li>\n<\/ol>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u30c6\u30fc\u30d6\u30eb\u9593\u306e\u30ea\u30ec\u30fc\u30b7\u30e7\u30f3\u30b7\u30c3\u30d7\u306e\u5b9a\u7fa9<\/li>\n\n\n\n<li>\u95a2\u9023\u30c7\u30fc\u30bf\u306e\u53d6\u5f97\u3068\u64cd\u4f5c<\/li>\n<\/ul>\n\n\n\n<h4 class=\"wp-block-heading\" id=\"i-3\">\u57fa\u672c\u7684\u306a\u30e2\u30c7\u30eb\u306e\u5b9f\u88c5\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=\"\">&lt;?php\n\nnamespace App\\Models;\n\nuse Illuminate\\Database\\Eloquent\\Model;\nuse Illuminate\\Database\\Eloquent\\SoftDeletes;\n\nclass User extends Model\n{\n    use SoftDeletes;  \/\/ \u8ad6\u7406\u524a\u9664\u3092\u6709\u52b9\u5316\n\n    \/\/ \u30c6\u30fc\u30d6\u30eb\u540d\u3092\u660e\u793a\u7684\u306b\u6307\u5b9a\uff08\u7701\u7565\u53ef\uff09\n    protected $table = 'users';\n\n    \/\/ \u4e00\u62ec\u4ee3\u5165\u53ef\u80fd\u306a\u5c5e\u6027\n    protected $fillable = [\n        'name',\n        'email',\n        'password',\n    ];\n\n    \/\/ JSON\u306b\u542b\u3081\u306a\u3044\u5c5e\u6027\n    protected $hidden = [\n        'password',\n        'remember_token',\n    ];\n\n    \/\/ \u578b\u30ad\u30e3\u30b9\u30c8\n    protected $casts = [\n        'email_verified_at' =&gt; 'datetime',\n        'is_admin' =&gt; 'boolean',\n    ];\n}<\/pre>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"i-4\">Eloquent ORM\u306e\u7279\u5fb4\u3068\u5229\u70b9<\/h3>\n\n\n\n<p>Eloquent ORM\u306f\u3001Laravel\u306b\u7d44\u307f\u8fbc\u307e\u308c\u305fORM\u30c4\u30fc\u30eb\u3067\u3059\u3002ActiveRecord\u30d1\u30bf\u30fc\u30f3\u3092\u63a1\u7528\u3057\u3066\u304a\u308a\u3001\u76f4\u611f\u7684\u306a\u30c7\u30fc\u30bf\u30d9\u30fc\u30b9\u64cd\u4f5c\u3092\u53ef\u80fd\u306b\u3057\u307e\u3059\u3002<\/p>\n\n\n\n<h4 class=\"wp-block-heading\" id=\"i-5\">Eloquent\u306e\u4e3b\u306a\u7279\u5fb4<\/h4>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>\u6d41\u66a2\u306a\u30a4\u30f3\u30bf\u30fc\u30d5\u30a7\u30fc\u30b9<\/strong><\/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=\"\">   \/\/ \u30af\u30a8\u30ea\u30d3\u30eb\u30c0\u3092\u4f7f\u7528\u3057\u305f\u691c\u7d22\n   $users = User::where('age', '&gt;=', 18)\n              -&gt;orderBy('name')\n              -&gt;take(10)\n              -&gt;get();\n\n   \/\/ \u30ea\u30ec\u30fc\u30b7\u30e7\u30f3\u3092\u4f7f\u7528\u3057\u305f\u30c7\u30fc\u30bf\u53d6\u5f97\n   $user = User::with('posts')\n              -&gt;find(1);<\/pre>\n\n\n\n<ol start=\"2\" class=\"wp-block-list\">\n<li><strong>\u7c21\u6f54\u306aCRUD\u64cd\u4f5c<\/strong><\/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\u306e\u4f5c\u6210\n   $user = User::create([\n       'name' =&gt; '\u5c71\u7530\u592a\u90ce',\n       'email' =&gt; 'yamada@example.com'\n   ]);\n\n   \/\/ \u30c7\u30fc\u30bf\u306e\u53d6\u5f97\u3068\u66f4\u65b0\n   $user = User::find(1);\n   $user-&gt;name = '\u5c71\u7530\u82b1\u5b50';\n   $user-&gt;save();\n\n   \/\/ \u30c7\u30fc\u30bf\u306e\u524a\u9664\n   $user-&gt;delete();<\/pre>\n\n\n\n<ol start=\"3\" class=\"wp-block-list\">\n<li><strong>\u30e2\u30c7\u30eb\u30a4\u30d9\u30f3\u30c8<\/strong><\/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 User extends Model\n   {\n       \/\/ \u30e2\u30c7\u30eb\u306e\u4fdd\u5b58\u524d\u306b\u5b9f\u884c\n       protected static function boot()\n       {\n           parent::boot();\n\n           static::creating(function ($user) {\n               $user-&gt;api_token = Str::random(60);\n           });\n\n           static::updating(function ($user) {\n               \/\/ \u66f4\u65b0\u6642\u306e\u51e6\u7406\n           });\n       }\n   }<\/pre>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"i-6\">\u30e2\u30c7\u30eb\u30d5\u30a1\u30a4\u30eb\u306e\u57fa\u672c\u69cb\u9020\u3068\u547d\u540d\u898f\u5247<\/h3>\n\n\n\n<p>Laravel\u30e2\u30c7\u30eb\u3092\u52b9\u679c\u7684\u306b\u4f7f\u7528\u3059\u308b\u305f\u3081\u306b\u306f\u3001\u9069\u5207\u306a\u69cb\u9020\u5316\u3068\u547d\u540d\u898f\u5247\u306b\u5f93\u3046\u3053\u3068\u304c\u91cd\u8981\u3067\u3059\u3002<\/p>\n\n\n\n<h4 class=\"wp-block-heading\" id=\"i-7\">\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><strong>\u30e2\u30c7\u30eb\u540d<\/strong><\/li>\n<\/ol>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u5358\u6570\u5f62\u3067\u5927\u6587\u5b57\u304b\u3089\u59cb\u3081\u308b<\/li>\n\n\n\n<li>\u4f8b\uff1aUser, Post, Comment<\/li>\n<\/ul>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>\u30c6\u30fc\u30d6\u30eb\u540d<\/strong><\/li>\n<\/ol>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u8907\u6570\u5f62\u3067\u5c0f\u6587\u5b57<\/li>\n\n\n\n<li>\u30b9\u30cd\u30fc\u30af\u30b1\u30fc\u30b9\u3092\u4f7f\u7528<\/li>\n\n\n\n<li>\u4f8b\uff1ausers, blog_posts, post_comments<\/li>\n<\/ul>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>\u30ea\u30ec\u30fc\u30b7\u30e7\u30f3\u30e1\u30bd\u30c3\u30c9<\/strong><\/li>\n<\/ol>\n\n\n\n<ul class=\"wp-block-list\">\n<li>hasOne, hasMany: \u52d5\u8a5e + \u5358\u6570\/\u8907\u6570\u5f62<\/li>\n\n\n\n<li>belongsTo: \u5358\u6570\u5f62<\/li>\n<\/ul>\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 User extends Model\n   {\n       \/\/ hasMany\u95a2\u4fc2\uff08\u8907\u6570\u5f62\uff09\n       public function posts()\n       {\n           return $this-&gt;hasMany(Post::class);\n       }\n\n       \/\/ belongsTo\u95a2\u4fc2\uff08\u5358\u6570\u5f62\uff09\n       public function department()\n       {\n           return $this-&gt;belongsTo(Department::class);\n       }\n   }<\/pre>\n\n\n\n<h4 class=\"wp-block-heading\" id=\"i-8\">\u30e2\u30c7\u30eb\u30d5\u30a1\u30a4\u30eb\u306e\u69cb\u9020\u5316<\/h4>\n\n\n\n<p>\u63a8\u5968\u3055\u308c\u308b\u69cb\u9020\u9806\u5e8f\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=\"\">class Post extends Model\n{\n    \/\/ 1. \u30c8\u30ec\u30a4\u30c8\u306e\u4f7f\u7528\n    use SoftDeletes, HasFactory;\n\n    \/\/ 2. \u30d7\u30ed\u30d1\u30c6\u30a3\u306e\u5b9a\u7fa9\n    protected $table = 'posts';\n    protected $primaryKey = 'id';\n    public $timestamps = true;\n\n    \/\/ 3. \u5c5e\u6027\u95a2\u9023\u306e\u5b9a\u7fa9\n    protected $fillable = ['title', 'content'];\n    protected $hidden = ['secret_key'];\n    protected $casts = ['published_at' =&gt; 'datetime'];\n\n    \/\/ 4. \u30ea\u30ec\u30fc\u30b7\u30e7\u30f3\u30b7\u30c3\u30d7\u306e\u5b9a\u7fa9\n    public function user()\n    {\n        return $this-&gt;belongsTo(User::class);\n    }\n    public function comments()\n    {\n        return $this-&gt;hasMany(Comment::class);\n    }\n\n    \/\/ 5. \u30b9\u30b3\u30fc\u30d7\u306e\u5b9a\u7fa9\n    public function scopePublished($query)\n    {\n        return $query-&gt;where('status', 'published');\n    }\n\n    \/\/ 6. \u30a2\u30af\u30bb\u30b5\u30fb\u30df\u30e5\u30fc\u30c6\u30bf\u306e\u5b9a\u7fa9\n    public function getTitleAttribute($value)\n    {\n        return ucfirst($value);\n    }\n\n    \/\/ 7. \u305d\u306e\u4ed6\u306e\u30ab\u30b9\u30bf\u30e0\u30e1\u30bd\u30c3\u30c9\n    public function getExcerpt($length = 100)\n    {\n        return Str::limit($this-&gt;content, $length);\n    }\n}<\/pre>\n\n\n\n<p>\u3053\u306e\u3088\u3046\u306a\u69cb\u9020\u5316\u306b\u3088\u308a\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u30b3\u30fc\u30c9\u306e\u53ef\u8aad\u6027\u304c\u5411\u4e0a<\/li>\n\n\n\n<li>\u30e1\u30f3\u30c6\u30ca\u30f3\u30b9\u304c\u5bb9\u6613\u306b<\/li>\n\n\n\n<li>\u30c1\u30fc\u30e0\u958b\u767a\u3067\u306e\u7d71\u4e00\u6027\u3092\u78ba\u4fdd<\/li>\n<\/ul>\n\n\n\n<p>\u4ee5\u4e0a\u304c\u3001Laravel\u30e2\u30c7\u30eb\u306e\u57fa\u790e\u77e5\u8b58\u3068\u30d9\u30b9\u30c8\u30d7\u30e9\u30af\u30c6\u30a3\u30b9\u306e\u6982\u8981\u3067\u3059\u3002\u3053\u308c\u3089\u306e\u57fa\u672c\u3092\u62bc\u3055\u3048\u308b\u3053\u3068\u3067\u3001\u3088\u308a\u52b9\u679c\u7684\u306a\u30e2\u30c7\u30eb\u306e\u5b9f\u88c5\u304c\u53ef\u80fd\u306b\u306a\u308a\u307e\u3059\u3002\u6b21\u306e\u30bb\u30af\u30b7\u30e7\u30f3\u3067\u306f\u3001\u3053\u308c\u3089\u306e\u77e5\u8b58\u3092\u6d3b\u7528\u3057\u305f\u5b9f\u8df5\u7684\u306a\u8a2d\u5b9a\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-9\">\u5b9f\u8df5\u3067\u4f7f\u3048\u308b\uff01Laravel\u30e2\u30c7\u30eb\u306e\u8a2d\u5b9a\u65b9\u6cd5<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"i-10\">\u30c6\u30fc\u30d6\u30eb\u540d\u3068\u30d7\u30e9\u30a4\u30de\u30ea\u30fc\u30ad\u30fc\u306e\u30ab\u30b9\u30bf\u30de\u30a4\u30ba<\/h3>\n\n\n\n<p>Laravel\u306e\u30e2\u30c7\u30eb\u306f\u898f\u7d04\u306b\u3088\u308b\u8a2d\u5b9a\uff08Convention over Configuration\uff09\u3092\u63a1\u7528\u3057\u3066\u3044\u307e\u3059\u304c\u3001\u6642\u306b\u306f\u65e2\u5b58\u306e\u30c7\u30fc\u30bf\u30d9\u30fc\u30b9\u69cb\u9020\u3084\u30d7\u30ed\u30b8\u30a7\u30af\u30c8\u8981\u4ef6\u306b\u5408\u308f\u305b\u3066\u30ab\u30b9\u30bf\u30de\u30a4\u30ba\u304c\u5fc5\u8981\u306b\u306a\u308a\u307e\u3059\u3002<\/p>\n\n\n\n<h4 class=\"wp-block-heading\" id=\"i-11\">\u30c6\u30fc\u30d6\u30eb\u540d\u306e\u30ab\u30b9\u30bf\u30de\u30a4\u30ba<\/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 CustomerInfo extends Model\n{\n    \/\/ \u30c6\u30fc\u30d6\u30eb\u540d\u3092\u660e\u793a\u7684\u306b\u6307\u5b9a\n    protected $table = 'customer_information';\n\n    \/\/ \u30bf\u30a4\u30e0\u30b9\u30bf\u30f3\u30d7\u3092\u7121\u52b9\u5316\uff08created_at, updated_at\u3092\u4f7f\u7528\u3057\u306a\u3044\u5834\u5408\uff09\n    public $timestamps = false;\n\n    \/\/ \u30c7\u30fc\u30bf\u30d9\u30fc\u30b9\u63a5\u7d9a\u3092\u6307\u5b9a\uff08\u8907\u6570\u306e\u30c7\u30fc\u30bf\u30d9\u30fc\u30b9\u3092\u4f7f\u7528\u3059\u308b\u5834\u5408\uff09\n    protected $connection = 'customer_db';\n}<\/pre>\n\n\n\n<h4 class=\"wp-block-heading\" id=\"i-12\">\u30d7\u30e9\u30a4\u30de\u30ea\u30fc\u30ad\u30fc\u306e\u30ab\u30b9\u30bf\u30de\u30a4\u30ba<\/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 Product extends Model\n{\n    \/\/ \u30d7\u30e9\u30a4\u30de\u30ea\u30fc\u30ad\u30fc\u306e\u30ab\u30e9\u30e0\u540d\u3092\u5909\u66f4\n    protected $primaryKey = 'product_id';\n\n    \/\/ \u6570\u5024\u4ee5\u5916\u306e\u30d7\u30e9\u30a4\u30de\u30ea\u30fc\u30ad\u30fc\u3092\u4f7f\u7528\u3059\u308b\u5834\u5408\n    public $incrementing = false;\n\n    \/\/ \u6587\u5b57\u5217\u306e\u30d7\u30e9\u30a4\u30de\u30ea\u30fc\u30ad\u30fc\u3092\u4f7f\u7528\u3059\u308b\u5834\u5408\n    protected $keyType = 'string';\n}<\/pre>\n\n\n\n<h4 class=\"wp-block-heading\" id=\"i-13\">\u5b9f\u88c5\u306e\u30d9\u30b9\u30c8\u30d7\u30e9\u30af\u30c6\u30a3\u30b9<\/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    \/\/ \u30e2\u30c7\u30eb\u306e\u57fa\u672c\u8a2d\u5b9a\u3092\u30c8\u30ec\u30a4\u30c8\u3068\u3057\u3066\u307e\u3068\u3081\u308b\n    use HasCustomDatabase;\n\n    \/\/ \u30ab\u30b9\u30bf\u30e0\u63a5\u7d9a\u8a2d\u5b9a\u306e\u5b9a\u6570\u5316\n    const DB_CONNECTION = 'orders_db';\n    const TABLE_NAME = 'order_details';\n\n    public function __construct(array $attributes = [])\n    {\n        parent::__construct($attributes);\n\n        \/\/ \u52d5\u7684\u306a\u30c6\u30fc\u30d6\u30eb\u540d\u306e\u8a2d\u5b9a\uff08\u4f8b\uff1a\u30b7\u30e3\u30fc\u30c7\u30a3\u30f3\u30b0\uff09\n        $this-&gt;setTable(self::TABLE_NAME . '_' . config('app.environment'));\n    }\n}<\/pre>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"i-14\">\u30bf\u30a4\u30e0\u30b9\u30bf\u30f3\u30d7\u3068\u65e5\u4ed8\u30df\u30e5\u30fc\u30c6\u30bf\u30fc\u306e\u6d3b\u7528\u6cd5<\/h3>\n\n\n\n<p>Laravel\u306e\u30e2\u30c7\u30eb\u306f\u3001\u65e5\u4ed8\u51e6\u7406\u306b\u95a2\u3059\u308b\u5f37\u529b\u306a\u6a5f\u80fd\u3092\u63d0\u4f9b\u3057\u3066\u3044\u307e\u3059\u3002<\/p>\n\n\n\n<h4 class=\"wp-block-heading\" id=\"i-15\">\u57fa\u672c\u7684\u306a\u30bf\u30a4\u30e0\u30b9\u30bf\u30f3\u30d7\u306e\u30ab\u30b9\u30bf\u30de\u30a4\u30ba<\/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    \/\/ \u30bf\u30a4\u30e0\u30b9\u30bf\u30f3\u30d7\u30ab\u30e9\u30e0\u306e\u540d\u524d\u3092\u30ab\u30b9\u30bf\u30de\u30a4\u30ba\n    const CREATED_AT = 'creation_date';\n    const UPDATED_AT = 'last_modified';\n\n    \/\/ \u65e5\u4ed8\u3068\u3057\u3066\u6271\u3046\u30ab\u30e9\u30e0\u3092\u6307\u5b9a\n    protected $dates = [\n        'published_at',\n        'review_date',\n        'expiry_date'\n    ];\n\n    \/\/ \u65e5\u4ed8\u306e\u30d5\u30a9\u30fc\u30de\u30c3\u30c8\u3092\u30ab\u30b9\u30bf\u30de\u30a4\u30ba\n    protected $dateFormat = 'Y-m-d H:i:s.u';\n}<\/pre>\n\n\n\n<h4 class=\"wp-block-heading\" id=\"i-16\">\u9ad8\u5ea6\u306a\u65e5\u4ed8\u64cd\u4f5c\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 Event extends Model\n{\n    protected $casts = [\n        'start_date' =&gt; 'datetime:Y-m-d',\n        'end_date' =&gt; 'datetime:Y-m-d',\n        'is_active' =&gt; 'boolean'\n    ];\n\n    \/\/ \u65e5\u4ed8\u30df\u30e5\u30fc\u30c6\u30bf\u30fc\u306e\u5b9f\u88c5\n    public function setStartDateAttribute($value)\n    {\n        $this-&gt;attributes['start_date'] = Carbon::parse($value)-&gt;startOfDay();\n    }\n\n    \/\/ \u65e5\u4ed8\u30a2\u30af\u30bb\u30b5\u306e\u5b9f\u88c5\n    public function getEventDurationAttribute()\n    {\n        return $this-&gt;start_date-&gt;diffInDays($this-&gt;end_date);\n    }\n\n    \/\/ \u30b9\u30b3\u30fc\u30d7\u3092\u4f7f\u7528\u3057\u305f\u65e5\u4ed8\u30d9\u30fc\u30b9\u306e\u691c\u7d22\n    public function scopeUpcoming($query)\n    {\n        return $query-&gt;where('start_date', '&gt;', Carbon::now());\n    }\n}<\/pre>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"i-17\">\u30e2\u30c7\u30eb\u30a4\u30d9\u30f3\u30c8\u3092\u4f7f\u3063\u305f\u81ea\u52d5\u51e6\u7406\u306e\u5b9f\u88c5<\/h3>\n\n\n\n<p>\u30e2\u30c7\u30eb\u30a4\u30d9\u30f3\u30c8\u3092\u6d3b\u7528\u3059\u308b\u3053\u3068\u3067\u3001\u30c7\u30fc\u30bf\u306e\u4f5c\u6210\u3001\u66f4\u65b0\u3001\u524a\u9664\u6642\u306b\u81ea\u52d5\u7684\u306b\u51e6\u7406\u3092\u5b9f\u884c\u3067\u304d\u307e\u3059\u3002<\/p>\n\n\n\n<h4 class=\"wp-block-heading\" id=\"i-18\">\u57fa\u672c\u7684\u306a\u30a4\u30d9\u30f3\u30c8\u30cf\u30f3\u30c9\u30ea\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 User extends Model\n{\n    protected static function boot()\n    {\n        parent::boot();\n\n        \/\/ \u4f5c\u6210\u6642\u306e\u30a4\u30d9\u30f3\u30c8\n        static::creating(function ($user) {\n            $user-&gt;uuid = (string) Str::uuid();\n            $user-&gt;activation_token = Str::random(32);\n        });\n\n        \/\/ \u66f4\u65b0\u6642\u306e\u30a4\u30d9\u30f3\u30c8\n        static::updating(function ($user) {\n            if ($user-&gt;isDirty('email')) {\n                $user-&gt;email_verified_at = null;\n                $user-&gt;sendEmailVerificationNotification();\n            }\n        });\n    }\n}<\/pre>\n\n\n\n<h4 class=\"wp-block-heading\" id=\"i-19\">\u30a4\u30d9\u30f3\u30c8\u3092\u4f7f\u3063\u305f\u9ad8\u5ea6\u306a\u81ea\u52d5\u51e6\u7406\u306e\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=\"\">class Invoice extends Model\n{\n    use SoftDeletes;\n\n    protected $dispatchesEvents = [\n        'saved' =&gt; InvoiceSaved::class,\n        'deleted' =&gt; InvoiceDeleted::class,\n    ];\n\n    protected static function boot()\n    {\n        parent::boot();\n\n        \/\/ \u8acb\u6c42\u66f8\u756a\u53f7\u306e\u81ea\u52d5\u751f\u6210\n        static::creating(function ($invoice) {\n            $invoice-&gt;number = static::generateInvoiceNumber();\n        });\n\n        \/\/ \u95a2\u9023\u3059\u308b\u652f\u6255\u3044\u8a18\u9332\u306e\u66f4\u65b0\n        static::updated(function ($invoice) {\n            if ($invoice-&gt;isDirty('status')) {\n                $invoice-&gt;payments()-&gt;update(['status' =&gt; $invoice-&gt;status]);\n            }\n        });\n\n        \/\/ \u524a\u9664\u6642\u306e\u6574\u5408\u6027\u30c1\u30a7\u30c3\u30af\n        static::deleting(function ($invoice) {\n            if ($invoice-&gt;payments()-&gt;unpaid()-&gt;exists()) {\n                throw new \\Exception('\u672a\u6255\u3044\u306e\u8acb\u6c42\u66f8\u306f\u524a\u9664\u3067\u304d\u307e\u305b\u3093');\n            }\n        });\n    }\n\n    \/\/ \u8acb\u6c42\u66f8\u756a\u53f7\u751f\u6210\u30ed\u30b8\u30c3\u30af\n    protected static function generateInvoiceNumber()\n    {\n        $latest = static::whereYear('created_at', now()-&gt;year)\n                       -&gt;latest('number')\n                       -&gt;first();\n\n        $number = $latest ? $latest-&gt;number + 1 : 1;\n        return sprintf('%s%06d', now()-&gt;format('Y'), $number);\n    }\n}<\/pre>\n\n\n\n<h4 class=\"wp-block-heading\" id=\"i-20\">\u30a4\u30d9\u30f3\u30c8\u4f7f\u7528\u6642\u306e\u6ce8\u610f\u70b9<\/h4>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>\u30d1\u30d5\u30a9\u30fc\u30de\u30f3\u30b9\u3078\u306e\u914d\u616e<\/strong><\/li>\n<\/ol>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u91cd\u3044\u51e6\u7406\u306f\u30ad\u30e5\u30fc\u306b\u5165\u308c\u308b<\/li>\n\n\n\n<li>\u5fc5\u8981\u6700\u5c0f\u9650\u306e\u30a4\u30d9\u30f3\u30c8\u306e\u307f\u3092\u4f7f\u7528<\/li>\n<\/ul>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>\u7121\u9650\u30eb\u30fc\u30d7\u306e\u9632\u6b62<\/strong><\/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=\"\">   protected static function boot()\n   {\n       parent::boot();\n\n       static::updating(function ($model) {\n           \/\/ \u30a4\u30d9\u30f3\u30c8\u5185\u3067save()\u3092\u547c\u3076\u3068\u7121\u9650\u30eb\u30fc\u30d7\u306e\u53ef\u80fd\u6027\n           if ($model-&gt;isDirty('some_field')) {\n               \\DB::transaction(function () use ($model) {\n                   $model-&gt;related_models()-&gt;update([\n                       'status' =&gt; 'updated'\n                   ]);\n               });\n           }\n       });\n   }<\/pre>\n\n\n\n<p>\u4ee5\u4e0a\u304c\u3001Laravel\u30e2\u30c7\u30eb\u306e\u5b9f\u8df5\u7684\u306a\u8a2d\u5b9a\u65b9\u6cd5\u306e\u89e3\u8aac\u3067\u3059\u3002\u3053\u308c\u3089\u306e\u6280\u8853\u3092\u9069\u5207\u306b\u7d44\u307f\u5408\u308f\u305b\u308b\u3053\u3068\u3067\u3001\u4fdd\u5b88\u6027\u304c\u9ad8\u304f\u3001\u5805\u7262\u306a\u30a2\u30d7\u30ea\u30b1\u30fc\u30b7\u30e7\u30f3\u306e\u69cb\u7bc9\u304c\u53ef\u80fd\u306b\u306a\u308a\u307e\u3059\u3002<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"i-21\">\u30c7\u30fc\u30bf\u30d9\u30fc\u30b9\u30ea\u30ec\u30fc\u30b7\u30e7\u30f3\u3092\u6975\u3081\u308b<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"i-22\">hasMany\u30fbbelongsTo\u30671\u5bfe\u591a\u306e\u95a2\u4fc2\u3092\u69cb\u7bc9<\/h3>\n\n\n\n<p>1\u5bfe\u591a\u306e\u30ea\u30ec\u30fc\u30b7\u30e7\u30f3\u306f\u3001\u6700\u3082\u4e00\u822c\u7684\u306b\u4f7f\u7528\u3055\u308c\u308b\u30c7\u30fc\u30bf\u30d9\u30fc\u30b9\u306e\u95a2\u4fc2\u6027\u3067\u3059\u3002Laravel\u3067\u306f\u3001\u3053\u306e\u95a2\u4fc2\u3092\u76f4\u611f\u7684\u306b\u5b9f\u88c5\u3067\u304d\u307e\u3059\u3002<\/p>\n\n\n\n<h4 class=\"wp-block-heading\" id=\"i-23\">\u57fa\u672c\u7684\u306a1\u5bfe\u591a\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=\"\">\/\/ User\u30e2\u30c7\u30eb\uff1a1\u4eba\u306e\u30e6\u30fc\u30b6\u30fc\u306f\u8907\u6570\u306e\u6295\u7a3f\u3092\u6301\u3064\nclass User extends Model\n{\n    public function posts()\n    {\n        return $this-&gt;hasMany(Post::class);\n    }\n\n    \/\/ \u30ea\u30ec\u30fc\u30b7\u30e7\u30f3\u5229\u7528\u4f8b\n    public function getLatestPosts($limit = 5)\n    {\n        return $this-&gt;posts()\n                    -&gt;latest()\n                    -&gt;take($limit)\n                    -&gt;get();\n    }\n}\n\n\/\/ Post\u30e2\u30c7\u30eb\uff1a1\u3064\u306e\u6295\u7a3f\u306f1\u4eba\u306e\u30e6\u30fc\u30b6\u30fc\u306b\u5c5e\u3059\u308b\nclass Post extends Model\n{\n    public function user()\n    {\n        return $this-&gt;belongsTo(User::class);\n    }\n\n    \/\/ \u30ab\u30b9\u30bf\u30e0\u5916\u90e8\u30ad\u30fc\u306e\u6307\u5b9a\u4f8b\n    public function category()\n    {\n        return $this-&gt;belongsTo(Category::class, 'category_id', 'id');\n    }\n}<\/pre>\n\n\n\n<h4 class=\"wp-block-heading\" id=\"i-24\">\u9ad8\u5ea6\u306a\u30ea\u30ec\u30fc\u30b7\u30e7\u30f3\u64cd\u4f5c<\/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 Department extends Model\n{\n    \/\/ \u90e8\u7f72\u306f\u8907\u6570\u306e\u5f93\u696d\u54e1\u3092\u6301\u3064\n    public function employees()\n    {\n        return $this-&gt;hasMany(Employee::class)\n                    -&gt;where('status', 'active')  \/\/ \u30c7\u30d5\u30a9\u30eb\u30c8\u306e\u6761\u4ef6\n                    -&gt;withDefault([              \/\/ \u95a2\u9023\u304c\u306a\u3044\u5834\u5408\u306e\u30c7\u30d5\u30a9\u30eb\u30c8\u5024\n                        'name' =&gt; 'No Employee'\n                    ]);\n    }\n\n    \/\/ \u7d66\u4e0e\u8a08\u7b97\u306e\u4f8b\n    public function getTotalSalaryAttribute()\n    {\n        return $this-&gt;employees()\n                    -&gt;sum('salary');\n    }\n\n    \/\/ \u5f93\u696d\u54e1\u6570\u306e\u53d6\u5f97\uff08\u30ad\u30e3\u30c3\u30b7\u30e5\u5229\u7528\u4f8b\uff09\n    public function getActiveEmployeeCountAttribute()\n    {\n        return cache()-&gt;remember(\n            \"department_{$this-&gt;id}_employee_count\",\n            now()-&gt;addHours(1),\n            fn() =&gt; $this-&gt;employees()-&gt;count()\n        );\n    }\n}<\/pre>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"i-25\">\u591a\u5bfe\u591a\u30ea\u30ec\u30fc\u30b7\u30e7\u30f3\u3068\u30d4\u30dc\u30c3\u30c8\u30c6\u30fc\u30d6\u30eb\u306e\u4f7f\u3044\u65b9<\/h3>\n\n\n\n<p>\u591a\u5bfe\u591a\u95a2\u4fc2\u306f\u3001\u4e2d\u9593\u30c6\u30fc\u30d6\u30eb\uff08\u30d4\u30dc\u30c3\u30c8\u30c6\u30fc\u30d6\u30eb\uff09\u3092\u4ecb\u3057\u3066\u4e8c\u3064\u306e\u30e2\u30c7\u30eb\u3092\u95a2\u9023\u4ed8\u3051\u307e\u3059\u3002<\/p>\n\n\n\n<h4 class=\"wp-block-heading\" id=\"i-26\">\u57fa\u672c\u7684\u306a\u591a\u5bfe\u591a\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 Course extends Model\n{\n    public function students()\n    {\n        return $this-&gt;belongsToMany(Student::class)\n                    -&gt;withTimestamps()           \/\/ \u4e2d\u9593\u30c6\u30fc\u30d6\u30eb\u306e\u30bf\u30a4\u30e0\u30b9\u30bf\u30f3\u30d7\n                    -&gt;withPivot('grade', 'note') \/\/ \u8ffd\u52a0\u306e\u30d4\u30dc\u30c3\u30c8\u30c7\u30fc\u30bf\n                    -&gt;using(CourseStudent::class); \/\/ \u30ab\u30b9\u30bf\u30e0\u30d4\u30dc\u30c3\u30c8\u30e2\u30c7\u30eb\n    }\n}\n\n\/\/ \u30ab\u30b9\u30bf\u30e0\u30d4\u30dc\u30c3\u30c8\u30e2\u30c7\u30eb\u306e\u5b9f\u88c5\nclass CourseStudent extends Pivot\n{\n    \/\/ \u30d4\u30dc\u30c3\u30c8\u30e2\u30c7\u30eb\u3067\u306e\u30a4\u30d9\u30f3\u30c8\u51e6\u7406\n    protected static function boot()\n    {\n        parent::boot();\n\n        static::created(function ($pivot) {\n            Log::info(\"\u751f\u5f92\u304c\u8b1b\u5ea7\u306b\u767b\u9332\u3055\u308c\u307e\u3057\u305f\", [\n                'student_id' =&gt; $pivot-&gt;student_id,\n                'course_id' =&gt; $pivot-&gt;course_id\n            ]);\n        });\n    }\n\n    \/\/ \u6210\u7e3e\u306b\u95a2\u3059\u308b\u30a2\u30af\u30bb\u30b5\n    public function getGradeTextAttribute()\n    {\n        return match($this-&gt;grade) {\n            'A' =&gt; '\u512a\u79c0',\n            'B' =&gt; '\u826f\u597d',\n            'C' =&gt; '\u666e\u901a',\n            default =&gt; '\u8981\u52aa\u529b'\n        };\n    }\n}<\/pre>\n\n\n\n<h4 class=\"wp-block-heading\" id=\"i-27\">\u9ad8\u5ea6\u306a\u591a\u5bfe\u591a\u30ea\u30ec\u30fc\u30b7\u30e7\u30f3\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=\"\">class Project extends Model\n{\n    public function tasks()\n    {\n        return $this-&gt;belongsToMany(Task::class)\n                    -&gt;as('assignment')           \/\/ \u30ea\u30ec\u30fc\u30b7\u30e7\u30f3\u540d\u306e\u30ab\u30b9\u30bf\u30de\u30a4\u30ba\n                    -&gt;withPivot([\n                        'assigned_at',\n                        'priority',\n                        'status'\n                    ])\n                    -&gt;wherePivot('status', '!=', 'completed')  \/\/ \u6761\u4ef6\u4ed8\u304d\u30ea\u30ec\u30fc\u30b7\u30e7\u30f3\n                    -&gt;orderByPivot('priority', 'desc');        \/\/ \u30d4\u30dc\u30c3\u30c8\u30c7\u30fc\u30bf\u3067\u30bd\u30fc\u30c8\n    }\n\n    \/\/ \u512a\u5148\u5ea6\u306e\u9ad8\u3044\u30bf\u30b9\u30af\u306e\u53d6\u5f97\n    public function getHighPriorityTasks()\n    {\n        return $this-&gt;tasks()\n                    -&gt;wherePivot('priority', 'high')\n                    -&gt;get();\n    }\n\n    \/\/ \u30bf\u30b9\u30af\u306e\u4e00\u62ec\u5272\u308a\u5f53\u3066\n    public function assignTasks(array $taskIds, array $attributes = [])\n    {\n        $pivotData = collect($taskIds)-&gt;mapWithKeys(function ($taskId) use ($attributes) {\n            return [$taskId =&gt; array_merge([\n                'assigned_at' =&gt; now(),\n                'status' =&gt; 'pending'\n            ], $attributes)];\n        });\n\n        return $this-&gt;tasks()-&gt;attach($pivotData);\n    }\n}<\/pre>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"i-28\">\u30dd\u30ea\u30e2\u30fc\u30d5\u30a3\u30c3\u30af\u30ea\u30ec\u30fc\u30b7\u30e7\u30f3\u306e\u5b9f\u88c5\u30c6\u30af\u30cb\u30c3\u30af<\/h3>\n\n\n\n<p>\u30dd\u30ea\u30e2\u30fc\u30d5\u30a3\u30c3\u30af\u30ea\u30ec\u30fc\u30b7\u30e7\u30f3\u306f\u3001\u4e00\u3064\u306e\u30e2\u30c7\u30eb\u304c\u8907\u6570\u306e\u7570\u306a\u308b\u30e2\u30c7\u30eb\u3068\u95a2\u9023\u3092\u6301\u3064\u5834\u5408\u306b\u4f7f\u7528\u3057\u307e\u3059\u3002<\/p>\n\n\n\n<h4 class=\"wp-block-heading\" id=\"i-29\">\u57fa\u672c\u7684\u306a\u30dd\u30ea\u30e2\u30fc\u30d5\u30a3\u30c3\u30af\u30ea\u30ec\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=\"\">class Image extends Model\n{\n    \/\/ \u753b\u50cf\u306f\u8907\u6570\u306e\u7570\u306a\u308b\u30e2\u30c7\u30eb\u306b\u6240\u5c5e\u53ef\u80fd\n    public function imageable()\n    {\n        return $this-&gt;morphTo();\n    }\n}\n\nclass Post extends Model\n{\n    \/\/ \u6295\u7a3f\u306f\u8907\u6570\u306e\u753b\u50cf\u3092\u6301\u3064\n    public function images()\n    {\n        return $this-&gt;morphMany(Image::class, 'imageable');\n    }\n}\n\nclass User extends Model\n{\n    \/\/ \u30e6\u30fc\u30b6\u30fc\u3082\u30d7\u30ed\u30d5\u30a3\u30fc\u30eb\u753b\u50cf\u3068\u3057\u3066\u753b\u50cf\u3092\u6301\u3064\n    public function profileImage()\n    {\n        return $this-&gt;morphOne(Image::class, 'imageable');\n    }\n}<\/pre>\n\n\n\n<h4 class=\"wp-block-heading\" id=\"i-30\">\u9ad8\u5ea6\u306a\u30dd\u30ea\u30e2\u30fc\u30d5\u30a3\u30c3\u30af\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    \/\/ \u30b3\u30e1\u30f3\u30c8\u306f\u4efb\u610f\u306e\u30e2\u30c7\u30eb\u306b\u5bfe\u3057\u3066\u4ed8\u3051\u3089\u308c\u308b\n    public function commentable()\n    {\n        return $this-&gt;morphTo();\n    }\n\n    \/\/ \u30b3\u30e1\u30f3\u30c8\u306b\u5bfe\u3059\u308b\u8fd4\u4fe1\uff08\u81ea\u5df1\u53c2\u7167\u30dd\u30ea\u30e2\u30fc\u30d5\u30a3\u30c3\u30af\uff09\n    public function replies()\n    {\n        return $this-&gt;morphMany(Comment::class, 'commentable');\n    }\n\n    \/\/ \u30cd\u30b9\u30c8\u3055\u308c\u305f\u8fd4\u4fe1\u3092\u53d6\u5f97\n    public function allReplies()\n    {\n        return $this-&gt;replies()-&gt;with('allReplies');\n    }\n}\n\n\/\/ \u30bf\u30b0\u4ed8\u3051\u53ef\u80fd\u306a\u30c8\u30ec\u30a4\u30c8\ntrait Taggable\n{\n    public function tags()\n    {\n        return $this-&gt;morphToMany(Tag::class, 'taggable')\n                    -&gt;withTimestamps();\n    }\n\n    \/\/ \u30bf\u30b0\u306e\u4e00\u62ec\u8a2d\u5b9a\n    public function syncTagsByName(array $tagNames)\n    {\n        $tags = collect($tagNames)-&gt;map(function ($name) {\n            return Tag::firstOrCreate(\n                ['name' =&gt; $name],\n                ['slug' =&gt; Str::slug($name)]\n            )-&gt;id;\n        });\n\n        return $this-&gt;tags()-&gt;sync($tags);\n    }\n}\n\n\/\/ \u30c8\u30ec\u30a4\u30c8\u306e\u4f7f\u7528\u4f8b\nclass Article extends Model\n{\n    use Taggable;\n\n    public function relatedArticles()\n    {\n        return Article::whereHas('tags', function ($query) {\n            $query-&gt;whereIn('id', $this-&gt;tags-&gt;pluck('id'));\n        })\n        -&gt;where('id', '!=', $this-&gt;id)\n        -&gt;limit(5)\n        -&gt;get();\n    }\n}<\/pre>\n\n\n\n<p>\u4ee5\u4e0a\u304c\u3001Laravel\u306b\u304a\u3051\u308b\u30c7\u30fc\u30bf\u30d9\u30fc\u30b9\u30ea\u30ec\u30fc\u30b7\u30e7\u30f3\u306e\u8a73\u7d30\u306a\u5b9f\u88c5\u65b9\u6cd5\u3067\u3059\u3002\u3053\u308c\u3089\u306e\u30c6\u30af\u30cb\u30c3\u30af\u3092\u9069\u5207\u306b\u7d44\u307f\u5408\u308f\u305b\u308b\u3053\u3068\u3067\u3001\u8907\u96d1\u306a\u30c7\u30fc\u30bf\u69cb\u9020\u3082\u52b9\u7387\u7684\u306b\u7ba1\u7406\u3067\u304d\u307e\u3059\u3002<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"i-31\">\u30af\u30a8\u30ea\u30d3\u30eb\u30c0\u3068\u30b9\u30b3\u30fc\u30d7\u306e\u5fdc\u7528\u30c6\u30af\u30cb\u30c3\u30af<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"i-32\">\u30ed\u30fc\u30ab\u30eb\u30b9\u30b3\u30fc\u30d7\u3067\u5171\u901a\u306e\u30af\u30a8\u30ea\u3092\u518d\u5229\u7528<\/h3>\n\n\n\n<p>\u30ed\u30fc\u30ab\u30eb\u30b9\u30b3\u30fc\u30d7\u3092\u6d3b\u7528\u3059\u308b\u3053\u3068\u3067\u3001\u983b\u7e41\u306b\u4f7f\u7528\u3059\u308b\u30af\u30a8\u30ea\u6761\u4ef6\u3092\u518d\u5229\u7528\u53ef\u80fd\u306a\u5f62\u3067\u5b9a\u7fa9\u3067\u304d\u307e\u3059\u3002<\/p>\n\n\n\n<h4 class=\"wp-block-heading\" id=\"i-33\">\u57fa\u672c\u7684\u306a\u30b9\u30b3\u30fc\u30d7\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 Post extends Model\n{\n    \/\/ \u516c\u958b\u6e08\u307f\u306e\u6295\u7a3f\u3092\u53d6\u5f97\n    public function scopePublished($query)\n    {\n        return $query-&gt;where('status', 'published')\n                     -&gt;where('published_at', '&lt;=', now());\n    }\n\n    \/\/ \u7279\u5b9a\u306e\u30ab\u30c6\u30b4\u30ea\u30fc\u306e\u6295\u7a3f\u3092\u53d6\u5f97\n    public function scopeInCategory($query, $category)\n    {\n        return $query-&gt;where('category_id', $category instanceof Category \n            ? $category-&gt;id \n            : $category\n        );\n    }\n\n    \/\/ \u4eba\u6c17\u306e\u6295\u7a3f\u3092\u53d6\u5f97\n    public function scopePopular($query, $minViews = 1000)\n    {\n        return $query-&gt;where('view_count', '&gt;=', $minViews)\n                     -&gt;orderBy('view_count', 'desc');\n    }\n}\n\n\/\/ \u30b9\u30b3\u30fc\u30d7\u306e\u4f7f\u7528\u4f8b\n$popularPosts = Post::published()\n    -&gt;inCategory('technology')\n    -&gt;popular(500)\n    -&gt;get();<\/pre>\n\n\n\n<h4 class=\"wp-block-heading\" id=\"i-34\">\u52d5\u7684\u306a\u30d1\u30e9\u30e1\u30fc\u30bf\u3092\u6301\u3064\u30b9\u30b3\u30fc\u30d7<\/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    \/\/ \u65e5\u4ed8\u7bc4\u56f2\u306b\u3088\u308b\u7d5e\u308a\u8fbc\u307f\n    public function scopeDateBetween($query, $startDate, $endDate = null)\n    {\n        $query-&gt;where('created_at', '&gt;=', Carbon::parse($startDate));\n\n        if ($endDate) {\n            $query-&gt;where('created_at', '&lt;=', Carbon::parse($endDate));\n        }\n\n        return $query;\n    }\n\n    \/\/ \u91d1\u984d\u7bc4\u56f2\u306b\u3088\u308b\u7d5e\u308a\u8fbc\u307f\n    public function scopePriceRange($query, $min = null, $max = null)\n    {\n        if ($min !== null) {\n            $query-&gt;where('total_amount', '&gt;=', $min);\n        }\n\n        if ($max !== null) {\n            $query-&gt;where('total_amount', '&lt;=', $max);\n        }\n\n        return $query;\n    }\n\n    \/\/ \u8907\u6570\u6761\u4ef6\u3092\u7d44\u307f\u5408\u308f\u305b\u305f\u30b9\u30b3\u30fc\u30d7\n    public function scopeHighValueRecent($query, $days = 30, $minAmount = 100000)\n    {\n        return $query-&gt;dateBetween(\n            now()-&gt;subDays($days),\n            now()\n        )-&gt;priceRange($minAmount);\n    }\n}<\/pre>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"i-35\">\u30b0\u30ed\u30fc\u30d0\u30eb\u30b9\u30b3\u30fc\u30d7\u3067\u30e2\u30c7\u30eb\u5168\u4f53\u306e\u52d5\u4f5c\u3092\u30ab\u30b9\u30bf\u30de\u30a4\u30ba<\/h3>\n\n\n\n<p>\u30b0\u30ed\u30fc\u30d0\u30eb\u30b9\u30b3\u30fc\u30d7\u3092\u4f7f\u7528\u3059\u308b\u3053\u3068\u3067\u3001\u30e2\u30c7\u30eb\u306e\u3059\u3079\u3066\u306e\u30af\u30a8\u30ea\u306b\u7279\u5b9a\u306e\u6761\u4ef6\u3092\u9069\u7528\u3067\u304d\u307e\u3059\u3002<\/p>\n\n\n\n<h4 class=\"wp-block-heading\" id=\"i-36\">\u30af\u30e9\u30b9\u30d9\u30fc\u30b9\u306e\u30b0\u30ed\u30fc\u30d0\u30eb\u30b9\u30b3\u30fc\u30d7<\/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=\"\">\/\/ \u30b0\u30ed\u30fc\u30d0\u30eb\u30b9\u30b3\u30fc\u30d7\u30af\u30e9\u30b9\u306e\u5b9a\u7fa9\nclass ActiveScope implements Scope\n{\n    public function apply(Builder $builder, Model $model)\n    {\n        $builder-&gt;where('is_active', true);\n    }\n\n    \/\/ \u30b9\u30b3\u30fc\u30d7\u306e\u9664\u5916\u6761\u4ef6\n    public function extend(Builder $builder)\n    {\n        $builder-&gt;macro('withInactive', function (Builder $builder) {\n            return $builder-&gt;withoutGlobalScope($this);\n        });\n\n        $builder-&gt;macro('onlyInactive', function (Builder $builder) {\n            return $builder-&gt;withoutGlobalScope($this)\n                          -&gt;where('is_active', false);\n        });\n    }\n}\n\n\/\/ \u30b0\u30ed\u30fc\u30d0\u30eb\u30b9\u30b3\u30fc\u30d7\u306e\u9069\u7528\nclass Product extends Model\n{\n    protected static function boot()\n    {\n        parent::boot();\n        static::addGlobalScope(new ActiveScope);\n    }\n}<\/pre>\n\n\n\n<h4 class=\"wp-block-heading\" id=\"i-37\">\u30af\u30ed\u30fc\u30b8\u30e3\u306b\u3088\u308b\u30b0\u30ed\u30fc\u30d0\u30eb\u30b9\u30b3\u30fc\u30d7<\/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 Document extends Model\n{\n    protected static function boot()\n    {\n        parent::boot();\n\n        \/\/ \u7279\u5b9a\u306e\u6761\u4ef6\u3067\u5e38\u306b\u30bd\u30fc\u30c8\n        static::addGlobalScope('ordered', function (Builder $builder) {\n            $builder-&gt;orderBy('priority', 'desc')\n                    -&gt;orderBy('created_at', 'desc');\n        });\n\n        \/\/ \u30c6\u30ca\u30f3\u30c8ID\u306b\u3088\u308b\u30d5\u30a3\u30eb\u30bf\u30ea\u30f3\u30b0\n        static::addGlobalScope('tenant', function (Builder $builder) {\n            if (auth()-&gt;check()) {\n                $builder-&gt;where('tenant_id', auth()-&gt;user()-&gt;tenant_id);\n            }\n        });\n    }\n\n    \/\/ \u30b0\u30ed\u30fc\u30d0\u30eb\u30b9\u30b3\u30fc\u30d7\u306e\u52d5\u7684\u306a\u9664\u5916\n    public static function allTenants()\n    {\n        return static::withoutGlobalScope('tenant');\n    }\n}<\/pre>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"i-38\">Eager\u30ed\u30fc\u30c7\u30a3\u30f3\u30b0\u3067\u30d1\u30d5\u30a9\u30fc\u30de\u30f3\u30b9\u3092\u6700\u9069\u5316<\/h3>\n\n\n\n<p>Eager\u30ed\u30fc\u30c7\u30a3\u30f3\u30b0\u3092\u9069\u5207\u306b\u4f7f\u7528\u3059\u308b\u3053\u3068\u3067\u3001N+1\u554f\u984c\u3092\u56de\u907f\u3057\u3001\u30a2\u30d7\u30ea\u30b1\u30fc\u30b7\u30e7\u30f3\u306e\u30d1\u30d5\u30a9\u30fc\u30de\u30f3\u30b9\u3092\u5411\u4e0a\u3055\u305b\u308b\u3053\u3068\u304c\u3067\u304d\u307e\u3059\u3002<\/p>\n\n\n\n<h4 class=\"wp-block-heading\" id=\"i-39\">\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=\"\">class BlogPost extends Model\n{\n    \/\/ \u30ea\u30ec\u30fc\u30b7\u30e7\u30f3\u306e\u5b9a\u7fa9\n    public function author()\n    {\n        return $this-&gt;belongsTo(User::class);\n    }\n\n    public function comments()\n    {\n        return $this-&gt;hasMany(Comment::class);\n    }\n\n    public function tags()\n    {\n        return $this-&gt;belongsToMany(Tag::class);\n    }\n\n    \/\/ Eager\u30ed\u30fc\u30c7\u30a3\u30f3\u30b0\u3092\u4f7f\u7528\u3057\u305f\u30b9\u30b3\u30fc\u30d7\n    public function scopeWithRelations($query)\n    {\n        return $query-&gt;with([\n            'author:id,name,email',  \/\/ \u5fc5\u8981\u306a\u30ab\u30e9\u30e0\u306e\u307f\u53d6\u5f97\n            'comments' =&gt; function ($query) {\n                $query-&gt;latest()-&gt;take(5);  \/\/ \u76f4\u8fd15\u4ef6\u306e\u30b3\u30e1\u30f3\u30c8\u306e\u307f\n            },\n            'tags:id,name'  \/\/ \u30bf\u30b0\u306e\u5fc5\u8981\u306a\u30ab\u30e9\u30e0\u306e\u307f\n        ]);\n    }\n}\n\n\/\/ \u52b9\u7387\u7684\u306a\u30c7\u30fc\u30bf\u53d6\u5f97\u4f8b\n$posts = BlogPost::withRelations()\n    -&gt;published()\n    -&gt;paginate(20);<\/pre>\n\n\n\n<h4 class=\"wp-block-heading\" id=\"i-40\">\u6761\u4ef6\u4ed8\u304dEager\u30ed\u30fc\u30c7\u30a3\u30f3\u30b0\u3068\u30ab\u30a6\u30f3\u30c8\u306e\u53d6\u5f97<\/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 Course extends Model\n{\n    public function scopeWithEnrollmentInfo($query)\n    {\n        return $query-&gt;withCount([\n            'students',  \/\/ \u5168\u751f\u5f92\u6570\n            'students as active_students_count' =&gt; function ($query) {\n                $query-&gt;where('status', 'active');\n            }\n        ])-&gt;with([\n            'students' =&gt; function ($query) {\n                $query-&gt;select('id', 'name', 'email')\n                      -&gt;where('status', 'active')\n                      -&gt;orderBy('name');\n            }\n        ]);\n    }\n\n    \/\/ \u9ad8\u5ea6\u306aEager\u30ed\u30fc\u30c7\u30a3\u30f3\u30b0\u306e\u4f8b\n    public function scopeWithDetailedStats($query)\n    {\n        return $query-&gt;withCount([\n            'students',\n            'lessons',\n            'assignments'\n        ])-&gt;withAvg('ratings', 'score')\n          -&gt;withSum('earnings', 'amount')\n          -&gt;with([\n              'lastLesson',\n              'upcomingLessons' =&gt; function ($query) {\n                  $query-&gt;where('start_date', '&gt;', now())\n                        -&gt;orderBy('start_date')\n                        -&gt;take(5);\n              }\n          ]);\n    }\n}<\/pre>\n\n\n\n<h4 class=\"wp-block-heading\" id=\"i-41\">\u30d1\u30d5\u30a9\u30fc\u30de\u30f3\u30b9\u6700\u9069\u5316\u306e\u30d9\u30b9\u30c8\u30d7\u30e9\u30af\u30c6\u30a3\u30b9<\/h4>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>\u9078\u629e\u7684\u306a\u30ab\u30e9\u30e0\u53d6\u5f97<\/strong><\/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=\"\">$users = User::select(['id', 'name', 'email'])\n    -&gt;with(['posts' =&gt; function ($query) {\n        $query-&gt;select(['id', 'user_id', 'title'])\n              -&gt;latest();\n    }])\n    -&gt;get();<\/pre>\n\n\n\n<ol start=\"2\" class=\"wp-block-list\">\n<li><strong>\u9045\u5ef6Eager\u30ed\u30fc\u30c7\u30a3\u30f3\u30b0<\/strong><\/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=\"\">\/\/ \u30b3\u30ec\u30af\u30b7\u30e7\u30f3\u53d6\u5f97\u5f8c\u306b\u5fc5\u8981\u306b\u5fdc\u3058\u3066\u30ea\u30ec\u30fc\u30b7\u30e7\u30f3\u3092\u30ed\u30fc\u30c9\n$posts = Post::all();\nif ($needComments) {\n    $posts-&gt;load(['comments' =&gt; function ($query) {\n        $query-&gt;latest();\n    }]);\n}<\/pre>\n\n\n\n<ol start=\"3\" class=\"wp-block-list\">\n<li><strong>\u30af\u30a8\u30ea\u306e\u30ad\u30e3\u30c3\u30b7\u30e5<\/strong><\/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 Category extends Model\n{\n    public function getCachedProductsAttribute()\n    {\n        return cache()-&gt;remember(\n            \"category_{$this-&gt;id}_products\",\n            now()-&gt;addHours(1),\n            fn() =&gt; $this-&gt;products()\n                        -&gt;with('tags')\n                        -&gt;active()\n                        -&gt;get()\n        );\n    }\n}<\/pre>\n\n\n\n<p>\u4ee5\u4e0a\u304c\u3001Laravel\u306b\u304a\u3051\u308b\u30af\u30a8\u30ea\u30d3\u30eb\u30c0\u3068\u30b9\u30b3\u30fc\u30d7\u306e\u5fdc\u7528\u30c6\u30af\u30cb\u30c3\u30af\u3067\u3059\u3002\u3053\u308c\u3089\u3092\u9069\u5207\u306b\u6d3b\u7528\u3059\u308b\u3053\u3068\u3067\u3001\u4fdd\u5b88\u6027\u306e\u9ad8\u3044\u52b9\u7387\u7684\u306a\u30c7\u30fc\u30bf\u30d9\u30fc\u30b9\u30a2\u30af\u30bb\u30b9\u3092\u5b9f\u73fe\u3067\u304d\u307e\u3059\u3002<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"i-42\">\u5b9f\u8df5\u7684\u306aLaravel\u30e2\u30c7\u30eb\u6d3b\u7528\u4e8b\u4f8b<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"i-43\">\u30bd\u30d5\u30c8\u30c7\u30ea\u30fc\u30c8\u3092\u4f7f\u3063\u305f\u8ad6\u7406\u524a\u9664\u306e\u5b9f\u88c5<\/h3>\n\n\n\n<p>\u30bd\u30d5\u30c8\u30c7\u30ea\u30fc\u30c8\u3092\u4f7f\u7528\u3059\u308b\u3053\u3068\u3067\u3001\u30c7\u30fc\u30bf\u3092\u7269\u7406\u7684\u306b\u524a\u9664\u305b\u305a\u306b\u3001\u524a\u9664\u6e08\u307f\u3068\u3057\u3066\u30de\u30fc\u30af\u3059\u308b\u3053\u3068\u304c\u3067\u304d\u307e\u3059\u3002\u3053\u308c\u306b\u3088\u308a\u3001\u30c7\u30fc\u30bf\u306e\u5fa9\u5143\u3084\u30a2\u30fc\u30ab\u30a4\u30d6\u304c\u53ef\u80fd\u306b\u306a\u308a\u307e\u3059\u3002<\/p>\n\n\n\n<h4 class=\"wp-block-heading\" id=\"i-44\">\u57fa\u672c\u7684\u306a\u30bd\u30d5\u30c8\u30c7\u30ea\u30fc\u30c8\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 Customer extends Model\n{\n    use SoftDeletes;\n\n    \/\/ \u524a\u9664\u65e5\u6642\u306e\u30ab\u30b9\u30bf\u30de\u30a4\u30ba\n    const DELETED_AT = 'archived_at';\n\n    \/\/ \u524a\u9664\u6642\u306b\u95a2\u9023\u30ec\u30b3\u30fc\u30c9\u3082\u542b\u3081\u3066\u524a\u9664\n    protected $cascadeDeletes = ['orders', 'addresses'];\n\n    \/\/ \u524a\u9664\u6642\u306e\u30a4\u30d9\u30f3\u30c8\u30cf\u30f3\u30c9\u30ea\u30f3\u30b0\n    protected static function boot()\n    {\n        parent::boot();\n\n        static::deleting(function ($customer) {\n            \/\/ \u524a\u9664\u7406\u7531\u306e\u8a18\u9332\n            $customer-&gt;deleteHistory()-&gt;create([\n                'reason' =&gt; request('reason'),\n                'deleted_by' =&gt; auth()-&gt;id()\n            ]);\n\n            \/\/ \u95a2\u9023\u3059\u308b\u6ce8\u6587\u3092\u30a2\u30fc\u30ab\u30a4\u30d6\n            $customer-&gt;orders()-&gt;each(function ($order) {\n                $order-&gt;archive();\n            });\n        });\n    }\n\n    \/\/ \u5fa9\u5143\u6642\u306e\u51e6\u7406\n    public function restore()\n    {\n        \/\/ \u30c8\u30e9\u30f3\u30b6\u30af\u30b7\u30e7\u30f3\u3067\u4e00\u62ec\u51e6\u7406\n        return DB::transaction(function () {\n            $this-&gt;orders()-&gt;restore();\n            parent::restore();\n\n            event(new CustomerRestored($this));\n\n            return true;\n        });\n    }\n}<\/pre>\n\n\n\n<h4 class=\"wp-block-heading\" id=\"i-45\">\u9ad8\u5ea6\u306a\u30bd\u30d5\u30c8\u30c7\u30ea\u30fc\u30c8\u64cd\u4f5c<\/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 Project extends Model\n{\n    use SoftDeletes;\n\n    \/\/ \u30b9\u30c6\u30fc\u30bf\u30b9\u7ba1\u7406\u3068\u7d44\u307f\u5408\u308f\u305b\u305f\u524a\u9664\n    public function archive()\n    {\n        return DB::transaction(function () {\n            $this-&gt;update(['status' =&gt; 'archived']);\n            $this-&gt;delete();\n\n            \/\/ \u95a2\u9023\u30bf\u30b9\u30af\u306e\u30a2\u30fc\u30ab\u30a4\u30d6\n            $this-&gt;tasks()-&gt;each-&gt;archive();\n\n            \/\/ \u30a2\u30fc\u30ab\u30a4\u30d6\u901a\u77e5\n            $this-&gt;team-&gt;notify(new ProjectArchived($this));\n        });\n    }\n\n    \/\/ \u6761\u4ef6\u4ed8\u304d\u5fa9\u5143\n    public function restoreWithValidation()\n    {\n        if ($this-&gt;canBeRestored()) {\n            return $this-&gt;restore();\n        }\n\n        throw new ProjectRestoreException(\n            '\u3053\u306e\u30d7\u30ed\u30b8\u30a7\u30af\u30c8\u306f\u5fa9\u5143\u3067\u304d\u307e\u305b\u3093'\n        );\n    }\n\n    \/\/ \u30b9\u30b3\u30fc\u30d7\u3068\u306e\u7d44\u307f\u5408\u308f\u305b\n    public function scopeRecentlyDeleted($query)\n    {\n        return $query-&gt;onlyTrashed()\n                    -&gt;where('deleted_at', '&gt;', now()-&gt;subDays(30));\n    }\n}<\/pre>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"i-46\">\u30e2\u30c7\u30eb\u30d5\u30a1\u30af\u30c8\u30ea\u3068\u30b7\u30fc\u30c0\u30fc\u3067\u30c6\u30b9\u30c8\u30c7\u30fc\u30bf\u3092\u751f\u6210<\/h3>\n\n\n\n<p>\u30c6\u30b9\u30c8\u3084\u958b\u767a\u74b0\u5883\u3067\u306e\u30c7\u30fc\u30bf\u751f\u6210\u3092\u52b9\u7387\u5316\u3059\u308b\u305f\u3081\u306b\u3001\u30d5\u30a1\u30af\u30c8\u30ea\u3068\u30b7\u30fc\u30c0\u30fc\u3092\u6d3b\u7528\u3057\u307e\u3059\u3002<\/p>\n\n\n\n<h4 class=\"wp-block-heading\" id=\"i-47\">\u9ad8\u5ea6\u306a\u30d5\u30a1\u30af\u30c8\u30ea\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 UserFactory extends Factory\n{\n    protected $model = User::class;\n\n    public function definition()\n    {\n        return [\n            'name' =&gt; $this-&gt;faker-&gt;name(),\n            'email' =&gt; $this-&gt;faker-&gt;unique()-&gt;safeEmail(),\n            'password' =&gt; Hash::make('password'),\n            'role' =&gt; $this-&gt;faker-&gt;randomElement(['user', 'editor', 'admin']),\n            'settings' =&gt; $this-&gt;generateSettings(),\n        ];\n    }\n\n    \/\/ \u30ab\u30b9\u30bf\u30e0\u72b6\u614b\u306e\u5b9a\u7fa9\n    public function admin()\n    {\n        return $this-&gt;state(function (array $attributes) {\n            return [\n                'role' =&gt; 'admin',\n                'is_super_admin' =&gt; true,\n                'permissions' =&gt; ['*'],\n            ];\n        });\n    }\n\n    \/\/ \u95a2\u9023\u30c7\u30fc\u30bf\u3092\u542b\u3080\u30d5\u30a1\u30af\u30c8\u30ea\n    public function withProfile()\n    {\n        return $this-&gt;has(\n            Profile::factory()\n                -&gt;state(function (array $attributes, User $user) {\n                    return ['user_id' =&gt; $user-&gt;id];\n                })\n        );\n    }\n\n    \/\/ \u30d7\u30e9\u30a4\u30d9\u30fc\u30c8\u30d8\u30eb\u30d1\u30fc\u30e1\u30bd\u30c3\u30c9\n    private function generateSettings()\n    {\n        return [\n            'theme' =&gt; $this-&gt;faker-&gt;randomElement(['light', 'dark']),\n            'notifications' =&gt; [\n                'email' =&gt; true,\n                'push' =&gt; false\n            ],\n            'language' =&gt; $this-&gt;faker-&gt;languageCode\n        ];\n    }\n}\n\n\/\/ \u30b7\u30fc\u30c0\u30fc\u3067\u306e\u4f7f\u7528\u4f8b\nclass DatabaseSeeder extends Seeder\n{\n    public function run()\n    {\n        User::factory()\n            -&gt;count(10)\n            -&gt;admin()\n            -&gt;withProfile()\n            -&gt;has(Post::factory()-&gt;count(3))\n            -&gt;create();\n    }\n}<\/pre>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"i-48\">API\u30ea\u30bd\u30fc\u30b9\u3068\u30e2\u30c7\u30eb\u306e\u9023\u643a\u65b9\u6cd5<\/h3>\n\n\n\n<p>API\u30ea\u30bd\u30fc\u30b9\u3092\u4f7f\u7528\u3059\u308b\u3053\u3068\u3067\u3001\u30e2\u30c7\u30eb\u30c7\u30fc\u30bf\u3092\u4e00\u8cab\u3057\u305f\u5f62\u5f0f\u3067JSON\u5fdc\u7b54\u306b\u5909\u63db\u3067\u304d\u307e\u3059\u3002<\/p>\n\n\n\n<h4 class=\"wp-block-heading\" id=\"i-49\">\u57fa\u672c\u7684\u306aAPI\u30ea\u30bd\u30fc\u30b9\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 ProductResource extends JsonResource\n{\n    public function toArray($request)\n    {\n        return [\n            'id' =&gt; $this-&gt;id,\n            'name' =&gt; $this-&gt;name,\n            'price' =&gt; [\n                'amount' =&gt; $this-&gt;price,\n                'currency' =&gt; 'JPY',\n                'formatted' =&gt; \"\u00a5\" . number_format($this-&gt;price)\n            ],\n            'category' =&gt; new CategoryResource($this-&gt;whenLoaded('category')),\n            'variations' =&gt; ProductVariationResource::collection(\n                $this-&gt;whenLoaded('variations')\n            ),\n            'created_at' =&gt; $this-&gt;created_at-&gt;toIso8601String(),\n            'updated_at' =&gt; $this-&gt;updated_at-&gt;toIso8601String(),\n        ];\n    }\n\n    \/\/ \u8ffd\u52a0\u306e\u30e1\u30bf\u30c7\u30fc\u30bf\n    public function with($request)\n    {\n        return [\n            'meta' =&gt; [\n                'available_stock' =&gt; $this-&gt;calculateAvailableStock(),\n                'ratings_average' =&gt; $this-&gt;ratings_avg\n            ]\n        ];\n    }\n}<\/pre>\n\n\n\n<h4 class=\"wp-block-heading\" id=\"i-50\">\u9ad8\u5ea6\u306aAPI\u30ea\u30bd\u30fc\u30b9\u6d3b\u7528\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=\"\">class OrderResource extends JsonResource\n{\n    public function toArray($request)\n    {\n        \/\/ \u8a8d\u53ef\u30c1\u30a7\u30c3\u30af\u306e\u7d71\u5408\n        $this-&gt;authorizeResource($request);\n\n        return [\n            'id' =&gt; $this-&gt;id,\n            'number' =&gt; $this-&gt;number,\n            'status' =&gt; $this-&gt;status,\n            \/\/ \u6761\u4ef6\u4ed8\u304d\u306e\u5c5e\u6027\n            'can_cancel' =&gt; $this-&gt;when(\n                $this-&gt;canBeCancelled(),\n                true\n            ),\n            \/\/ \u95a2\u9023\u30ea\u30bd\u30fc\u30b9\n            'customer' =&gt; new CustomerResource($this-&gt;whenLoaded('customer')),\n            'items' =&gt; OrderItemResource::collection($this-&gt;whenLoaded('items')),\n            \/\/ \u8a08\u7b97\u6e08\u307f\u5c5e\u6027\n            'totals' =&gt; [\n                'subtotal' =&gt; $this-&gt;calculateSubtotal(),\n                'tax' =&gt; $this-&gt;calculateTax(),\n                'total' =&gt; $this-&gt;calculateTotal()\n            ],\n            \/\/ \u6761\u4ef6\u4ed8\u304d\u30ea\u30ec\u30fc\u30b7\u30e7\u30f3\n            'payment' =&gt; $this-&gt;when(\n                $request-&gt;user()-&gt;can('view-payments'),\n                new PaymentResource($this-&gt;whenLoaded('payment'))\n            ),\n            \/\/ \u30ab\u30b9\u30bf\u30e0\u30d5\u30a9\u30fc\u30de\u30c3\u30c8\n            'dates' =&gt; [\n                'ordered' =&gt; $this-&gt;created_at-&gt;format('Y-m-d H:i:s'),\n                'shipped' =&gt; $this-&gt;when(\n                    $this-&gt;shipped_at,\n                    fn() =&gt; $this-&gt;shipped_at-&gt;format('Y-m-d H:i:s')\n                )\n            ]\n        ];\n    }\n\n    \/\/ \u8a8d\u53ef\u30c1\u30a7\u30c3\u30af\n    protected function authorizeResource($request)\n    {\n        if ($request-&gt;user()-&gt;cannot('view', $this-&gt;resource)) {\n            throw new AuthorizationException(\n                '\u3053\u306e\u30aa\u30fc\u30c0\u30fc\u306e\u95b2\u89a7\u6a29\u9650\u304c\u3042\u308a\u307e\u305b\u3093'\n            );\n        }\n    }\n\n    \/\/ \u30ec\u30b9\u30dd\u30f3\u30b9\u306e\u30ab\u30b9\u30bf\u30de\u30a4\u30ba\n    public function withResponse($request, $response)\n    {\n        $response-&gt;header('X-Order-ID', $this-&gt;id);\n\n        if ($this-&gt;status === 'completed') {\n            $response-&gt;header('Cache-Control', 'public, max-age=3600');\n        }\n    }\n}<\/pre>\n\n\n\n<p>\u4ee5\u4e0a\u304c\u3001Laravel\u30e2\u30c7\u30eb\u306e\u5b9f\u8df5\u7684\u306a\u6d3b\u7528\u4e8b\u4f8b\u3067\u3059\u3002\u3053\u308c\u3089\u306e\u30c6\u30af\u30cb\u30c3\u30af\u3092\u9069\u5207\u306b\u7d44\u307f\u5408\u308f\u305b\u308b\u3053\u3068\u3067\u3001\u5805\u7262\u3067\u4fdd\u5b88\u6027\u306e\u9ad8\u3044\u30a2\u30d7\u30ea\u30b1\u30fc\u30b7\u30e7\u30f3\u3092\u69cb\u7bc9\u3067\u304d\u307e\u3059\u3002<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"i-51\">Laravel\u30e2\u30c7\u30eb\u306e\u30c8\u30e9\u30d6\u30eb\u30b7\u30e5\u30fc\u30c6\u30a3\u30f3\u30b0<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"i-52\">\u3088\u304f\u3042\u308bN+1\u554f\u984c\u306e\u89e3\u6c7a\u65b9\u6cd5<\/h3>\n\n\n\n<p>N+1\u554f\u984c\u306f\u3001\u30c7\u30fc\u30bf\u30d9\u30fc\u30b9\u30af\u30a8\u30ea\u306e\u5b9f\u884c\u56de\u6570\u304c\u4e0d\u5fc5\u8981\u306b\u5897\u52a0\u3057\u3066\u3057\u307e\u3046\u4e00\u822c\u7684\u306a\u554f\u984c\u3067\u3059\u3002\u9069\u5207\u306a\u5bfe\u7b56\u3092\u8b1b\u3058\u308b\u3053\u3068\u3067\u3001\u30a2\u30d7\u30ea\u30b1\u30fc\u30b7\u30e7\u30f3\u306e\u30d1\u30d5\u30a9\u30fc\u30de\u30f3\u30b9\u3092\u5927\u5e45\u306b\u6539\u5584\u3067\u304d\u307e\u3059\u3002<\/p>\n\n\n\n<h4 class=\"wp-block-heading\" id=\"i-53\">N+1\u554f\u984c\u306e\u691c\u51fa\u3068\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=\"\">class PostController extends Controller\n{\n    public function index()\n    {\n        \/\/ \u554f\u984c\u306e\u3042\u308b\u30b3\u30fc\u30c9\n        $posts = Post::all();  \/\/ 1\u56de\u76ee\u306e\u30af\u30a8\u30ea\n        foreach ($posts as $post) {\n            $post-&gt;author-&gt;name;  \/\/ \u5404\u6295\u7a3f\u306b\u5bfe\u3057\u3066\u8ffd\u52a0\u30af\u30a8\u30ea\u304c\u767a\u751f\n        }\n\n        \/\/ \u6539\u5584\u5f8c\u306e\u30b3\u30fc\u30c9\n        $posts = Post::with('author')-&gt;get();  \/\/ Eager\u30ed\u30fc\u30c7\u30a3\u30f3\u30b0\u3092\u4f7f\u7528\n\n        \/\/ \u3055\u3089\u306b\u6700\u9069\u5316: \u5fc5\u8981\u306a\u30ab\u30e9\u30e0\u306e\u307f\u53d6\u5f97\n        $posts = Post::select(['id', 'title', 'author_id'])\n            -&gt;with(['author' =&gt; function ($query) {\n                $query-&gt;select(['id', 'name']);\n            }])\n            -&gt;get();\n    }\n}\n\n\/\/ \u30af\u30a8\u30ea\u30ed\u30b0\u3092\u4f7f\u7528\u3057\u305fN+1\u554f\u984c\u306e\u691c\u51fa\nclass QueryLogger\n{\n    public static function enable()\n    {\n        DB::listen(function ($query) {\n            $sql = $query-&gt;sql;\n            $bindings = $query-&gt;bindings;\n            $time = $query-&gt;time;\n\n            Log::info('SQL', [\n                'sql' =&gt; $sql,\n                'bindings' =&gt; $bindings,\n                'time' =&gt; $time\n            ]);\n        });\n    }\n}<\/pre>\n\n\n\n<h4 class=\"wp-block-heading\" id=\"i-54\">\u9ad8\u5ea6\u306aN+1\u5bfe\u7b56<\/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\u3067Eager\u30ed\u30fc\u30c9\u3059\u308b\u30ea\u30ec\u30fc\u30b7\u30e7\u30f3\n    protected $with = ['author'];\n\n    \/\/ \u6761\u4ef6\u4ed8\u304dEager\u30ed\u30fc\u30c7\u30a3\u30f3\u30b0\n    public function scopeWithRelationsBasedOnRequest($query, Request $request)\n    {\n        if ($request-&gt;includeComments) {\n            $query-&gt;with('comments');\n        }\n\n        if ($request-&gt;includeTags) {\n            $query-&gt;with('tags:id,name');\n        }\n\n        return $query;\n    }\n\n    \/\/ \u30b5\u30d6\u30af\u30a8\u30ea\u3092\u4f7f\u7528\u3057\u305f\u6700\u9069\u5316\n    public function scopeWithLatestCommentCount($query)\n    {\n        return $query-&gt;withCount([\n            'comments' =&gt; function ($query) {\n                $query-&gt;where('created_at', '&gt;', now()-&gt;subDays(7));\n            }\n        ]);\n    }\n}<\/pre>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"i-55\">\u5927\u898f\u6a21\u30c7\u30fc\u30bf\u51e6\u7406\u6642\u306e\u30e1\u30e2\u30ea\u7ba1\u7406\u30c6\u30af\u30cb\u30c3\u30af<\/h3>\n\n\n\n<p>\u5927\u91cf\u306e\u30c7\u30fc\u30bf\u3092\u51e6\u7406\u3059\u308b\u969b\u306f\u3001\u30e1\u30e2\u30ea\u4f7f\u7528\u91cf\u306b\u6ce8\u610f\u3092\u6255\u3046\u5fc5\u8981\u304c\u3042\u308a\u307e\u3059\u3002<\/p>\n\n\n\n<h4 class=\"wp-block-heading\" id=\"i-56\">\u30c1\u30e3\u30f3\u30af\u51e6\u7406\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 DataProcessor\n{\n    \/\/ \u30c1\u30e3\u30f3\u30af\u51e6\u7406\u306b\u3088\u308b\u5927\u91cf\u30c7\u30fc\u30bf\u306e\u53d6\u308a\u6271\u3044\n    public function processLargeDataset()\n    {\n        User::chunk(1000, function ($users) {\n            foreach ($users as $user) {\n                $this-&gt;processUser($user);\n            }\n        });\n    }\n\n    \/\/ LazyCollection\u3092\u4f7f\u7528\u3057\u305f\u52b9\u7387\u7684\u306a\u51e6\u7406\n    public function processUsingLazy()\n    {\n        User::lazy()-&gt;each(function ($user) {\n            $this-&gt;processUser($user);\n        });\n    }\n\n    \/\/ \u30d0\u30c3\u30c1\u51e6\u7406\u306e\u5b9f\u88c5\n    public function processBatch()\n    {\n        User::query()\n            -&gt;where('status', 'pending')\n            -&gt;chunkById(500, function ($users) {\n                $userIds = $users-&gt;pluck('id')-&gt;toArray();\n\n                \/\/ \u30d0\u30c3\u30c1\u51e6\u7406\n                DB::transaction(function () use ($userIds) {\n                    User::whereIn('id', $userIds)\n                        -&gt;update(['status' =&gt; 'processed']);\n\n                    \/\/ \u95a2\u9023\u51e6\u7406\n                    $this-&gt;processRelatedData($userIds);\n                });\n            });\n    }\n}\n\n\/\/ \u30ab\u30b9\u30bf\u30e0\u30d0\u30c3\u30c1\u51e6\u7406\u306e\u5b9f\u88c5\nclass BatchProcessor\n{\n    protected $batchSize = 1000;\n    protected $processedCount = 0;\n\n    public function process()\n    {\n        return DB::transaction(function () {\n            Order::where('status', 'pending')\n                -&gt;orderBy('id')\n                -&gt;chunk($this-&gt;batchSize, function ($orders) {\n                    foreach ($orders as $order) {\n                        $this-&gt;processOrder($order);\n                        $this-&gt;processedCount++;\n\n                        if ($this-&gt;shouldCommit()) {\n                            DB::commit();\n                            DB::beginTransaction();\n                        }\n                    }\n                });\n\n            return $this-&gt;processedCount;\n        });\n    }\n\n    protected function shouldCommit()\n    {\n        return $this-&gt;processedCount % ($this-&gt;batchSize \/ 4) === 0;\n    }\n}<\/pre>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"i-57\">\u30e2\u30c7\u30eb\u30a4\u30d9\u30f3\u30c8\u306e\u30c7\u30d0\u30c3\u30b0\u65b9\u6cd5<\/h3>\n\n\n\n<p>\u30e2\u30c7\u30eb\u30a4\u30d9\u30f3\u30c8\u306e\u52d5\u4f5c\u3092\u7406\u89e3\u3057\u3001\u9069\u5207\u306b\u30c7\u30d0\u30c3\u30b0\u3059\u308b\u3053\u3068\u306f\u91cd\u8981\u3067\u3059\u3002<\/p>\n\n\n\n<h4 class=\"wp-block-heading\" id=\"i-58\">\u30a4\u30d9\u30f3\u30c8\u306e\u30c7\u30d0\u30c3\u30b0\u3068\u76e3\u8996<\/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 User extends Model\n{\n    \/\/ \u30a4\u30d9\u30f3\u30c8\u306e\u30c7\u30d0\u30c3\u30b0\u7528\u30c8\u30ec\u30a4\u30c8\n    use LogsModelEvents;\n\n    protected static function boot()\n    {\n        parent::boot();\n\n        \/\/ \u30a4\u30d9\u30f3\u30c8\u306e\u30ed\u30b0\u8a18\u9332\n        static::creating(function ($model) {\n            Log::info('Creating user', [\n                'attributes' =&gt; $model-&gt;getDirty(),\n                'trace' =&gt; debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS)\n            ]);\n        });\n\n        \/\/ \u30a4\u30d9\u30f3\u30c8\u306e\u5b9f\u884c\u6642\u9593\u8a08\u6e2c\n        static::updating(function ($model) {\n            $start = microtime(true);\n\n            return function ($model) use ($start) {\n                $time = microtime(true) - $start;\n                Log::info(\"User update completed in {$time}s\", [\n                    'id' =&gt; $model-&gt;id,\n                    'changes' =&gt; $model-&gt;getChanges()\n                ]);\n            };\n        });\n    }\n}\n\n\/\/ \u30a4\u30d9\u30f3\u30c8\u30c7\u30d0\u30c3\u30b0\u7528\u30c8\u30ec\u30a4\u30c8\ntrait LogsModelEvents\n{\n    protected static function bootLogsModelEvents()\n    {\n        if (config('app.debug')) {\n            static::observe(new class {\n                protected $events = [\n                    'creating', 'created',\n                    'updating', 'updated',\n                    'deleting', 'deleted',\n                    'saving', 'saved',\n                ];\n\n                public function __call($method, $parameters)\n                {\n                    if (in_array($method, $this-&gt;events)) {\n                        Log::debug(\"Model event: {$method}\", [\n                            'model' =&gt; get_class($parameters[0]),\n                            'id' =&gt; $parameters[0]-&gt;id ?? null,\n                            'changes' =&gt; $parameters[0]-&gt;getDirty()\n                        ]);\n                    }\n                }\n            });\n        }\n    }\n}\n\n\/\/ \u30a4\u30d9\u30f3\u30c8\u306e\u30d1\u30d5\u30a9\u30fc\u30de\u30f3\u30b9\u76e3\u8996\nclass ModelEventProfiler\n{\n    protected static $timings = [];\n\n    public static function start($event)\n    {\n        static::$timings[$event] = microtime(true);\n    }\n\n    public static function end($event)\n    {\n        if (isset(static::$timings[$event])) {\n            $duration = microtime(true) - static::$timings[$event];\n            Log::info(\"Event {$event} took {$duration}s\");\n            unset(static::$timings[$event]);\n        }\n    }\n\n    public static function monitor(Model $model, array $events)\n    {\n        foreach ($events as $event) {\n            $model::$event(function ($model) use ($event) {\n                static::start($event);\n\n                return function ($model) use ($event) {\n                    static::end($event);\n                };\n            });\n        }\n    }\n}<\/pre>\n\n\n\n<p>\u4ee5\u4e0a\u304c\u3001Laravel\u30e2\u30c7\u30eb\u306e\u30c8\u30e9\u30d6\u30eb\u30b7\u30e5\u30fc\u30c6\u30a3\u30f3\u30b0\u306b\u95a2\u3059\u308b\u8a73\u7d30\u306a\u89e3\u8aac\u3067\u3059\u3002\u3053\u308c\u3089\u306e\u30c6\u30af\u30cb\u30c3\u30af\u3092\u6d3b\u7528\u3059\u308b\u3053\u3068\u3067\u3001\u3088\u308a\u52b9\u7387\u7684\u3067\u4fe1\u983c\u6027\u306e\u9ad8\u3044\u30a2\u30d7\u30ea\u30b1\u30fc\u30b7\u30e7\u30f3\u3092\u69cb\u7bc9\u3067\u304d\u307e\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":{"0":"post-2663","1":"post","2":"type-post","3":"status-publish","4":"format-standard","6":"category-php","7":"nothumb"},"_links":{"self":[{"href":"https:\/\/dexall.co.jp\/articles\/index.php?rest_route=\/wp\/v2\/posts\/2663","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=2663"}],"version-history":[{"count":2,"href":"https:\/\/dexall.co.jp\/articles\/index.php?rest_route=\/wp\/v2\/posts\/2663\/revisions"}],"predecessor-version":[{"id":2665,"href":"https:\/\/dexall.co.jp\/articles\/index.php?rest_route=\/wp\/v2\/posts\/2663\/revisions\/2665"}],"wp:attachment":[{"href":"https:\/\/dexall.co.jp\/articles\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=2663"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/dexall.co.jp\/articles\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=2663"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/dexall.co.jp\/articles\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=2663"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}