{"id":2581,"date":"2025-03-24T08:47:00","date_gmt":"2025-03-23T23:47:00","guid":{"rendered":"https:\/\/dexall.co.jp\/articles\/?p=2581"},"modified":"2025-03-24T08:47:29","modified_gmt":"2025-03-23T23:47:29","slug":"laravel-factory%e3%81%ae%e5%ae%8c%e5%85%a8%e3%82%ac%e3%82%a4%e3%83%89%ef%bc%9a%e3%83%86%e3%82%b9%e3%83%88%e3%83%87%e3%83%bc%e3%82%bf%e4%bd%9c%e6%88%90%e3%82%9210%e5%80%8d%e5%8a%b9%e7%8e%87%e5%8c%96","status":"publish","type":"post","link":"https:\/\/dexall.co.jp\/articles\/?p=2581","title":{"rendered":"Laravel Factory\u306e\u5b8c\u5168\u30ac\u30a4\u30c9\uff1a\u30c6\u30b9\u30c8\u30c7\u30fc\u30bf\u4f5c\u6210\u309210\u500d\u52b9\u7387\u5316\u3059\u308b\u5b9f\u8df5\u30c6\u30af\u30cb\u30c3\u30af"},"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 Factory\u3068\u306f\uff1a\u57fa\u790e\u304b\u3089\u7406\u89e3\u3059\u308b\u81ea\u52d5\u30c6\u30b9\u30c8\u30c7\u30fc\u30bf\u751f\u6210<\/a>    <ul class=\"menu_level_1\">      <li class=\"first\">        <a href=\"#i-1\">Factory\u306e\u5f79\u5272\u3068\u91cd\u8981\u6027<\/a>      <\/li>      <li>        <a href=\"#i-2\">\u5f93\u6765\u306e\u30b7\u30fc\u30c9\u30c7\u30fc\u30bf\u4f5c\u6210\u3068\u306e\u6bd4\u8f03<\/a>      <\/li>      <li class=\"last\">        <a href=\"#i-3\">Factory\u3092\u4f7f\u3046\u30e1\u30ea\u30c3\u30c8<\/a>      <\/li>    <\/ul>  <\/li>  <li>    <a href=\"#i-4\">Laravel Factory\u306e\u57fa\u672c\u7684\u306a\u4f7f\u3044\u65b9<\/a>    <ul class=\"menu_level_1\">      <li class=\"first\">        <a href=\"#i-5\">Factory\u5b9a\u7fa9\u30d5\u30a1\u30a4\u30eb\u306e\u4f5c\u6210\u65b9\u6cd5<\/a>      <\/li>      <li>        <a href=\"#i-6\">\u30e2\u30c7\u30eb\u3068Factory\u306e\u7d10\u4ed8\u3051<\/a>      <\/li>      <li class=\"last\">        <a href=\"#i-7\">\u57fa\u672c\u7684\u306a\u30c7\u30fc\u30bf\u5b9a\u7fa9\u306e\u4f5c\u6210<\/a>      <\/li>    <\/ul>  <\/li>  <li>    <a href=\"#i-8\">\u5b9f\u8df5\u7684\u306aFactory\u6d3b\u7528\u30c6\u30af\u30cb\u30c3\u30af<\/a>    <ul class=\"menu_level_1\">      <li class=\"first\">        <a href=\"#i-9\">Faker\u3092\u4f7f\u3063\u305f\u591a\u69d8\u306a\u30c6\u30b9\u30c8\u30c7\u30fc\u30bf\u306e\u751f\u6210<\/a>      <\/li>      <li>        <a href=\"#i-10\">\u30ea\u30ec\u30fc\u30b7\u30e7\u30f3\u3092\u542b\u3080Factory\u306e\u5b9a\u7fa9\u65b9\u6cd5<\/a>      <\/li>      <li class=\"last\">        <a href=\"#i-11\">\u72b6\u614b\uff08state\uff09\u3092\u4f7f\u3063\u305f\u6761\u4ef6\u4ed8\u304d\u30c7\u30fc\u30bf\u751f\u6210<\/a>      <\/li>    <\/ul>  <\/li>  <li>    <a href=\"#i-12\">\u30c6\u30b9\u30c8\u3067\u306eFactory\u6d3b\u7528\u65b9\u6cd5<\/a>    <ul class=\"menu_level_1\">      <li class=\"first\">        <a href=\"#i-13\">\u30e6\u30cb\u30c3\u30c8\u30c6\u30b9\u30c8\u3067\u306e\u52b9\u679c\u7684\u306a\u4f7f\u3044\u65b9<\/a>      <\/li>      <li>        <a href=\"#i-14\">\u8907\u96d1\u306a\u30c6\u30b9\u30c8\u30c7\u30fc\u30bf\u30bb\u30c3\u30c8\u306e\u4f5c\u6210<\/a>      <\/li>      <li class=\"last\">        <a href=\"#i-15\">\u30c6\u30b9\u30c8\u30d1\u30d5\u30a9\u30fc\u30de\u30f3\u30b9\u306e\u6700\u9069\u5316<\/a>      <\/li>    <\/ul>  <\/li>  <li>    <a href=\"#i-16\">\u5b9f\u52d9\u3067\u3088\u304f\u4f7f\u3046Factory\u30d1\u30bf\u30fc\u30f3\u96c6<\/a>    <ul class=\"menu_level_1\">      <li class=\"first\">        <a href=\"#i-17\">\u30e6\u30fc\u30b6\u30fc\u95a2\u9023\u306e\u30c7\u30fc\u30bf\u751f\u6210\u30d1\u30bf\u30fc\u30f3<\/a>      <\/li>      <li>        <a href=\"#i-18\">EC\u30b5\u30a4\u30c8\u3067\u306e\u5546\u54c1\u30c7\u30fc\u30bf\u751f\u6210\u4f8b<\/a>      <\/li>      <li class=\"last\">        <a href=\"#i-19\">\u30d6\u30ed\u30b0\u8a18\u4e8b\u306e\u30c6\u30b9\u30c8\u30c7\u30fc\u30bf\u4f5c\u6210\u4f8b<\/a>      <\/li>    <\/ul>  <\/li>  <li class=\"last\">    <a href=\"#i-20\">Factory\u306e\u30d9\u30b9\u30c8\u30d7\u30e9\u30af\u30c6\u30a3\u30b9\u3068\u30c8\u30e9\u30d6\u30eb\u30b7\u30e5\u30fc\u30c6\u30a3\u30f3\u30b0<\/a>    <ul class=\"menu_level_1\">      <li class=\"first\">        <a href=\"#i-21\">Factory\u8a2d\u8a08\u6642\u306e\u6ce8\u610f\u70b9<\/a>      <\/li>      <li>        <a href=\"#i-22\">\u3088\u304f\u3042\u308b\u30a8\u30e9\u30fc\u3068\u305d\u306e\u89e3\u6c7a\u65b9\u6cd5<\/a>      <\/li>      <li class=\"last\">        <a href=\"#i-23\">\u30d1\u30d5\u30a9\u30fc\u30de\u30f3\u30b9\u3092\u8003\u616e\u3057\u305fFactory\u8a2d\u8a08<\/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 Factory\u3068\u306f\uff1a\u57fa\u790e\u304b\u3089\u7406\u89e3\u3059\u308b\u81ea\u52d5\u30c6\u30b9\u30c8\u30c7\u30fc\u30bf\u751f\u6210<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"i-1\">Factory\u306e\u5f79\u5272\u3068\u91cd\u8981\u6027<\/h3>\n\n\n\n<p>Laravel\u306e\u30d5\u30a1\u30af\u30c8\u30ea\u30fc\uff08Factory\uff09\u306f\u3001\u30c6\u30b9\u30c8\u3084\u30b7\u30fc\u30c7\u30a3\u30f3\u30b0\u3067\u4f7f\u7528\u3059\u308b\u30c0\u30df\u30fc\u30c7\u30fc\u30bf\u3092\u52b9\u7387\u7684\u306b\u751f\u6210\u3059\u308b\u305f\u3081\u306e\u5f37\u529b\u306a\u6a5f\u80fd\u3067\u3059\u3002\u958b\u767a\u73fe\u5834\u3067\u306f\u3001\u4ee5\u4e0b\u306e\u3088\u3046\u306a\u5834\u9762\u3067\u91cd\u8981\u306a\u5f79\u5272\u3092\u679c\u305f\u3057\u307e\u3059\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u30c6\u30b9\u30c8\u30b1\u30fc\u30b9\u5b9f\u884c\u6642\u306e\u30c6\u30b9\u30c8\u30c7\u30fc\u30bf\u751f\u6210<\/li>\n\n\n\n<li>\u958b\u767a\u74b0\u5883\u3067\u306e\u521d\u671f\u30c7\u30fc\u30bf\u6295\u5165<\/li>\n\n\n\n<li>\u30c7\u30e2\u30c7\u30fc\u30bf\u306e\u4f5c\u6210<\/li>\n\n\n\n<li>\u30d1\u30d5\u30a9\u30fc\u30de\u30f3\u30b9\u30c6\u30b9\u30c8\u7528\u306e\u5927\u91cf\u30c7\u30fc\u30bf\u751f\u6210<\/li>\n<\/ul>\n\n\n\n<p>\u7279\u306b\u5927\u898f\u6a21\u306a\u30a2\u30d7\u30ea\u30b1\u30fc\u30b7\u30e7\u30f3\u958b\u767a\u306b\u304a\u3044\u3066\u3001Factory\u306e\u6d3b\u7528\u306f\u958b\u767a\u52b9\u7387\u3092\u5927\u304d\u304f\u5411\u4e0a\u3055\u305b\u308b\u9375\u3068\u306a\u308a\u307e\u3059\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"i-2\">\u5f93\u6765\u306e\u30b7\u30fc\u30c9\u30c7\u30fc\u30bf\u4f5c\u6210\u3068\u306e\u6bd4\u8f03<\/h3>\n\n\n\n<p>\u5f93\u6765\u306e\u30b7\u30fc\u30c9\u30c7\u30fc\u30bf\u4f5c\u6210\u65b9\u6cd5\u3068Factory\u3092\u6bd4\u8f03\u3057\u3066\u307f\u307e\u3057\u3087\u3046\uff1a<\/p>\n\n\n\n<p><strong>\u5f93\u6765\u306e\u65b9\u6cd5\uff08DatabaseSeeder\uff09:<\/strong><\/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=\"\">\/\/ DatabaseSeeder.php\npublic function run()\n{\n    DB::table('users')-&gt;insert([\n        'name' =&gt; '\u5c71\u7530\u592a\u90ce',\n        'email' =&gt; 'yamada@example.com',\n        'password' =&gt; Hash::make('password'),\n        \/\/ \u4ed6\u306e\u5fc5\u8981\u306a\u30c7\u30fc\u30bf...\n    ]);\n}<\/pre>\n\n\n\n<p><strong>Factory\u3092\u4f7f\u7528\u3057\u305f\u65b9\u6cd5:<\/strong><\/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=\"\">\/\/ UserFactory.php\npublic function definition()\n{\n    return [\n        'name' =&gt; fake()-&gt;name(),\n        'email' =&gt; fake()-&gt;unique()-&gt;safeEmail(),\n        'password' =&gt; Hash::make('password'),\n    ];\n}\n\n\/\/ \u4f7f\u7528\u4f8b\nUser::factory()-&gt;count(10)-&gt;create();<\/pre>\n\n\n\n<p>\u5f93\u6765\u306e\u65b9\u6cd5\u3068\u6bd4\u8f03\u3057\u305fFactory\u306e\u512a\u4f4d\u70b9\uff1a<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>\u30c7\u30fc\u30bf\u306e\u52d5\u7684\u751f\u6210<\/strong>\uff1aFaker\u3092\u4f7f\u7528\u3057\u3066\u3001\u3088\u308a\u30ea\u30a2\u30eb\u306a\u30c6\u30b9\u30c8\u30c7\u30fc\u30bf\u3092\u81ea\u52d5\u751f\u6210<\/li>\n\n\n\n<li><strong>\u518d\u5229\u7528\u6027<\/strong>\uff1a\u4e00\u5ea6\u5b9a\u7fa9\u3057\u305fFactory\u306f\u8907\u6570\u306e\u5834\u6240\u3067\u518d\u5229\u7528\u53ef\u80fd<\/li>\n\n\n\n<li><strong>\u67d4\u8edf\u6027<\/strong>\uff1a\u72b6\u614b\uff08state\uff09\u3092\u4f7f\u7528\u3057\u3066\u3001\u69d8\u3005\u306a\u30d1\u30bf\u30fc\u30f3\u306e\u30c7\u30fc\u30bf\u3092\u5bb9\u6613\u306b\u751f\u6210<\/li>\n\n\n\n<li><strong>\u95a2\u9023\u30c7\u30fc\u30bf\u306e\u81ea\u52d5\u751f\u6210<\/strong>\uff1a\u30ea\u30ec\u30fc\u30b7\u30e7\u30f3\u3092\u542b\u3080\u30c7\u30fc\u30bf\u3082\u7c21\u5358\u306b\u751f\u6210\u53ef\u80fd<\/li>\n<\/ol>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"i-3\">Factory\u3092\u4f7f\u3046\u30e1\u30ea\u30c3\u30c8<\/h3>\n\n\n\n<p>Laravel Factory\u3092\u6d3b\u7528\u3059\u308b\u3053\u3068\u3067\u3001\u4ee5\u4e0b\u306e\u3088\u3046\u306a\u5177\u4f53\u7684\u306a\u30e1\u30ea\u30c3\u30c8\u304c\u5f97\u3089\u308c\u307e\u3059\uff1a<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>\u958b\u767a\u6642\u9593\u306e\u77ed\u7e2e<\/strong><\/li>\n<\/ol>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u30c6\u30b9\u30c8\u30c7\u30fc\u30bf\u306e\u624b\u52d5\u4f5c\u6210\u304c\u4e0d\u8981\u306b<\/li>\n\n\n\n<li>\u8907\u96d1\u306a\u30c7\u30fc\u30bf\u69cb\u9020\u3082\u81ea\u52d5\u751f\u6210\u53ef\u80fd<\/li>\n\n\n\n<li>\u7e70\u308a\u8fd4\u3057\u5229\u7528\u53ef\u80fd\u306a\u30c6\u30f3\u30d7\u30ec\u30fc\u30c8\u3068\u3057\u3066\u6a5f\u80fd<\/li>\n<\/ul>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>\u30c6\u30b9\u30c8\u306e\u54c1\u8cea\u5411\u4e0a<\/strong><\/li>\n<\/ol>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u3088\u308a\u73fe\u5b9f\u7684\u306a\u30c6\u30b9\u30c8\u30c7\u30fc\u30bf\u306e\u4f7f\u7528<\/li>\n\n\n\n<li>\u30a8\u30c3\u30b8\u30b1\u30fc\u30b9\u306e\u30c6\u30b9\u30c8\u304c\u5bb9\u6613<\/li>\n\n\n\n<li>\u30c7\u30fc\u30bf\u306e\u4e00\u8cab\u6027\u304c\u4fdd\u8a3c\u3055\u308c\u308b<\/li>\n<\/ul>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>\u30e1\u30f3\u30c6\u30ca\u30f3\u30b9\u6027\u306e\u5411\u4e0a<\/strong><\/li>\n<\/ol>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u30c7\u30fc\u30bf\u69cb\u9020\u306e\u5909\u66f4\u306b\u67d4\u8edf\u306b\u5bfe\u5fdc<\/li>\n\n\n\n<li>\u30c1\u30fc\u30e0\u9593\u3067\u306e\u5171\u901a\u7406\u89e3\u304c\u5bb9\u6613<\/li>\n\n\n\n<li>\u30b3\u30fc\u30c9\u306e\u53ef\u8aad\u6027\u304c\u5411\u4e0a<\/li>\n<\/ul>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>\u30b9\u30b1\u30fc\u30e9\u30d3\u30ea\u30c6\u30a3\u306e\u78ba\u4fdd<\/strong><\/li>\n<\/ol>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u5927\u91cf\u30c7\u30fc\u30bf\u306e\u52b9\u7387\u7684\u306a\u751f\u6210<\/li>\n\n\n\n<li>\u7570\u306a\u308b\u74b0\u5883\u3067\u306e\u4e00\u8cab\u3057\u305f\u30c7\u30fc\u30bf\u751f\u6210<\/li>\n\n\n\n<li>\u30d1\u30d5\u30a9\u30fc\u30de\u30f3\u30b9\u30c6\u30b9\u30c8\u306e\u5b9f\u65bd\u304c\u5bb9\u6613<\/li>\n<\/ul>\n\n\n\n<p>\u3053\u308c\u3089\u306e\u30e1\u30ea\u30c3\u30c8\u306b\u3088\u308a\u3001Laravel Factory\u306f\u73fe\u4ee3\u306eWeb\u958b\u767a\u306b\u304a\u3051\u308b\u5fc5\u9808\u30c4\u30fc\u30eb\u306e1\u3064\u3068\u306a\u3063\u3066\u3044\u307e\u3059\u3002\u7279\u306b\u3001\u30c1\u30fc\u30e0\u958b\u767a\u3084\u30a2\u30b8\u30e3\u30a4\u30eb\u958b\u767a\u306b\u304a\u3044\u3066\u3001\u305d\u306e\u771f\u4fa1\u3092\u767a\u63ee\u3057\u307e\u3059\u3002<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"i-4\">Laravel Factory\u306e\u57fa\u672c\u7684\u306a\u4f7f\u3044\u65b9<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"i-5\">Factory\u5b9a\u7fa9\u30d5\u30a1\u30a4\u30eb\u306e\u4f5c\u6210\u65b9\u6cd5<\/h3>\n\n\n\n<p>Laravel\u3067\u306f\u3001Artisan\u30b3\u30de\u30f3\u30c9\u3092\u4f7f\u7528\u3057\u3066\u7c21\u5358\u306bFactory\u30d5\u30a1\u30a4\u30eb\u3092\u4f5c\u6210\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=\"\"># \u57fa\u672c\u7684\u306aFactory\u4f5c\u6210\u30b3\u30de\u30f3\u30c9\nphp artisan make:factory UserFactory\n\n# \u30e2\u30c7\u30eb\u3068\u540c\u6642\u306b\u4f5c\u6210\u3059\u308b\u5834\u5408\nphp artisan make:model User --factory\n\n# \u30de\u30a4\u30b0\u30ec\u30fc\u30b7\u30e7\u30f3\u3001\u30b7\u30fc\u30c0\u30fc\u3001\u30d5\u30a1\u30af\u30c8\u30ea\u30fc\u3092\u540c\u6642\u306b\u4f5c\u6210\u3059\u308b\u5834\u5408\nphp artisan make:model User -mfs<\/pre>\n\n\n\n<p>\u4f5c\u6210\u3055\u308c\u305fFactory\u30d5\u30a1\u30a4\u30eb\u306f <code>database\/factories<\/code> \u30c7\u30a3\u30ec\u30af\u30c8\u30ea\u306b\u914d\u7f6e\u3055\u308c\u307e\u3059\u3002\u57fa\u672c\u7684\u306aFactory\u5b9a\u7fa9\u30d5\u30a1\u30a4\u30eb\u306e\u69cb\u9020\u306f\u4ee5\u4e0b\u306e\u3088\u3046\u306b\u306a\u308a\u307e\u3059\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=\"\">namespace Database\\Factories;\n\nuse Illuminate\\Database\\Eloquent\\Factories\\Factory;\nuse App\\Models\\User;\n\nclass UserFactory extends Factory\n{\n    \/**\n     * \u30e2\u30c7\u30eb\u3068\u5bfe\u5fdc\u3059\u308b\u30d5\u30a1\u30af\u30c8\u30ea\u30fc\u306e\u5b9a\u7fa9\n     *\/\n    protected $model = User::class;\n\n    \/**\n     * \u30d5\u30a1\u30af\u30c8\u30ea\u30fc\u306e\u30c7\u30d5\u30a9\u30eb\u30c8\u72b6\u614b\u306e\u5b9a\u7fa9\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; bcrypt('password'),\n            'created_at' =&gt; now(),\n            'updated_at' =&gt; now(),\n        ];\n    }\n}<\/pre>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"i-6\">\u30e2\u30c7\u30eb\u3068Factory\u306e\u7d10\u4ed8\u3051<\/h3>\n\n\n\n<p>\u30e2\u30c7\u30eb\u3068Factory\u3092\u7d10\u4ed8\u3051\u308b\u306b\u306f\u3001\u4ee5\u4e0b\u306e2\u3064\u306e\u65b9\u6cd5\u304c\u3042\u308a\u307e\u3059\uff1a<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>Factory\u5074\u3067\u306e\u7d10\u4ed8\u3051<\/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=\"\">\/\/ UserFactory.php\nprotected $model = User::class;<\/pre>\n\n\n\n<ol start=\"2\" class=\"wp-block-list\">\n<li><strong>\u30e2\u30c7\u30eb\u5074\u3067\u306e\u7d10\u4ed8\u3051<\/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=\"\">\/\/ User.php\nnamespace App\\Models;\n\nuse Illuminate\\Database\\Eloquent\\Factories\\HasFactory;\nuse Database\\Factories\\UserFactory;\n\nclass User extends Model\n{\n    use HasFactory;\n\n    \/**\n     * \u30ab\u30b9\u30bf\u30e0\u30d5\u30a1\u30af\u30c8\u30ea\u30fc\u306e\u6307\u5b9a\n     *\/\n    protected static function newFactory()\n    {\n        return UserFactory::new();\n    }\n}<\/pre>\n\n\n\n<p>\u6700\u65b0\u306eLaravel\u3067\u306f\u3001<code>HasFactory<\/code> \u30c8\u30ec\u30a4\u30c8\u3092\u4f7f\u7528\u3059\u308b\u3060\u3051\u3067\u81ea\u52d5\u7684\u306b\u7d10\u4ed8\u3051\u304c\u884c\u308f\u308c\u308b\u305f\u3081\u3001\u901a\u5e38\u306f\u7279\u5225\u306a\u8a2d\u5b9a\u306f\u4e0d\u8981\u3067\u3059\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"i-7\">\u57fa\u672c\u7684\u306a\u30c7\u30fc\u30bf\u5b9a\u7fa9\u306e\u4f5c\u6210<\/h3>\n\n\n\n<p>Factory\u3067\u306e\u30c7\u30fc\u30bf\u5b9a\u7fa9\u306b\u306f\u3001Faker\u30e9\u30a4\u30d6\u30e9\u30ea\u306e\u6a5f\u80fd\u3092\u6d3b\u7528\u3057\u307e\u3059\u3002\u4ee5\u4e0b\u306b\u4e00\u822c\u7684\u306a\u30c7\u30fc\u30bf\u5b9a\u7fa9\u30d1\u30bf\u30fc\u30f3\u3092\u793a\u3057\u307e\u3059\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=\"\">public function definition()\n{\n    return [\n        \/\/ \u57fa\u672c\u7684\u306a\u6587\u5b57\u5217\u30c7\u30fc\u30bf\n        'title' =&gt; $this-&gt;faker-&gt;sentence(),    \/\/ \u30e9\u30f3\u30c0\u30e0\u306a\u6587\u7ae0\n        'name' =&gt; $this-&gt;faker-&gt;name(),         \/\/ \u4eba\u540d\n\n        \/\/ \u6570\u5024\u30c7\u30fc\u30bf\n        'age' =&gt; $this-&gt;faker-&gt;numberBetween(18, 60),  \/\/ 18-60\u306e\u7bc4\u56f2\n        'price' =&gt; $this-&gt;faker-&gt;randomNumber(4),      \/\/ 4\u6841\u306e\u6570\u5024\n\n        \/\/ \u65e5\u4ed8\u30c7\u30fc\u30bf\n        'birthday' =&gt; $this-&gt;faker-&gt;date(),          \/\/ Y-m-d\u5f62\u5f0f\u306e\u65e5\u4ed8\n        'created_at' =&gt; $this-&gt;faker-&gt;dateTime(),    \/\/ \u65e5\u6642\n\n        \/\/ \u771f\u507d\u5024\n        'is_active' =&gt; $this-&gt;faker-&gt;boolean(),      \/\/ true\/false\n\n        \/\/ \u9078\u629e\u80a2\u304b\u3089\u306e\u30e9\u30f3\u30c0\u30e0\u9078\u629e\n        'status' =&gt; $this-&gt;faker-&gt;randomElement(['pending', 'active', 'suspended']),\n\n        \/\/ \u30e6\u30cb\u30fc\u30af\u306a\u5024\n        'email' =&gt; $this-&gt;faker-&gt;unique()-&gt;safeEmail(),\n        'username' =&gt; $this-&gt;faker-&gt;unique()-&gt;userName(),\n    ];\n}<\/pre>\n\n\n\n<p>Factory\u3092\u4f7f\u7528\u3057\u3066\u30c7\u30fc\u30bf\u3092\u751f\u6210\u3059\u308b\u57fa\u672c\u7684\u306a\u65b9\u6cd5\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=\"\">\/\/ \u5358\u4e00\u306e\u30a4\u30f3\u30b9\u30bf\u30f3\u30b9\u3092\u4f5c\u6210\uff08DB\u306b\u4fdd\u5b58\uff09\n$user = User::factory()-&gt;create();\n\n\/\/ \u8907\u6570\u306e\u30a4\u30f3\u30b9\u30bf\u30f3\u30b9\u3092\u4f5c\u6210\n$users = User::factory()-&gt;count(5)-&gt;create();\n\n\/\/ \u30a4\u30f3\u30b9\u30bf\u30f3\u30b9\u3092\u4f5c\u6210\u3059\u308b\u304c\u3001DB\u306b\u306f\u4fdd\u5b58\u3057\u306a\u3044\n$user = User::factory()-&gt;make();\n\n\/\/ \u914d\u5217\u3068\u3057\u3066\u53d6\u5f97\n$userData = User::factory()-&gt;raw();<\/pre>\n\n\n\n<p>\u5b9f\u969b\u306e\u958b\u767a\u3067\u306f\u3001\u3053\u308c\u3089\u306e\u57fa\u672c\u7684\u306a\u4f7f\u3044\u65b9\u3092\u7d44\u307f\u5408\u308f\u305b\u3066\u3001\u3088\u308a\u8907\u96d1\u306a\u30c6\u30b9\u30c8\u30c7\u30fc\u30bf\u3092\u751f\u6210\u3057\u307e\u3059\u3002\u305f\u3068\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=\"\">\/\/ \u7279\u5b9a\u306e\u5024\u3092\u4e0a\u66f8\u304d\u3057\u3066\u30a4\u30f3\u30b9\u30bf\u30f3\u30b9\u3092\u4f5c\u6210\n$admin = User::factory()-&gt;create([\n    'role' =&gt; 'admin',\n    'email' =&gt; 'admin@example.com'\n]);\n\n\/\/ \u8907\u6570\u306e\u30a4\u30f3\u30b9\u30bf\u30f3\u30b9\u3092\u4f5c\u6210\u3057\u3001\u5404\u30a4\u30f3\u30b9\u30bf\u30f3\u30b9\u3092\u30ab\u30b9\u30bf\u30de\u30a4\u30ba\n$users = User::factory()\n    -&gt;count(3)\n    -&gt;sequence(\n        ['role' =&gt; 'user'],\n        ['role' =&gt; 'editor'],\n        ['role' =&gt; 'moderator']\n    )\n    -&gt;create();<\/pre>\n\n\n\n<p>\u3053\u308c\u3089\u306e\u57fa\u672c\u7684\u306a\u6a5f\u80fd\u3092\u7406\u89e3\u3059\u308b\u3053\u3068\u3067\u3001\u6b21\u306e\u30bb\u30af\u30b7\u30e7\u30f3\u3067\u8aac\u660e\u3059\u308b\u9ad8\u5ea6\u306a\u4f7f\u3044\u65b9\u3084\u5b9f\u8df5\u7684\u306a\u30c6\u30af\u30cb\u30c3\u30af\u306e\u57fa\u790e\u304c\u56fa\u307e\u308a\u307e\u3059\u3002<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"i-8\">\u5b9f\u8df5\u7684\u306aFactory\u6d3b\u7528\u30c6\u30af\u30cb\u30c3\u30af<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"i-9\">Faker\u3092\u4f7f\u3063\u305f\u591a\u69d8\u306a\u30c6\u30b9\u30c8\u30c7\u30fc\u30bf\u306e\u751f\u6210<\/h3>\n\n\n\n<p>Faker\u30e9\u30a4\u30d6\u30e9\u30ea\u3092\u6d3b\u7528\u3059\u308b\u3053\u3068\u3067\u3001\u3088\u308a\u73fe\u5b9f\u7684\u3067\u591a\u69d8\u306a\u30c6\u30b9\u30c8\u30c7\u30fc\u30bf\u3092\u751f\u6210\u3067\u304d\u307e\u3059\u3002\u4ee5\u4e0b\u306b\u5b9f\u8df5\u7684\u306a\u4f8b\u3092\u793a\u3057\u307e\u3059\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=\"\">\/\/ PostFactory.php\npublic function definition()\n{\n    return [\n        \/\/ \u65e5\u672c\u8a9e\u306e\u30bf\u30a4\u30c8\u30eb\u3068\u672c\u6587\u3092\u751f\u6210\n        'title' =&gt; $this-&gt;faker-&gt;realText(30),    \/\/ 30\u6587\u5b57\u7a0b\u5ea6\u306e\u81ea\u7136\u306a\u65e5\u672c\u8a9e\n        'content' =&gt; $this-&gt;faker-&gt;realText(200),  \/\/ 200\u6587\u5b57\u7a0b\u5ea6\u306e\u81ea\u7136\u306a\u65e5\u672c\u8a9e\n\n        \/\/ \u753b\u50cfURL\u306e\u751f\u6210\n        'thumbnail' =&gt; $this-&gt;faker-&gt;imageUrl(640, 480, 'posts'),\n\n        \/\/ \u6295\u7a3f\u30b9\u30c6\u30fc\u30bf\u30b9\u3092\u30e9\u30f3\u30c0\u30e0\u306b\u8a2d\u5b9a\n        'status' =&gt; $this-&gt;faker-&gt;randomElement([\n            'draft',\n            'published',\n            'archived'\n        ]),\n\n        \/\/ \u73fe\u5b9f\u7684\u306a\u65e5\u6642\u306e\u751f\u6210\n        'published_at' =&gt; $this-&gt;faker-&gt;dateTimeBetween('-1 year', 'now'),\n\n        \/\/ \u30a2\u30af\u30bb\u30b9\u6570\u306a\u3069\u306e\u7d71\u8a08\u30c7\u30fc\u30bf\n        'view_count' =&gt; $this-&gt;faker-&gt;numberBetween(100, 10000),\n        'like_count' =&gt; $this-&gt;faker-&gt;numberBetween(0, 1000),\n\n        \/\/ JSON\u30c7\u30fc\u30bf\u306e\u751f\u6210\n        'metadata' =&gt; json_encode([\n            'tags' =&gt; $this-&gt;faker-&gt;words(3),\n            'categories' =&gt; $this-&gt;faker-&gt;randomElements(['\u6280\u8853', '\u30ad\u30e3\u30ea\u30a2', '\u6587\u5316', '\u7d44\u7e54'], 2),\n            'reading_time' =&gt; $this-&gt;faker-&gt;numberBetween(3, 15)\n        ])\n    ];\n}<\/pre>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"i-10\">\u30ea\u30ec\u30fc\u30b7\u30e7\u30f3\u3092\u542b\u3080Factory\u306e\u5b9a\u7fa9\u65b9\u6cd5<\/h3>\n\n\n\n<p>\u8907\u6570\u306e\u30e2\u30c7\u30eb\u9593\u306e\u30ea\u30ec\u30fc\u30b7\u30e7\u30f3\u3092\u542b\u3080\u30c6\u30b9\u30c8\u30c7\u30fc\u30bf\u306e\u751f\u6210\u306f\u3001\u5b9f\u52d9\u3067\u306f\u975e\u5e38\u306b\u91cd\u8981\u3067\u3059\u3002\u4ee5\u4e0b\u306b\u4e3b\u8981\u306a\u30d1\u30bf\u30fc\u30f3\u3092\u793a\u3057\u307e\u3059\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=\"\">\/\/ UserFactory.php\nclass UserFactory extends Factory\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        ];\n    }\n\n    \/\/ \u30d7\u30ed\u30d5\u30a3\u30fc\u30eb\u60c5\u5831\u3092\u6301\u3064\u30e6\u30fc\u30b6\u30fc\u3092\u4f5c\u6210\n    public function withProfile()\n    {\n        return $this-&gt;afterCreating(function (User $user) {\n            Profile::factory()-&gt;create(['user_id' =&gt; $user-&gt;id]);\n        });\n    }\n}\n\n\/\/ PostFactory.php\nclass PostFactory extends Factory\n{\n    public function definition()\n    {\n        return [\n            'title' =&gt; $this-&gt;faker-&gt;sentence(),\n            'content' =&gt; $this-&gt;faker-&gt;paragraphs(3, true),\n            'user_id' =&gt; User::factory()  \/\/ \u81ea\u52d5\u7684\u306b\u30e6\u30fc\u30b6\u30fc\u3092\u4f5c\u6210\n        ];\n    }\n\n    \/\/ \u30b3\u30e1\u30f3\u30c8\u4ed8\u304d\u306e\u6295\u7a3f\u3092\u4f5c\u6210\n    public function withComments(int $count = 3)\n    {\n        return $this-&gt;afterCreating(function (Post $post) use ($count) {\n            Comment::factory()-&gt;count($count)-&gt;create([\n                'post_id' =&gt; $post-&gt;id\n            ]);\n        });\n    }\n}<\/pre>\n\n\n\n<p>\u3053\u308c\u3089\u306eFactory\u3092\u4f7f\u7528\u3059\u308b\u4f8b\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=\"\">\/\/ \u57fa\u672c\u7684\u306a\u4f7f\u7528\u65b9\u6cd5\n$user = User::factory()-&gt;withProfile()-&gt;create();\n$post = Post::factory()-&gt;withComments(5)-&gt;create();\n\n\/\/ \u3088\u308a\u8907\u96d1\u306a\u30ea\u30ec\u30fc\u30b7\u30e7\u30f3\n$user = User::factory()\n    -&gt;has(Post::factory()-&gt;count(3)-&gt;withComments(2))\n    -&gt;withProfile()\n    -&gt;create();\n\n\/\/ \u591a\u5bfe\u591a\u306e\u30ea\u30ec\u30fc\u30b7\u30e7\u30f3\n$tags = Tag::factory()-&gt;count(3)-&gt;create();\n$post = Post::factory()\n    -&gt;hasAttached($tags, ['created_at' =&gt; now()])\n    -&gt;create();<\/pre>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"i-11\">\u72b6\u614b\uff08state\uff09\u3092\u4f7f\u3063\u305f\u6761\u4ef6\u4ed8\u304d\u30c7\u30fc\u30bf\u751f\u6210<\/h3>\n\n\n\n<p>state\u3092\u4f7f\u7528\u3059\u308b\u3053\u3068\u3067\u3001\u7279\u5b9a\u306e\u6761\u4ef6\u3092\u6e80\u305f\u3059\u30c6\u30b9\u30c8\u30c7\u30fc\u30bf\u3092\u7c21\u5358\u306b\u751f\u6210\u3067\u304d\u307e\u3059\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=\"\">\/\/ UserFactory.php\nclass UserFactory extends Factory\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            'role' =&gt; 'user',\n            'email_verified_at' =&gt; now(),\n            'status' =&gt; 'active'\n        ];\n    }\n\n    \/\/ \u7ba1\u7406\u8005\u30e6\u30fc\u30b6\u30fc\n    public function admin()\n    {\n        return $this-&gt;state(function (array $attributes) {\n            return [\n                'role' =&gt; 'admin',\n                'email' =&gt; 'admin.' . $this-&gt;faker-&gt;unique()-&gt;safeEmail()\n            ];\n        });\n    }\n\n    \/\/ \u672a\u8a8d\u8a3c\u30e6\u30fc\u30b6\u30fc\n    public function unverified()\n    {\n        return $this-&gt;state(fn (array $attributes) =&gt; [\n            'email_verified_at' =&gt; null\n        ]);\n    }\n\n    \/\/ \u505c\u6b62\u4e2d\u306e\u30e6\u30fc\u30b6\u30fc\n    public function suspended()\n    {\n        return $this-&gt;state(fn (array $attributes) =&gt; [\n            'status' =&gt; 'suspended',\n            'suspended_at' =&gt; now()\n        ]);\n    }\n}<\/pre>\n\n\n\n<p>\u72b6\u614b\u3092\u7d44\u307f\u5408\u308f\u305b\u305f\u9ad8\u5ea6\u306a\u4f7f\u7528\u4f8b\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=\"\">\/\/ \u672a\u8a8d\u8a3c\u306e\u7ba1\u7406\u8005\u3092\u4f5c\u6210\n$admin = User::factory()\n    -&gt;admin()\n    -&gt;unverified()\n    -&gt;create();\n\n\/\/ \u505c\u6b62\u4e2d\u306e\u30e6\u30fc\u30b6\u30fc\u30923\u4eba\u4f5c\u6210\u3057\u3001\u5404\u30e6\u30fc\u30b6\u30fc\u306b\u6295\u7a3f\u3092\u95a2\u9023\u4ed8\u3051\n$users = User::factory()\n    -&gt;suspended()\n    -&gt;has(Post::factory()-&gt;count(2))\n    -&gt;count(3)\n    -&gt;create();\n\n\/\/ \u8907\u6570\u306e\u72b6\u614b\u3092\u6301\u3064\u6295\u7a3f\u3092\u4f5c\u6210\n$posts = Post::factory()\n    -&gt;count(10)\n    -&gt;sequence(\n        ['status' =&gt; 'draft'],\n        ['status' =&gt; 'published']\n    )\n    -&gt;create();<\/pre>\n\n\n\n<p>\u3053\u308c\u3089\u306e\u30c6\u30af\u30cb\u30c3\u30af\u3092\u7d44\u307f\u5408\u308f\u305b\u308b\u3053\u3068\u3067\u3001\u8907\u96d1\u306a\u8981\u4ef6\u3092\u6301\u3064\u30c6\u30b9\u30c8\u30c7\u30fc\u30bf\u3082\u52b9\u7387\u7684\u306b\u751f\u6210\u3067\u304d\u307e\u3059\u3002\u6b21\u306e\u30bb\u30af\u30b7\u30e7\u30f3\u3067\u306f\u3001\u3053\u308c\u3089\u306e\u30c6\u30af\u30cb\u30c3\u30af\u3092\u5b9f\u969b\u306e\u30c6\u30b9\u30c8\u30b1\u30fc\u30b9\u3067\u3069\u306e\u3088\u3046\u306b\u6d3b\u7528\u3059\u308b\u304b\u3092\u8aac\u660e\u3057\u307e\u3059\u3002<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"i-12\">\u30c6\u30b9\u30c8\u3067\u306eFactory\u6d3b\u7528\u65b9\u6cd5<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"i-13\">\u30e6\u30cb\u30c3\u30c8\u30c6\u30b9\u30c8\u3067\u306e\u52b9\u679c\u7684\u306a\u4f7f\u3044\u65b9<\/h3>\n\n\n\n<p>PHPUnit\u3067Factory\u3092\u52b9\u679c\u7684\u306b\u6d3b\u7528\u3059\u308b\u65b9\u6cd5\u3092\u5177\u4f53\u7684\u306b\u898b\u3066\u3044\u304d\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=\"\">use Tests\\TestCase;\nuse App\\Models\\User;\nuse App\\Models\\Post;\nuse Illuminate\\Foundation\\Testing\\RefreshDatabase;\n\nclass PostTest extends TestCase\n{\n    use RefreshDatabase;\n\n    public function test_user_can_create_post()\n    {\n        \/\/ \u30c6\u30b9\u30c8\u30e6\u30fc\u30b6\u30fc\u306e\u4f5c\u6210\n        $user = User::factory()-&gt;create();\n\n        \/\/ \u30a2\u30af\u30c6\u30a3\u30f3\u30b0\u30e6\u30fc\u30b6\u30fc\u3068\u3057\u3066\u8a2d\u5b9a\n        $this-&gt;actingAs($user);\n\n        \/\/ \u6295\u7a3f\u30c7\u30fc\u30bf\u306e\u6e96\u5099\n        $postData = Post::factory()-&gt;make()-&gt;toArray();\n\n        \/\/ \u6295\u7a3f\u4f5c\u6210\u306e\u30ea\u30af\u30a8\u30b9\u30c8\n        $response = $this-&gt;post('\/posts', $postData);\n\n        \/\/ \u30a2\u30b5\u30fc\u30b7\u30e7\u30f3\n        $response-&gt;assertStatus(201);\n        $this-&gt;assertDatabaseHas('posts', [\n            'title' =&gt; $postData['title'],\n            'user_id' =&gt; $user-&gt;id\n        ]);\n    }\n\n    public function test_user_can_view_their_posts()\n    {\n        \/\/ \u6295\u7a3f\u3092\u6301\u3064\u30e6\u30fc\u30b6\u30fc\u3092\u4f5c\u6210\n        $user = User::factory()\n            -&gt;has(Post::factory()-&gt;count(3))\n            -&gt;create();\n\n        $response = $this-&gt;actingAs($user)\n            -&gt;get('\/my-posts');\n\n        $response-&gt;assertStatus(200)\n            -&gt;assertJsonCount(3, 'data');\n    }\n\n    public function test_post_deletion_removes_associated_comments()\n    {\n        \/\/ \u30b3\u30e1\u30f3\u30c8\u4ed8\u304d\u306e\u6295\u7a3f\u3092\u4f5c\u6210\n        $post = Post::factory()\n            -&gt;has(Comment::factory()-&gt;count(3))\n            -&gt;create();\n\n        \/\/ \u6295\u7a3f\u3092\u524a\u9664\n        $post-&gt;delete();\n\n        \/\/ \u30b3\u30e1\u30f3\u30c8\u3082\u524a\u9664\u3055\u308c\u3066\u3044\u308b\u3053\u3068\u3092\u78ba\u8a8d\n        $this-&gt;assertDatabaseMissing('comments', ['post_id' =&gt; $post-&gt;id]);\n    }\n}<\/pre>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"i-14\">\u8907\u96d1\u306a\u30c6\u30b9\u30c8\u30c7\u30fc\u30bf\u30bb\u30c3\u30c8\u306e\u4f5c\u6210<\/h3>\n\n\n\n<p>\u5b9f\u969b\u306e\u30a2\u30d7\u30ea\u30b1\u30fc\u30b7\u30e7\u30f3\u3067\u306f\u3001\u8907\u6570\u306e\u30e2\u30c7\u30eb\u304c\u7d61\u307f\u5408\u3046\u8907\u96d1\u306a\u30c6\u30b9\u30c8\u30b1\u30fc\u30b9\u304c\u5fc5\u8981\u306b\u306a\u308a\u307e\u3059\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 OrderTest extends TestCase\n{\n    use RefreshDatabase;\n\n    public function test_complete_order_process()\n    {\n        \/\/ \u5546\u54c1\u3092\u6301\u3064\u5e97\u8217\u3092\u4f5c\u6210\n        $shop = Shop::factory()\n            -&gt;has(Product::factory()-&gt;count(3))\n            -&gt;create();\n\n        \/\/ \u30ab\u30fc\u30c8\u9805\u76ee\u3092\u6301\u3064\u30e6\u30fc\u30b6\u30fc\u3092\u4f5c\u6210\n        $user = User::factory()\n            -&gt;has(CartItem::factory()-&gt;count(2)-&gt;state(function (array $attributes) use ($shop) {\n                return [\n                    'product_id' =&gt; $shop-&gt;products-&gt;random()-&gt;id\n                ];\n            }))\n            -&gt;create();\n\n        \/\/ \u914d\u9001\u5148\u4f4f\u6240\u3092\u4f5c\u6210\n        $address = Address::factory()-&gt;create([\n            'user_id' =&gt; $user-&gt;id\n        ]);\n\n        \/\/ \u6ce8\u6587\u51e6\u7406\u306e\u30c6\u30b9\u30c8\n        $response = $this-&gt;actingAs($user)\n            -&gt;post('\/orders', [\n                'address_id' =&gt; $address-&gt;id,\n                'payment_method' =&gt; 'credit_card'\n            ]);\n\n        $response-&gt;assertStatus(201);\n\n        \/\/ \u6ce8\u6587\u30c7\u30fc\u30bf\u306e\u691c\u8a3c\n        $this-&gt;assertDatabaseHas('orders', [\n            'user_id' =&gt; $user-&gt;id,\n            'status' =&gt; 'pending',\n            'address_id' =&gt; $address-&gt;id\n        ]);\n    }\n}<\/pre>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"i-15\">\u30c6\u30b9\u30c8\u30d1\u30d5\u30a9\u30fc\u30de\u30f3\u30b9\u306e\u6700\u9069\u5316<\/h3>\n\n\n\n<p>Factory\u3092\u4f7f\u7528\u3059\u308b\u969b\u306e\u30d1\u30d5\u30a9\u30fc\u30de\u30f3\u30b9\u6700\u9069\u5316\u30c6\u30af\u30cb\u30c3\u30af\u3092\u7d39\u4ecb\u3057\u307e\u3059\uff1a<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>make()\u306e\u6d3b\u7528<\/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=\"\">public function test_post_validation()\n{\n    \/\/ DB\u306b\u4fdd\u5b58\u305b\u305a\u306b\u30c6\u30b9\u30c8\u30c7\u30fc\u30bf\u3092\u751f\u6210\n    $postData = Post::factory()-&gt;make()-&gt;toArray();\n\n    $response = $this-&gt;post('\/posts', $postData);\n\n    $response-&gt;assertStatus(201);\n}<\/pre>\n\n\n\n<ol start=\"2\" class=\"wp-block-list\">\n<li><strong>\u72b6\u614b\u306e\u518d\u5229\u7528<\/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 PostTest extends TestCase\n{\n    private $user;\n    private $post;\n\n    protected function setUp(): void\n    {\n        parent::setUp();\n\n        \/\/ \u30c6\u30b9\u30c8\u3067\u5171\u901a\u3057\u3066\u4f7f\u7528\u3059\u308b\u30c7\u30fc\u30bf\u3092\u6e96\u5099\n        $this-&gt;user = User::factory()-&gt;create();\n        $this-&gt;post = Post::factory()-&gt;create([\n            'user_id' =&gt; $this-&gt;user-&gt;id\n        ]);\n    }\n\n    public function test_user_can_edit_their_post()\n    {\n        $this-&gt;actingAs($this-&gt;user)\n            -&gt;patch(\"\/posts\/{$this-&gt;post-&gt;id}\", [\n                'title' =&gt; 'Updated Title'\n            ])\n            -&gt;assertStatus(200);\n    }\n}<\/pre>\n\n\n\n<ol start=\"3\" class=\"wp-block-list\">\n<li><strong>\u30d0\u30c3\u30c1\u51e6\u7406\u306e\u6700\u9069\u5316<\/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=\"\">public function test_bulk_post_processing()\n{\n    \/\/ \u4e00\u62ec\u3067\u30c7\u30fc\u30bf\u3092\u4f5c\u6210\n    $posts = Post::factory()\n        -&gt;count(100)\n        -&gt;create();\n\n    \/\/ \u30af\u30a8\u30ea\u30ab\u30a6\u30f3\u30c8\u306e\u958b\u59cb\n    DB::enableQueryLog();\n\n    \/\/ \u30d0\u30c3\u30c1\u51e6\u7406\u306e\u5b9f\u884c\n    $result = ProcessPostsBatch::dispatch($posts-&gt;pluck('id'));\n\n    \/\/ \u30af\u30a8\u30ea\u6570\u306e\u78ba\u8a8d\n    $queryCount = count(DB::getQueryLog());\n    $this-&gt;assertLessThan(10, $queryCount, '\u30af\u30a8\u30ea\u6570\u304c\u591a\u3059\u304e\u307e\u3059');\n}<\/pre>\n\n\n\n<ol start=\"4\" class=\"wp-block-list\">\n<li><strong>\u30c8\u30e9\u30f3\u30b6\u30af\u30b7\u30e7\u30f3\u306e\u6d3b\u7528<\/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=\"\">use Illuminate\\Foundation\\Testing\\DatabaseTransactions;\n\nclass ComplexDataTest extends TestCase\n{\n    use DatabaseTransactions;\n\n    public function test_complex_data_relationships()\n    {\n        \/\/ \u5927\u91cf\u306e\u30c6\u30b9\u30c8\u30c7\u30fc\u30bf\u3092\u4f5c\u6210\n        $users = User::factory()\n            -&gt;has(Post::factory()-&gt;count(5))\n            -&gt;has(Comment::factory()-&gt;count(10))\n            -&gt;count(20)\n            -&gt;create();\n\n        \/\/ \u30c6\u30b9\u30c8\u5b9f\u884c\n        \/\/ \u30c8\u30e9\u30f3\u30b6\u30af\u30b7\u30e7\u30f3\u306b\u3088\u308a\u3001\u30c6\u30b9\u30c8\u7d42\u4e86\u6642\u306b\u81ea\u52d5\u7684\u306b\u30ed\u30fc\u30eb\u30d0\u30c3\u30af\n    }\n}<\/pre>\n\n\n\n<p>\u3053\u308c\u3089\u306e\u30c6\u30af\u30cb\u30c3\u30af\u3092\u9069\u5207\u306b\u7d44\u307f\u5408\u308f\u305b\u308b\u3053\u3068\u3067\u3001\u30c6\u30b9\u30c8\u306e\u5b9f\u884c\u6642\u9593\u3092\u77ed\u7e2e\u3057\u3064\u3064\u3001\u4fe1\u983c\u6027\u306e\u9ad8\u3044\u30c6\u30b9\u30c8\u3092\u5b9f\u73fe\u3067\u304d\u307e\u3059\u3002\u6b21\u306e\u30bb\u30af\u30b7\u30e7\u30f3\u3067\u306f\u3001\u5b9f\u52d9\u3067\u3088\u304f\u4f7f\u7528\u3059\u308bFactory\u30d1\u30bf\u30fc\u30f3\u306b\u3064\u3044\u3066\u8a73\u3057\u304f\u89e3\u8aac\u3057\u3066\u3044\u304d\u307e\u3059\u3002<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"i-16\">\u5b9f\u52d9\u3067\u3088\u304f\u4f7f\u3046Factory\u30d1\u30bf\u30fc\u30f3\u96c6<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"i-17\">\u30e6\u30fc\u30b6\u30fc\u95a2\u9023\u306e\u30c7\u30fc\u30bf\u751f\u6210\u30d1\u30bf\u30fc\u30f3<\/h3>\n\n\n\n<p>\u30e6\u30fc\u30b6\u30fc\u7ba1\u7406\u30b7\u30b9\u30c6\u30e0\u3067\u3088\u304f\u4f7f\u7528\u3055\u308c\u308b\u5b9f\u8df5\u7684\u306aFactory\u30d1\u30bf\u30fc\u30f3\u3092\u7d39\u4ecb\u3057\u307e\u3059\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=\"\">\/\/ UserFactory.php\nclass UserFactory extends Factory\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            'remember_token' =&gt; Str::random(10),\n            'email_verified_at' =&gt; now(),\n        ];\n    }\n\n    \/\/ \u30d7\u30ed\u30d5\u30a3\u30fc\u30eb\u60c5\u5831\u3092\u542b\u3080\u30e6\u30fc\u30b6\u30fc\n    public function withCompleteProfile()\n    {\n        return $this-&gt;afterCreating(function (User $user) {\n            UserProfile::factory()-&gt;create([\n                'user_id' =&gt; $user-&gt;id,\n                'phone' =&gt; $this-&gt;faker-&gt;phoneNumber(),\n                'address' =&gt; $this-&gt;faker-&gt;address(),\n                'birth_date' =&gt; $this-&gt;faker-&gt;dateTimeBetween('-60 years', '-18 years'),\n                'occupation' =&gt; $this-&gt;faker-&gt;jobTitle(),\n                'bio' =&gt; $this-&gt;faker-&gt;text(200)\n            ]);\n        });\n    }\n\n    \/\/ \u6a29\u9650\u3092\u6301\u3064\u30e6\u30fc\u30b6\u30fc\n    public function withPermissions(array $permissions = [])\n    {\n        return $this-&gt;afterCreating(function (User $user) use ($permissions) {\n            $defaultPermissions = ['read', 'write', 'delete'];\n            $perms = empty($permissions) ? $defaultPermissions : $permissions;\n\n            foreach ($perms as $permission) {\n                Permission::factory()-&gt;create([\n                    'user_id' =&gt; $user-&gt;id,\n                    'name' =&gt; $permission\n                ]);\n            }\n        });\n    }\n\n    \/\/ \u7d44\u7e54\u306b\u6240\u5c5e\u3059\u308b\u30e6\u30fc\u30b6\u30fc\n    public function withOrganization()\n    {\n        return $this-&gt;afterCreating(function (User $user) {\n            $organization = Organization::factory()-&gt;create();\n            $user-&gt;organizations()-&gt;attach($organization-&gt;id, [\n                'role' =&gt; $this-&gt;faker-&gt;randomElement(['member', 'admin', 'owner']),\n                'joined_at' =&gt; now()\n            ]);\n        });\n    }\n}\n\n\/\/ \u4f7f\u7528\u4f8b\n$users = User::factory()\n    -&gt;withCompleteProfile()\n    -&gt;withPermissions(['admin', 'manage_users'])\n    -&gt;withOrganization()\n    -&gt;count(5)\n    -&gt;create();<\/pre>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"i-18\">EC\u30b5\u30a4\u30c8\u3067\u306e\u5546\u54c1\u30c7\u30fc\u30bf\u751f\u6210\u4f8b<\/h3>\n\n\n\n<p>EC\u30b5\u30a4\u30c8\u3067\u5fc5\u8981\u3068\u306a\u308b\u5546\u54c1\u95a2\u9023\u306eFactory\u30d1\u30bf\u30fc\u30f3\u3067\u3059\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=\"\">\/\/ ProductFactory.php\nclass ProductFactory extends Factory\n{\n    public function definition()\n    {\n        return [\n            'name' =&gt; $this-&gt;faker-&gt;productName(),\n            'slug' =&gt; $this-&gt;faker-&gt;slug(),\n            'description' =&gt; $this-&gt;faker-&gt;paragraph(3),\n            'price' =&gt; $this-&gt;faker-&gt;numberBetween(1000, 100000),\n            'stock' =&gt; $this-&gt;faker-&gt;numberBetween(0, 100),\n            'status' =&gt; 'active'\n        ];\n    }\n\n    \/\/ \u5546\u54c1\u30d0\u30ea\u30a8\u30fc\u30b7\u30e7\u30f3\u4ed8\u304d\n    public function withVariations()\n    {\n        return $this-&gt;afterCreating(function (Product $product) {\n            \/\/ \u30b5\u30a4\u30ba\u30d0\u30ea\u30a8\u30fc\u30b7\u30e7\u30f3\n            $sizes = ['S', 'M', 'L', 'XL'];\n            \/\/ \u30ab\u30e9\u30fc\u30d0\u30ea\u30a8\u30fc\u30b7\u30e7\u30f3\n            $colors = ['Red', 'Blue', 'Black', 'White'];\n\n            foreach ($sizes as $size) {\n                foreach ($colors as $color) {\n                    ProductVariation::factory()-&gt;create([\n                        'product_id' =&gt; $product-&gt;id,\n                        'name' =&gt; \"$size - $color\",\n                        'sku' =&gt; Str::random(8),\n                        'additional_price' =&gt; $this-&gt;faker-&gt;numberBetween(0, 1000),\n                        'stock' =&gt; $this-&gt;faker-&gt;numberBetween(0, 50)\n                    ]);\n                }\n            }\n        });\n    }\n\n    \/\/ \u30ab\u30c6\u30b4\u30ea\u30fc\u3068\u5c5e\u6027\u4ed8\u304d\n    public function withCategoryAndAttributes()\n    {\n        return $this-&gt;afterCreating(function (Product $product) {\n            \/\/ \u30ab\u30c6\u30b4\u30ea\u30fc\u306e\u5272\u308a\u5f53\u3066\n            $category = Category::factory()-&gt;create();\n            $product-&gt;categories()-&gt;attach($category-&gt;id);\n\n            \/\/ \u5546\u54c1\u5c5e\u6027\u306e\u8ffd\u52a0\n            $attributes = [\n                'brand' =&gt; $this-&gt;faker-&gt;company(),\n                'material' =&gt; $this-&gt;faker-&gt;randomElement(['Cotton', 'Polyester', 'Wool', 'Silk']),\n                'weight' =&gt; $this-&gt;faker-&gt;numberBetween(100, 1000) . 'g',\n                'dimensions' =&gt; $this-&gt;faker-&gt;numberBetween(10, 100) . 'x' . \n                              $this-&gt;faker-&gt;numberBetween(10, 100) . 'x' . \n                              $this-&gt;faker-&gt;numberBetween(10, 100) . 'cm'\n            ];\n\n            foreach ($attributes as $key =&gt; $value) {\n                ProductAttribute::factory()-&gt;create([\n                    'product_id' =&gt; $product-&gt;id,\n                    'name' =&gt; $key,\n                    'value' =&gt; $value\n                ]);\n            }\n        });\n    }\n}<\/pre>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"i-19\">\u30d6\u30ed\u30b0\u8a18\u4e8b\u306e\u30c6\u30b9\u30c8\u30c7\u30fc\u30bf\u4f5c\u6210\u4f8b<\/h3>\n\n\n\n<p>\u30d6\u30ed\u30b0\u30b7\u30b9\u30c6\u30e0\u3067\u5fc5\u8981\u3068\u306a\u308bFactory\u30d1\u30bf\u30fc\u30f3\u3067\u3059\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=\"\">\/\/ ArticleFactory.php\nclass ArticleFactory extends Factory\n{\n    public function definition()\n    {\n        return [\n            'title' =&gt; $this-&gt;faker-&gt;sentence(),\n            'slug' =&gt; $this-&gt;faker-&gt;slug(),\n            'content' =&gt; $this-&gt;faker-&gt;paragraphs(5, true),\n            'excerpt' =&gt; $this-&gt;faker-&gt;paragraph(),\n            'published_at' =&gt; $this-&gt;faker-&gt;dateTimeBetween('-1 year', 'now'),\n            'status' =&gt; 'published'\n        ];\n    }\n\n    \/\/ SEO\u60c5\u5831\u4ed8\u304d\u306e\u8a18\u4e8b\n    public function withSEOData()\n    {\n        return $this-&gt;afterCreating(function (Article $article) {\n            SEOData::factory()-&gt;create([\n                'article_id' =&gt; $article-&gt;id,\n                'meta_title' =&gt; $this-&gt;faker-&gt;sentence(),\n                'meta_description' =&gt; $this-&gt;faker-&gt;text(160),\n                'keywords' =&gt; implode(',', $this-&gt;faker-&gt;words(5)),\n                'og_image' =&gt; $this-&gt;faker-&gt;imageUrl(1200, 630)\n            ]);\n        });\n    }\n\n    \/\/ \u95a2\u9023\u30b3\u30f3\u30c6\u30f3\u30c4\u4ed8\u304d\u306e\u8a18\u4e8b\n    public function withRelatedContent()\n    {\n        return $this-&gt;afterCreating(function (Article $article) {\n            \/\/ \u30bf\u30b0\u306e\u8ffd\u52a0\n            $tags = Tag::factory()-&gt;count(3)-&gt;create();\n            $article-&gt;tags()-&gt;attach($tags-&gt;pluck('id'));\n\n            \/\/ \u30ab\u30c6\u30b4\u30ea\u30fc\u306e\u8ffd\u52a0\n            $category = Category::factory()-&gt;create();\n            $article-&gt;category()-&gt;associate($category);\n            $article-&gt;save();\n\n            \/\/ \u95a2\u9023\u8a18\u4e8b\u306e\u8ffd\u52a0\n            $relatedArticles = Article::factory()-&gt;count(3)-&gt;create();\n            $article-&gt;relatedArticles()-&gt;attach($relatedArticles-&gt;pluck('id'));\n        });\n    }\n\n    \/\/ \u30b3\u30e1\u30f3\u30c8\u3068\u30ea\u30a2\u30af\u30b7\u30e7\u30f3\u4ed8\u304d\u306e\u8a18\u4e8b\n    public function withEngagement()\n    {\n        return $this-&gt;afterCreating(function (Article $article) {\n            \/\/ \u30b3\u30e1\u30f3\u30c8\u306e\u8ffd\u52a0\n            Comment::factory()\n                -&gt;count(5)\n                -&gt;create(['article_id' =&gt; $article-&gt;id]);\n\n            \/\/ \u30ea\u30a2\u30af\u30b7\u30e7\u30f3\u306e\u8ffd\u52a0\n            $reactionTypes = ['like', 'love', 'wow', 'sad', 'angry'];\n            foreach ($reactionTypes as $type) {\n                Reaction::factory()\n                    -&gt;count($this-&gt;faker-&gt;numberBetween(1, 20))\n                    -&gt;create([\n                        'article_id' =&gt; $article-&gt;id,\n                        'type' =&gt; $type\n                    ]);\n            }\n        });\n    }\n}\n\n\/\/ \u4f7f\u7528\u4f8b\n$articles = Article::factory()\n    -&gt;withSEOData()\n    -&gt;withRelatedContent()\n    -&gt;withEngagement()\n    -&gt;count(10)\n    -&gt;create();<\/pre>\n\n\n\n<p>\u3053\u308c\u3089\u306e\u30d1\u30bf\u30fc\u30f3\u306f\u3001\u5b9f\u969b\u306e\u30d7\u30ed\u30b8\u30a7\u30af\u30c8\u3067\u5fc5\u8981\u3068\u306a\u308b\u4e00\u822c\u7684\u306a\u30e6\u30fc\u30b9\u30b1\u30fc\u30b9\u3092\u30ab\u30d0\u30fc\u3057\u3066\u3044\u307e\u3059\u3002\u6b21\u306e\u30bb\u30af\u30b7\u30e7\u30f3\u3067\u306f\u3001\u3053\u308c\u3089\u306eFactory\u3092\u4f7f\u7528\u3059\u308b\u969b\u306e\u30d9\u30b9\u30c8\u30d7\u30e9\u30af\u30c6\u30a3\u30b9\u3068\u30c8\u30e9\u30d6\u30eb\u30b7\u30e5\u30fc\u30c6\u30a3\u30f3\u30b0\u306b\u3064\u3044\u3066\u89e3\u8aac\u3057\u307e\u3059\u3002<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"i-20\">Factory\u306e\u30d9\u30b9\u30c8\u30d7\u30e9\u30af\u30c6\u30a3\u30b9\u3068\u30c8\u30e9\u30d6\u30eb\u30b7\u30e5\u30fc\u30c6\u30a3\u30f3\u30b0<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"i-21\">Factory\u8a2d\u8a08\u6642\u306e\u6ce8\u610f\u70b9<\/h3>\n\n\n\n<p>Factory\u8a2d\u8a08\u6642\u306b\u8003\u616e\u3059\u3079\u304d\u91cd\u8981\u306a\u30dd\u30a4\u30f3\u30c8\u3092\u89e3\u8aac\u3057\u307e\u3059\uff1a<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>\u30c7\u30fc\u30bf\u306e\u4e00\u8cab\u6027\u3092\u4fdd\u3064<\/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=\"\">\/\/ \u826f\u3044\u4f8b\uff1a\u95a2\u9023\u30c7\u30fc\u30bf\u306e\u6574\u5408\u6027\u3092\u4fdd\u8a3c\nclass OrderFactory extends Factory\n{\n    public function definition()\n    {\n        $product = Product::factory()-&gt;create();\n        $quantity = $this-&gt;faker-&gt;numberBetween(1, 5);\n\n        return [\n            'product_id' =&gt; $product-&gt;id,\n            'quantity' =&gt; $quantity,\n            'unit_price' =&gt; $product-&gt;price,\n            'total_amount' =&gt; $product-&gt;price * $quantity,\n        ];\n    }\n}\n\n\/\/ \u60aa\u3044\u4f8b\uff1a\u30c7\u30fc\u30bf\u306e\u6574\u5408\u6027\u304c\u53d6\u308c\u3066\u3044\u306a\u3044\nclass OrderFactory extends Factory\n{\n    public function definition()\n    {\n        return [\n            'product_id' =&gt; Product::factory(),\n            'quantity' =&gt; $this-&gt;faker-&gt;numberBetween(1, 5),\n            'unit_price' =&gt; $this-&gt;faker-&gt;numberBetween(100, 1000),\n            'total_amount' =&gt; $this-&gt;faker-&gt;numberBetween(1000, 5000), \/\/ \u8a08\u7b97\u304c\u5408\u308f\u306a\u3044\n        ];\n    }\n}<\/pre>\n\n\n\n<ol start=\"2\" class=\"wp-block-list\">\n<li><strong>\u518d\u5229\u7528\u53ef\u80fd\u306a\u30c8\u30ec\u30a4\u30c8\u306e\u6d3b\u7528<\/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=\"\">\/\/ HasTimestamps.php\ntrait HasTimestamps\n{\n    public function withCustomTimestamps($createdAt = null, $updatedAt = null)\n    {\n        return $this-&gt;state(function (array $attributes) use ($createdAt, $updatedAt) {\n            return [\n                'created_at' =&gt; $createdAt ?? now(),\n                'updated_at' =&gt; $updatedAt ?? $createdAt ?? now(),\n            ];\n        });\n    }\n}\n\n\/\/ UserFactory.php\nclass UserFactory extends Factory\n{\n    use HasTimestamps;\n\n    \/\/ \u4ed6\u306e\u30d5\u30a1\u30af\u30c8\u30ea\u30fc\u3067\u3082\u540c\u3058\u30c8\u30ec\u30a4\u30c8\u3092\u518d\u5229\u7528\u53ef\u80fd\n}<\/pre>\n\n\n\n<ol start=\"3\" class=\"wp-block-list\">\n<li><strong>\u547d\u540d\u898f\u5247\u306e\u7d71\u4e00<\/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=\"\">\/\/ \u826f\u3044\u4f8b\uff1a\u4e00\u8cab\u3057\u305f\u547d\u540d\u898f\u5247\nclass ProductFactory extends Factory\n{\n    \/\/ \u72b6\u614b\u3092\u8868\u3059\u30e1\u30bd\u30c3\u30c9\u540d\u306f with~ \u307e\u305f\u306f as~ \u3067\u7d71\u4e00\n    public function withDiscount()\n    {\n        return $this-&gt;state([...]);\n    }\n\n    public function asOutOfStock()\n    {\n        return $this-&gt;state([...]);\n    }\n\n    \/\/ \u30ea\u30ec\u30fc\u30b7\u30e7\u30f3\u8ffd\u52a0\u306f has~ \u3067\u7d71\u4e00\n    public function hasReviews()\n    {\n        return $this-&gt;has(Review::factory()-&gt;count(3));\n    }\n}<\/pre>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"i-22\">\u3088\u304f\u3042\u308b\u30a8\u30e9\u30fc\u3068\u305d\u306e\u89e3\u6c7a\u65b9\u6cd5<\/h3>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>\u5faa\u74b0\u53c2\u7167\u306e\u554f\u984c<\/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=\"\">\/\/ \u554f\u984c\u306e\u3042\u308b\u30b3\u30fc\u30c9\nclass UserFactory extends Factory\n{\n    public function definition()\n    {\n        return [\n            'team_id' =&gt; Team::factory(), \/\/ Team\u30d5\u30a1\u30af\u30c8\u30ea\u30fc\u304c User \u3092\u5fc5\u8981\u3068\u3059\u308b\u5834\u5408\u3001\u5faa\u74b0\u53c2\u7167\u304c\u767a\u751f\n        ];\n    }\n}\n\n\/\/ \u89e3\u6c7a\u7b56\uff1aafterCreating \u3092\u4f7f\u7528\nclass UserFactory extends Factory\n{\n    public function definition()\n    {\n        return [\n            'name' =&gt; $this-&gt;faker-&gt;name(),\n        ];\n    }\n\n    public function withTeam()\n    {\n        return $this-&gt;afterCreating(function (User $user) {\n            $team = Team::factory()-&gt;create();\n            $user-&gt;update(['team_id' =&gt; $team-&gt;id]);\n        });\n    }\n}<\/pre>\n\n\n\n<ol start=\"2\" class=\"wp-block-list\">\n<li><strong>\u30e1\u30e2\u30ea\u30ea\u30fc\u30af\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=\"\">\/\/ \u30e1\u30e2\u30ea\u30ea\u30fc\u30af\u3092\u5f15\u304d\u8d77\u3053\u3059\u53ef\u80fd\u6027\u306e\u3042\u308b\u30b3\u30fc\u30c9\npublic function test_large_data_set()\n{\n    $users = User::factory()\n        -&gt;has(Post::factory()-&gt;count(100))\n        -&gt;count(1000)\n        -&gt;create();  \/\/ \u30e1\u30e2\u30ea\u3092\u5927\u91cf\u306b\u6d88\u8cbb\n\n    \/\/ \u30c6\u30b9\u30c8\u30b3\u30fc\u30c9\n}\n\n\/\/ \u6539\u5584\u7b56\uff1a\u30c1\u30e3\u30f3\u30af\u3092\u4f7f\u7528\npublic function test_large_data_set()\n{\n    User::factory()\n        -&gt;count(1000)\n        -&gt;create()\n        -&gt;chunk(100)\n        -&gt;each(function ($users) {\n            $users-&gt;each(function ($user) {\n                Post::factory()\n                    -&gt;count(100)\n                    -&gt;create(['user_id' =&gt; $user-&gt;id]);\n            });\n        });\n}<\/pre>\n\n\n\n<ol start=\"3\" class=\"wp-block-list\">\n<li><strong>\u30e6\u30cb\u30fc\u30af\u5236\u7d04\u306e\u885d\u7a81<\/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=\"\">\/\/ \u554f\u984c\u304c\u767a\u751f\u3057\u3084\u3059\u3044\u30b3\u30fc\u30c9\nclass UserFactory extends Factory\n{\n    public function definition()\n    {\n        return [\n            'email' =&gt; 'test@example.com', \/\/ \u56fa\u5b9a\u5024\u3060\u3068\u30e6\u30cb\u30fc\u30af\u5236\u7d04\u306b\u9055\u53cd\n        ];\n    }\n}\n\n\/\/ \u89e3\u6c7a\u7b56\uff1asequence \u306e\u6d3b\u7528\nclass UserFactory extends Factory\n{\n    public function definition()\n    {\n        return [\n            'email' =&gt; $this-&gt;faker-&gt;unique()-&gt;safeEmail(),\n        ];\n    }\n\n    public function sequence()\n    {\n        return $this-&gt;state(function ($attributes, $index) {\n            return [\n                'email' =&gt; \"test{$index}@example.com\",\n            ];\n        });\n    }\n}<\/pre>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"i-23\">\u30d1\u30d5\u30a9\u30fc\u30de\u30f3\u30b9\u3092\u8003\u616e\u3057\u305fFactory\u8a2d\u8a08<\/h3>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>\u9045\u5ef6\u30ed\u30fc\u30c7\u30a3\u30f3\u30b0\u306e\u6d3b\u7528<\/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 ProductFactory extends Factory\n{\n    public function definition()\n    {\n        return [\n            'category_id' =&gt; fn() =&gt; Category::factory(),\n            'name' =&gt; fn() =&gt; $this-&gt;faker-&gt;productName(),\n        ];\n    }\n}<\/pre>\n\n\n\n<ol start=\"2\" class=\"wp-block-list\">\n<li><strong>\u30d0\u30c3\u30c1\u51e6\u7406\u306e\u6700\u9069\u5316<\/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=\"\">\/\/ \u30d1\u30d5\u30a9\u30fc\u30de\u30f3\u30b9\u3092\u8003\u616e\u3057\u305f\u30d5\u30a1\u30af\u30c8\u30ea\u30fc\u8a2d\u8a08\nclass OrderFactory extends Factory\n{\n    public function definition()\n    {\n        return [\n            'user_id' =&gt; User::factory(),\n            'status' =&gt; 'pending',\n            'amount' =&gt; $this-&gt;faker-&gt;randomFloat(2, 10, 1000),\n        ];\n    }\n\n    \/\/ \u30d0\u30c3\u30c1\u51e6\u7406\u306b\u6700\u9069\u5316\u3055\u308c\u305f\u30e1\u30bd\u30c3\u30c9\n    public function createBatch($count)\n    {\n        \/\/ \u30c8\u30e9\u30f3\u30b6\u30af\u30b7\u30e7\u30f3\u3092\u4f7f\u7528\n        return DB::transaction(function () use ($count) {\n            $chunks = array_chunk(range(1, $count), 1000);\n            $orders = collect();\n\n            foreach ($chunks as $chunk) {\n                $batchOrders = static::factory()\n                    -&gt;count(count($chunk))\n                    -&gt;create();\n\n                $orders = $orders-&gt;concat($batchOrders);\n            }\n\n            return $orders;\n        });\n    }\n}<\/pre>\n\n\n\n<ol start=\"3\" class=\"wp-block-list\">\n<li><strong>\u30ad\u30e3\u30c3\u30b7\u30e5\u306e\u6d3b\u7528<\/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 ProductFactory extends Factory\n{\n    private static $categories = null;\n\n    public function definition()\n    {\n        \/\/ \u30ab\u30c6\u30b4\u30ea\u30fc\u3092\u30ad\u30e3\u30c3\u30b7\u30e5\n        if (self::$categories === null) {\n            self::$categories = Category::all();\n        }\n\n        return [\n            'category_id' =&gt; self::$categories-&gt;random()-&gt;id,\n            'name' =&gt; $this-&gt;faker-&gt;productName(),\n        ];\n    }\n}<\/pre>\n\n\n\n<p>\u3053\u308c\u3089\u306e\u30d9\u30b9\u30c8\u30d7\u30e9\u30af\u30c6\u30a3\u30b9\u3068\u30c8\u30e9\u30d6\u30eb\u30b7\u30e5\u30fc\u30c6\u30a3\u30f3\u30b0\u306e\u77e5\u8b58\u306f\u3001\u5b9f\u969b\u306e\u30d7\u30ed\u30b8\u30a7\u30af\u30c8\u3067Factory\u3092\u52b9\u679c\u7684\u306b\u6d3b\u7528\u3059\u308b\u4e0a\u3067\u975e\u5e38\u306b\u91cd\u8981\u3067\u3059\u3002\u7279\u306b\u5927\u898f\u6a21\u306a\u30d7\u30ed\u30b8\u30a7\u30af\u30c8\u3084\u8907\u96d1\u306a\u30c7\u30fc\u30bf\u69cb\u9020\u3092\u6271\u3046\u5834\u5408\u306f\u3001\u3053\u308c\u3089\u306e\u70b9\u306b\u6ce8\u610f\u3092\u6255\u3046\u3053\u3068\u3067\u3001\u3088\u308a\u4fdd\u5b88\u6027\u304c\u9ad8\u304f\u52b9\u7387\u7684\u306a\u30c6\u30b9\u30c8\u30b3\u30fc\u30c9\u3092\u5b9f\u73fe\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-2581","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\/2581","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=2581"}],"version-history":[{"count":2,"href":"https:\/\/dexall.co.jp\/articles\/index.php?rest_route=\/wp\/v2\/posts\/2581\/revisions"}],"predecessor-version":[{"id":2583,"href":"https:\/\/dexall.co.jp\/articles\/index.php?rest_route=\/wp\/v2\/posts\/2581\/revisions\/2583"}],"wp:attachment":[{"href":"https:\/\/dexall.co.jp\/articles\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=2581"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/dexall.co.jp\/articles\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=2581"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/dexall.co.jp\/articles\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=2581"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}