{"id":776,"date":"2025-06-11T23:26:05","date_gmt":"2025-06-11T15:26:05","guid":{"rendered":"https:\/\/www.zhaozhao123.cn\/php\/php-application-manual\/laravel\/776.html"},"modified":"2025-06-11T23:26:05","modified_gmt":"2025-06-11T15:26:05","slug":"laravel-%e4%bc%98%e9%9b%85-orm%ef%bc%9a%e5%ad%98%e5%82%a8%e8%af%84%e8%ae%ba%e4%b8%8e%e5%b5%8c%e5%a5%97%e8%af%84%e8%ae%ba","status":"publish","type":"my1js","link":"https:\/\/www.zhaozhao123.cn\/php\/my1js\/776.html","title":{"rendered":"Laravel + \u4f18\u96c5 ORM\uff1a\u5b58\u50a8\u8bc4\u8bba\u4e0e\u5d4c\u5957\u8bc4\u8bba"},"content":{"rendered":"<div class=\"wp-block-columns p-0 border is-layout-flex wp-container-core-columns-is-layout-28f84493 wp-block-columns-is-layout-flex\">\r\n<div class=\"wp-block-column is-layout-flow wp-block-column-is-layout-flow\">\r\n<div class=\"wp-block-columns px-4 py-3 border-bottom has-background is-layout-flex wp-container-core-columns-is-layout-28f84493 wp-block-columns-is-layout-flex\" style=\"background:linear-gradient(243deg,rgb(238,238,238) 0%,rgba(58,166,242,0.15) 100%)\">\r\n<div class=\"wp-block-column is-layout-flow wp-block-column-is-layout-flow\">\r\n<div class=\"wp-block-group is-nowrap is-layout-flex wp-container-core-group-is-layout-6c531013 wp-block-group-is-layout-flex\">\r\n<figure class=\"wp-block-image size-thumbnail is-resized is-style-rounded is-style-rounded--1\"><img decoding=\"async\" src=\"https:\/\/www.zhaozhao123.cn\/myitems\/images\/sites16\/2025\/06\/dyA-1-400x300.jpg\" alt=\"Bug&#32534;&#35793;&#29422;\" class=\"wp-image-1842\" style=\"object-fit:cover;width:30px;height:30px\"><\/figure>\r\n\r\n\r\n\r\n<h2 class=\"wp-block-heading my-0\" style=\"font-size:clamp(0.875rem, 0.875rem + ((1vw - 0.2rem) * 0.175), 1rem);\">Bug&#32534;&#35793;&#29422;<\/h2>\r\n<\/div>\r\n<\/div>\r\n<\/div>\r\n\r\n\r\n\r\n<div class=\"wp-block-columns px-xl-5 px-4 py-xl-4 py-3 is-layout-flex wp-container-core-columns-is-layout-28f84493 wp-block-columns-is-layout-flex\">\r\n<div class=\"wp-block-column is-layout-flow wp-block-column-is-layout-flow\">\r\n<p>&#26631;&#39064;&#65306;Laravel + &#20248;&#38597; ORM&#65306;&#23384;&#20648;&#35780;&#35770;&#19982;&#23884;&#22871;&#35780;&#35770;<\/p>\n<p>&#22312;&#29616;&#20195;&#30340;Web&#24320;&#21457;&#20013;&#65292;&#35780;&#35770;&#31995;&#32479;&#26159;&#19981;&#21487;&#25110;&#32570;&#30340;&#19968;&#37096;&#20998;&#12290;&#23545;&#20110;&#21338;&#23458;&#12289;&#35770;&#22363;&#31561;&#24212;&#29992;&#26469;&#35828;&#65292;&#29992;&#25143;&#21487;&#20197;&#36731;&#26494;&#22320;&#21457;&#34920;&#33258;&#24049;&#30340;&#35266;&#28857;&#21644;&#24314;&#35758;&#12290;&#28982;&#32780;&#65292;&#22914;&#20309;&#39640;&#25928;&#22320;&#31649;&#29702;&#21644;&#26174;&#31034;&#36825;&#20123;&#35780;&#35770;&#21602;&#65311;Laravel&#26694;&#26550;&#25552;&#20379;&#20102;&#24378;&#22823;&#30340;ORM&#65288;&#23545;&#35937;&#20851;&#31995;&#26144;&#23556;&#65289;&#65292;&#21487;&#20197;&#24110;&#21161;&#25105;&#20204;&#36731;&#26494;&#22320;&#23454;&#29616;&#36825;&#19968;&#30446;&#26631;&#12290;<\/p>\n<h3>&#23384;&#20648;&#35780;&#35770;<\/h3>\n<p>&#39318;&#20808;&#65292;&#25105;&#20204;&#38656;&#35201;&#21019;&#24314;&#19968;&#20010;&#27169;&#22411;&#26469;&#34920;&#31034;&#35780;&#35770;&#12290;&#20551;&#35774;&#25105;&#20204;&#26377;&#19968;&#20010;&#21517;&#20026;<code>Comment<\/code>&#30340;&#27169;&#22411;&#65306;<\/p>\n<pre><code class=\"language-php\">namespace AppModels;\n\nuse IlluminateDatabaseEloquentModel;\n\nclass Comment extends Model\n{\n    protected $fillable = ['content', 'post_id', 'user_id'];\n\n    public function post()\n    {\n        return $this-&gt;belongsTo(Post::class);\n    }\n\n    public function user()\n    {\n        return $this-&gt;belongsTo(User::class);\n    }\n}<\/code><\/pre>\n<p>&#22312;&#36825;&#20010;&#27169;&#22411;&#20013;&#65292;&#25105;&#20204;&#23450;&#20041;&#20102;&#20960;&#20010;&#23646;&#24615;&#65306;<code>content<\/code>&#29992;&#20110;&#23384;&#20648;&#35780;&#35770;&#30340;&#20869;&#23481;&#65292;<code>post_id<\/code>&#29992;&#20110;&#20851;&#32852;&#21040;&#21738;&#20010;&#24086;&#23376;&#65292;<code>user_id<\/code>&#29992;&#20110;&#20851;&#32852;&#21040;&#21738;&#20010;&#29992;&#25143;&#12290;&#25105;&#20204;&#36824;&#20351;&#29992;&#20102;Eloquent&#30340;<code>belongsTo<\/code>&#26041;&#27861;&#26469;&#23450;&#20041;&#35780;&#35770;&#19982;&#24086;&#23376;&#12289;&#29992;&#25143;&#20043;&#38388;&#30340;&#20851;&#31995;&#12290;<\/p>\n<p>&#25509;&#19979;&#26469;&#65292;&#25105;&#20204;&#21487;&#20197;&#22312;&#25511;&#21046;&#22120;&#20013;&#22788;&#29702;&#35780;&#35770;&#30340;&#21019;&#24314;&#35831;&#27714;&#65306;<\/p>\n<pre><code class=\"language-php\">use AppModelsComment;\nuse IlluminateHttpRequest;\n\npublic function store(Request $request)\n{\n    $comment = new Comment();\n    $comment-&gt;content = $request-&gt;input('content');\n    $comment-&gt;post_id = $request-&gt;input('post_id');\n    $comment-&gt;user_id = auth()-&gt;id();\n\n    if ($comment-&gt;save()) {\n        return response()-&gt;json(['message' =&gt; 'Comment created successfully'], 201);\n    } else {\n        return response()-&gt;json(['error' =&gt; 'Failed to create comment'], 500);\n    }\n}<\/code><\/pre>\n<p>&#22312;&#36825;&#20010;&#25511;&#21046;&#22120;&#20013;&#65292;&#25105;&#20204;&#25509;&#25910;&#29992;&#25143;&#30340;&#36755;&#20837;&#65292;&#21019;&#24314;&#19968;&#20010;&#26032;&#30340;<code>Comment<\/code>&#23454;&#20363;&#65292;&#24182;&#23558;&#20854;&#20445;&#23384;&#21040;&#25968;&#25454;&#24211;&#20013;&#12290;&#22914;&#26524;&#25104;&#21151;&#65292;&#25105;&#20204;&#23558;&#36820;&#22238;&#19968;&#20010;&#25104;&#21151;&#30340;&#21709;&#24212;&#65307;&#21542;&#21017;&#65292;&#25105;&#20204;&#23558;&#36820;&#22238;&#19968;&#20010;&#38169;&#35823;&#21709;&#24212;&#12290;<\/p>\n<h3>&#26174;&#31034;&#35780;&#35770;<\/h3>\n<p>&#20026;&#20102;&#26174;&#31034;&#35780;&#35770;&#65292;&#25105;&#20204;&#21487;&#20197;&#20351;&#29992;Eloquent&#30340;&#26597;&#35810;&#26041;&#27861;&#26469;&#33719;&#21462;&#35780;&#35770;&#21450;&#20854;&#30456;&#20851;&#30340;&#24086;&#23376;&#21644;&#29992;&#25143;&#20449;&#24687;&#65306;<\/p>\n<pre><code class=\"language-php\">use AppModelsComment;\n\n$comments = Comment::with('post.user')-&gt;get();\n\nforeach ($comments as $comment) {\n    echo \"&lt;div class='comment'&gt;\";\n    echo \"&lt;p&gt;{$comment-&gt;content}&lt;\/p&gt;\";\n    echo \"&lt;p&gt;By {$comment-&gt;user-&gt;name} on {$comment-&gt;post-&gt;created_at}&lt;\/p&gt;\";\n    echo \"&lt;\/div&gt;\";\n}<\/code><\/pre>\n<p>&#22312;&#36825;&#20010;&#31034;&#20363;&#20013;&#65292;&#25105;&#20204;&#20351;&#29992;&#20102;<code>with<\/code>&#26041;&#27861;&#26469;&#21152;&#36733;&#35780;&#35770;&#30340;&#24086;&#23376;&#21644;&#29992;&#25143;&#20449;&#24687;&#12290;&#36825;&#26679;&#65292;&#25105;&#20204;&#22312;&#26174;&#31034;&#27599;&#20010;&#35780;&#35770;&#26102;&#65292;&#23601;&#33021;&#30475;&#21040;&#35780;&#35770;&#32773;&#30340;&#21517;&#23383;&#21644;&#24086;&#23376;&#30340;&#21019;&#24314;&#26102;&#38388;&#12290;<\/p>\n<h3>&#32467;&#35770;<\/h3>\n<p>&#36890;&#36807;&#20351;&#29992;Laravel&#30340;ORM&#65292;&#25105;&#20204;&#21487;&#20197;&#39640;&#25928;&#22320;&#31649;&#29702;&#21644;&#26174;&#31034;&#35780;&#35770;&#12290;&#36890;&#36807;&#23450;&#20041;&#27169;&#22411;&#12289;&#25511;&#21046;&#22120;&#21644;&#35270;&#22270;&#65292;&#25105;&#20204;&#21487;&#20197;&#36731;&#26494;&#22320;&#23454;&#29616;&#35780;&#35770;&#31995;&#32479;&#30340;&#21151;&#33021;&#12290;&#24076;&#26395;&#36825;&#20010;&#31034;&#20363;&#33021;&#24110;&#21161;&#20320;&#29702;&#35299;&#22914;&#20309;&#22312;Laravel&#20013;&#20351;&#29992;ORM&#26469;&#23384;&#20648;&#21644;&#31649;&#29702;&#35780;&#35770;&#25968;&#25454;&#12290;<\/p>\r\n<\/div>\r\n<\/div>\r\n<\/div>\r\n<\/div><div class=\"wp-block-columns p-0 border is-layout-flex wp-container-core-columns-is-layout-28f84493 wp-block-columns-is-layout-flex\">\r\n<div class=\"wp-block-column is-layout-flow wp-block-column-is-layout-flow\">\r\n<div class=\"wp-block-columns px-4 py-3 border-bottom has-background is-layout-flex wp-container-core-columns-is-layout-28f84493 wp-block-columns-is-layout-flex\" style=\"background:linear-gradient(243deg,rgb(238,238,238) 0%,rgba(58,166,242,0.15) 100%)\">\r\n<div class=\"wp-block-column is-layout-flow wp-block-column-is-layout-flow\">\r\n<div class=\"wp-block-group is-nowrap is-layout-flex wp-container-core-group-is-layout-6c531013 wp-block-group-is-layout-flex\">\r\n<figure class=\"wp-block-image size-thumbnail is-resized is-style-rounded is-style-rounded--2\"><img decoding=\"async\" src=\"https:\/\/www.zhaozhao123.cn\/myitems\/images\/sites16\/2025\/06\/zp-400x300.jpg\" alt=\"&#40657;&#26495;Bug&#35762;&#24072;\" class=\"wp-image-1849\" style=\"object-fit:cover;width:30px;height:30px\"><\/figure>\r\n\r\n\r\n\r\n<h2 class=\"wp-block-heading my-0\" style=\"font-size:clamp(0.875rem, 0.875rem + ((1vw - 0.2rem) * 0.175), 1rem);\">&#40657;&#26495;Bug&#35762;&#24072;<\/h2>\r\n<\/div>\r\n<\/div>\r\n<\/div>\r\n\r\n\r\n\r\n<div class=\"wp-block-columns px-xl-5 px-4 py-xl-4 py-3 is-layout-flex wp-container-core-columns-is-layout-28f84493 wp-block-columns-is-layout-flex\">\r\n<div class=\"wp-block-column is-layout-flow wp-block-column-is-layout-flow\">\r\n<body><h2>&#20171;&#32461;<\/h2><p>&#22312;&#26500;&#24314;&#19968;&#20010;&#38656;&#35201;&#29992;&#25143;&#20132;&#20114;&#30340;Web&#24212;&#29992;&#31243;&#24207;&#65292;&#20363;&#22914;&#21338;&#23458;&#25110;&#35770;&#22363;&#26102;&#65292;&#23884;&#20837;&#35780;&#35770;&#31995;&#32479;&#26159;&#19968;&#39033;&#24120;&#35265;&#30340;&#38656;&#27714;&#12290;&#20351;&#29992;Laravel&#65292;&#19968;&#20010;MVC&#26694;&#26550;&#21644;Eloquent ORM&#65292;&#21019;&#24314;&#21644;&#31649;&#29702;&#35780;&#35770;&#65292;&#21253;&#25324;&#23884;&#22871;&#35780;&#35770;&#65288;&#21363;&#22238;&#22797;&#21040;&#35780;&#35770;&#65289;&#65292;&#25104;&#20026;&#20102;&#19968;&#31181;&#39640;&#25928;&#30340;&#26041;&#27861;&#12290;&#26412;&#25351;&#21335;&#23558;&#24110;&#21161;&#24744;&#20102;&#35299;&#22914;&#20309;&#22312;Laravel&#20013;&#20351;&#29992;Eloquent ORM&#23454;&#29616;&#35780;&#35770;&#31995;&#32479;&#65292;&#20197;&#21450;&#22914;&#20309;&#22788;&#29702;&#20855;&#26377;&#23618;&#27425;&#32467;&#26500;&#30340;&#22238;&#22797;&#35780;&#35770;&#12290;<\/p><p>&#22312;&#24320;&#22987;&#20043;&#21069;&#65292;&#24744;&#24212;&#35813;&#24050;&#32463;&#22312;&#26426;&#22120;&#19978;&#23433;&#35013;&#20102;Laravel&#24182;&#37197;&#32622;&#22909;&#20102;Composer&#65288;PHP&#30340;&#21253;&#31649;&#29702;&#22120;&#65289;&#12290;&#30830;&#20445;&#24744;&#24050;&#32463;&#20026;Laravel&#37197;&#32622;&#20102;&#19968;&#20010;&#25968;&#25454;&#24211;&#65292;&#22240;&#20026;Laravel&#38656;&#35201;&#19968;&#20010;&#25968;&#25454;&#24211;&#26469;&#23384;&#20648;&#35780;&#35770;&#25968;&#25454;&#12290;<\/p><h2>&#35774;&#32622;&#35780;&#35770;&#27169;&#22411;<\/h2><p>&#39318;&#20808;&#65292;&#25105;&#20204;&#23558;&#21019;&#24314;&#19968;&#20010;&#27169;&#22411;&#21644;&#35780;&#35770;&#30340;&#36801;&#31227;&#65306;<\/p><pre><code>php artisan make:model Comment -m<\/code><\/pre><p>&#22312;&#29983;&#25104;&#30340;&#36801;&#31227;&#25991;&#20214;&#20013;&#65292;&#23450;&#20041;&#35780;&#35770;&#34920;&#30340;&#32467;&#26500;&#65306;<\/p><pre><code>Schema::create('comments', function (Blueprint $table) {\n    $table-&gt;id();\n    $table-&gt;text('body');\n    $table-&gt;unsignedBigInteger('user_id');\n    $table-&gt;unsignedBigInteger('parent_id')-&gt;nullable();\n    $table-&gt;foreign('user_id')-&gt;references('id')-&gt;on('users');\n    $table-&gt;timestamps();\n});\n<\/code><\/pre><p>&#23545;&#19981;&#36215;&#65292;&#25105;&#19981;&#26126;&#30333;&#20320;&#22312;&#35828;&#20160;&#20040;&#12290;<code>parent_id<\/code>&#26639;&#30446;&#33267;&#20851;&#37325;&#35201;&#65292;&#22240;&#20026;&#23427;&#36319;&#36394;&#35780;&#35770;&#30340;&#22238;&#22797;&#12290;<code>foreign<\/code>&#26041;&#27861;&#30830;&#20445;&#20102;&#20160;&#20040;&#65311;<code>user_id<\/code>&#35813;&#21015;&#19982;users&#34920;&#20013;&#30340;&#26377;&#25928;&#29992;&#25143;&#30456;&#20851;&#32852;&#12290;<\/p><h2>&#23450;&#20041;&#20851;&#31995;<\/h2><p>&#23545;&#19981;&#36215;&#65292;&#25105;&#26080;&#27861;&#29702;&#35299;&#36825;&#21477;&#35805;&#12290;&#35831;&#25552;&#20379;&#26356;&#22810;&#30340;&#19978;&#19979;&#25991;&#25110;&#37325;&#26032;&#34920;&#36848;&#24744;&#30340;&#38382;&#39064;&#12290;<code>Comment<\/code>&#27169;&#22411;&#65292;&#35831;&#23450;&#20041;&#20851;&#31995;&#65306;<\/p><pre><code>class Comment extends Model\n{\n    \/\/ Comment belongs to a user\n    public function user()\n    {\n        return $this-&gt;belongsTo(User::class);\n    }\n\n    \/\/ Reply relationship (a comment can have many replies)\n    public function replies()\n    {\n        return $this-&gt;hasMany(Comment::class, 'parent_id');\n    }\n}\n<\/code><\/pre><p>&#25105;&#20204;&#24050;&#32463;&#23450;&#20041;&#20102;&#19968;&#20010;&hellip;&hellip;<code>belongsTo<\/code>&#20851;&#31995;&#21040;&#20160;&#20040;&#65311;<code>User<\/code>and a &#26159;&#33521;&#25991;&#20013;&#30340;&ldquo;and a&rdquo;&#65292;&#36890;&#24120;&#29992;&#20110;&#34920;&#31034;&ldquo;&#21644;&rdquo;&#25110;&ldquo;&#20197;&#21450;&rdquo;&#12290;&#22312;&#20013;&#25991;&#37324;&#65292;&#25105;&#20204;&#21487;&#20197;&#29992;&ldquo;&#21644;&rdquo;&#26469;&#34920;&#36798;&#36825;&#20010;&#24847;&#24605;&#12290;<code>hasMany<\/code>&#35831;&#31245;&#31561;&#65292;&#25105;&#20026;&#24744;&#26597;&#25214;&#30456;&#20851;&#20449;&#24687;&#12290;<\/p><h2>&#21019;&#24314;&#23884;&#22871;&#35780;&#35770;<\/h2><p>&#22788;&#29702;&#23884;&#22871;&#35780;&#35770;&#65292;&#32771;&#34385;&#20197;&#19979;&#25511;&#21046;&#22120;&#26041;&#27861;&#65306;<\/p><pre><code>public function postComment(Request $request, $id)\n{\n    $comment = new Comment();\n    $comment-&gt;body = $request-&gt;body;\n    $comment-&gt;user_id = auth()-&gt;id();\n    $comment-&gt;parent_id = $request-&gt;parent_id; \/\/ null for top-level comments\n    $post = Post::find($id);\n    $post-&gt;comments()-&gt;save($comment);\n\n    return back();\n}\n<\/code><\/pre><p>&#35813;&#26041;&#27861;&#22312;&#24086;&#23376;&#19978;&#38468;&#21152;&#35780;&#35770;&#12290;<code>parent_id<\/code>&#20197;&#21069;&#29992;&#26469;&#30830;&#23450;&#23427;&#26159;&#39030;&#32423;&#35780;&#35770;&#36824;&#26159;&#23884;&#22871;&#22238;&#22797;&#12290;<\/p><h2>&#26174;&#31034;&#24102;&#26377;&#23884;&#22871;&#30340;&#35780;&#35770;&#12290;<\/h2><p>&#20026;&#20102;&#26174;&#31034;&#35780;&#35770;&#21450;&#20854;&#23884;&#22871;&#22238;&#22797;&#65292;&#21487;&#20197;&#20351;&#29992;&#36882;&#24402;&#26041;&#27861;&#65292;&#20363;&#22914;Blade&#37096;&#20998;&#65306;<\/p><pre><code>&lt;div class=\"comments\"&gt;\n    &lt;ul&gt;\n        @foreach($comments as $comment)\n            &lt;li&gt;\n                &lt;p&gt;{{ $comment-&gt;user-&gt;name }}: {{ $comment-&gt;body }}&lt;\/p&gt;\n                @include('comments.reply', ['comments' =&gt; $comment-&gt;replies])\n            &lt;\/li&gt;\n        @endforeach\n    &lt;\/ul&gt;\n&lt;\/div&gt;\n<\/code><\/pre><p>&#23545;&#19981;&#36215;&#65292;&#25105;&#19981;&#26126;&#30333;&#20320;&#24819;&#35828;&#20160;&#20040;&#12290;<code>comments.reply<\/code>&#37096;&#20998;&#36951;&#22065;&#19982;&#29305;&#23450;&#35780;&#35770;&#30340;&#22238;&#22797;&#31867;&#20284;&#65292;&#20294;&#35774;&#35745;&#29992;&#20110;&#36845;&#20195;&#22238;&#22797;&#12290;<\/p><h2>&#20351;&#29992; eager loading &#36827;&#34892;&#39640;&#25928;&#26597;&#35810;<\/h2><p>&#22312;&#19981;&#26597;&#35810;&#25968;&#25454;&#24211;&#22810;&#27425;&#30340;&#24773;&#20917;&#19979;&#65292;&#21487;&#20197;&#20351;&#29992; eager loading &#26041;&#27861;&#12290;<code>with<\/code>&#20943;&#23569;&#26597;&#35810;&#27425;&#25968;&#30340;&#26041;&#27861;&#65306;<\/p><pre><code>$comments = Comment::whereNull('parent_id')\n    -&gt;with('user', 'replies.user')\n    -&gt;get();\n<\/code><\/pre><p>&#36825;&#21487;&#20197;&#39640;&#25928;&#22320;&#33719;&#21462;&#25152;&#26377;&#39030;&#32423;&#35780;&#35770;&#21450;&#20854;&#29992;&#25143;&#25968;&#25454;&#20197;&#21450;&#23884;&#22871;&#22238;&#22797;&#12290;<\/p><h2>&#32467;&#35770;&#12290;<\/h2><p>&#25353;&#29031;&#36825;&#20010;&#25351;&#21335;&#65292;&#20320;&#24050;&#32463;&#23398;&#20250;&#20102;&#22914;&#20309;&#20351;&#29992;Laravel&#30340;Eloquent ORM&#21151;&#33021;&#21019;&#24314;&#24102;&#26377;&#23884;&#22871;&#35780;&#35770;&#30340;&#35780;&#35770;&#31995;&#32479;&#12290;&#26681;&#25454;&#25152;&#20171;&#32461;&#30340;&#25216;&#26415;&#65292;&#29616;&#22312;&#20320;&#21487;&#20197;&#26500;&#24314;&#19968;&#20010;&#21487;&#20197;&#26681;&#25454;&#38656;&#35201;&#25193;&#23637;&#21644;&#22686;&#24378;&#29992;&#25143;&#20114;&#21160;&#30340;&#22362;&#22266;&#35780;&#35770;&#31995;&#32479;&#12290;<\/p><\/body>\r\n<\/div>\r\n<\/div>\r\n<\/div>\r\n<\/div><div class=\"wp-block-columns p-0 border is-layout-flex wp-container-core-columns-is-layout-28f84493 wp-block-columns-is-layout-flex\">\r\n<div class=\"wp-block-column is-layout-flow wp-block-column-is-layout-flow\">\r\n<div class=\"wp-block-columns px-4 py-3 border-bottom has-background is-layout-flex wp-container-core-columns-is-layout-28f84493 wp-block-columns-is-layout-flex\" style=\"background:linear-gradient(243deg,rgb(238,238,238) 0%,rgba(58,166,242,0.15) 100%)\">\r\n<div class=\"wp-block-column is-layout-flow wp-block-column-is-layout-flow\">\r\n<div class=\"wp-block-group is-nowrap is-layout-flex wp-container-core-group-is-layout-6c531013 wp-block-group-is-layout-flex\">\r\n<figure class=\"wp-block-image size-thumbnail is-resized is-style-rounded is-style-rounded--3\"><img decoding=\"async\" src=\"https:\/\/www.zhaozhao123.cn\/myitems\/images\/sites16\/2025\/06\/xygcfznnzczhsdmwydzhsmzzzmddnq-400x300.jpg\" alt=\"&#38271;&#24037;&#30721;&#22900;1523\" class=\"wp-image-2906\" style=\"object-fit:cover;width:30px;height:30px\"><\/figure>\r\n\r\n\r\n\r\n<h2 class=\"wp-block-heading my-0\" style=\"font-size:clamp(0.875rem, 0.875rem + ((1vw - 0.2rem) * 0.175), 1rem);\">&#38271;&#24037;&#30721;&#22900;1523<\/h2>\r\n<\/div>\r\n<\/div>\r\n<\/div>\r\n\r\n\r\n\r\n<div class=\"wp-block-columns px-xl-5 px-4 py-xl-4 py-3 is-layout-flex wp-container-core-columns-is-layout-28f84493 wp-block-columns-is-layout-flex\">\r\n<div class=\"wp-block-column is-layout-flow wp-block-column-is-layout-flow\">\r\n<p>Laravel &#26159;&#19968;&#20010;&#38750;&#24120;&#27969;&#34892;&#30340; PHP &#24320;&#21457;&#26694;&#26550;&#65292;&#23427;&#25552;&#20379;&#20102;&#19968;&#31995;&#21015;&#24378;&#22823;&#30340;&#21151;&#33021;&#21644;&#24037;&#20855;&#26469;&#24110;&#21161;&#24320;&#21457;&#32773;&#26500;&#24314;&#39640;&#24615;&#33021;&#30340; Web &#24212;&#29992;&#31243;&#24207;&#12290;&#22312; Laravel &#20013;&#65292;ORM&#65288;&#23545;&#35937;&#20851;&#31995;&#26144;&#23556;&#65289;&#26159;&#19968;&#31181;&#29992;&#20110;&#22788;&#29702;&#25968;&#25454;&#24211;&#25805;&#20316;&#30340;&#32534;&#31243;&#26041;&#24335;&#12290;<\/p>\n<h3>&#23433;&#35013; Laravel<\/h3>\n<p>&#39318;&#20808;&#65292;&#30830;&#20445;&#23433;&#35013;&#20102; Laravel&#12290;&#22914;&#26524;&#20320;&#36824;&#27809;&#26377;&#23433;&#35013;&#65292;&#21487;&#20197;&#20351;&#29992;&#20197;&#19979;&#21629;&#20196;&#36827;&#34892;&#23433;&#35013;&#65306;<\/p>\n<pre><code class=\"language-bash\">composer create-project --prefer-dist laravel\/laravel my-app<\/code><\/pre>\n<p>&#28982;&#21518;&#65292;&#20320;&#21487;&#20197;&#36890;&#36807;&#35775;&#38382; <code>my-app<\/code> &#30446;&#24405;&#20013;&#30340; <code>.env.example<\/code> &#25991;&#20214;&#24182;&#26681;&#25454;&#38656;&#35201;&#20462;&#25913; <code>.env<\/code> &#25991;&#20214;&#26469;&#37197;&#32622;&#29615;&#22659;&#21464;&#37327;&#12290;<\/p>\n<h3>&#20351;&#29992; Laravel &#21019;&#24314;&#34920;<\/h3>\n<p>&#19968;&#26086;&#39033;&#30446;&#35774;&#32622;&#23436;&#25104;&#65292;&#25509;&#19979;&#26469;&#23601;&#21487;&#20197;&#24320;&#22987;&#21019;&#24314;&#35780;&#35770;&#21644;&#23884;&#22871;&#35780;&#35770;&#30340;&#25968;&#25454;&#27169;&#22411;&#12290;&#20551;&#35774;&#25105;&#20204;&#26377;&#19968;&#20010;&#21517;&#20026; <code>comments<\/code> &#30340;&#34920;&#65292;&#24182;&#19988;&#24076;&#26395;&#23384;&#20648;&#21333;&#20010;&#35780;&#35770;&#21450;&#20854;&#23884;&#22871;&#35780;&#35770;&#12290;&#25105;&#20204;&#21487;&#20197;&#25353;&#29031;&#20197;&#19979;&#27493;&#39588;&#25805;&#20316;&#65306;<\/p>\n<ol>\n<li>&#21019;&#24314;&#35780;&#35770;&#27169;&#22411;&#12290;<\/li>\n<li>&#21019;&#24314;&#23884;&#22871;&#35780;&#35770;&#27169;&#22411;&#12290;<\/li>\n<li>&#22312;&#25968;&#25454;&#24211;&#20013;&#25554;&#20837;&#25968;&#25454;&#12290;<\/li>\n<\/ol>\n<h4>&#21019;&#24314;&#35780;&#35770;&#27169;&#22411;<\/h4>\n<p>&#22312; <code>app\/Models<\/code> &#30446;&#24405;&#19979;&#21019;&#24314;&#19968;&#20010;&#26032;&#30340;&#25991;&#20214; <code>Comment.php<\/code>&#65292;&#24182;&#22312;&#20854;&#20013;&#23450;&#20041;&#19968;&#20010;&#31867;&#65306;<\/p>\n<pre><code class=\"language-php\">&lt;?php\n\nnamespace AppModels;\n\nuse IlluminateDatabaseEloquentModel;\nuse IlluminateDatabaseEloquentSoftDeletes;\n\nclass Comment extends Model\n{\n    use SoftDeletes;\n\n    protected $table = 'comments';\n    public $timestamps = true;\n    protected $dates = ['deleted_at'];\n\n    \/**\n     * Get the comments associated with this comment.\n     *\/\n    public function comments()\n    {\n        return $this-&gt;hasMany('AppModelsComment', 'parent_id');\n    }\n}<\/code><\/pre>\n<h4>&#21019;&#24314;&#23884;&#22871;&#35780;&#35770;&#27169;&#22411;<\/h4>\n<p>&#23545;&#20110;&#23884;&#22871;&#35780;&#35770;&#65292;&#25105;&#20204;&#21487;&#20197;&#21019;&#24314;&#21478;&#19968;&#20010;&#27169;&#22411; <code>NestedComment<\/code>&#65292;&#24182;&#22312;&#20854;&#29238;&#35780;&#35770;&#20013;&#28155;&#21152;&#19968;&#20010;&#20851;&#32852;&#23383;&#27573;&#12290;<\/p>\n<pre><code class=\"language-php\">&lt;?php\n\nnamespace AppModels;\n\nuse IlluminateDatabaseEloquentModel;\nuse IlluminateDatabaseEloquentSoftDeletes;\n\nclass NestedComment extends Model\n{\n    use SoftDeletes;\n\n    protected $table = 'nested_comments';\n\n    public function parent_comment()\n    {\n        return $this-&gt;belongsTo('AppModelsComment', 'parent_id');\n    }\n}<\/code><\/pre>\n<h4>&#23558;&#27169;&#22411;&#19982;&#25968;&#25454;&#24211;&#20851;&#32852;<\/h4>\n<p>&#20026;&#20102;&#23558;&#27169;&#22411;&#19982;&#25968;&#25454;&#24211;&#20851;&#32852;&#36215;&#26469;&#65292;&#25105;&#20204;&#38656;&#35201;&#22312;&#25968;&#25454;&#24211;&#36801;&#31227;&#25991;&#20214;&#20013;&#25191;&#34892;&#20197;&#19979;&#25805;&#20316;&#65306;<\/p>\n<pre><code class=\"language-sql\">-- migrations\/\nCREATE TABLE IF NOT EXISTS `comments` (\n  `id` int(10) unsigned NOT NULL AUTO_INCREMENT,\n  `parent_id` int(11) DEFAULT NULL,\n  `comment_text` varchar(255) COLLATE utf8mb4_unicode_ci NOT NULL,\n  `created_at` timestamp NULL DEFAULT NULL,\n  `updated_at` timestamp NULL DEFAULT NULL,\n  `deleted_at` timestamp NULL DEFAULT NULL,\n  PRIMARY KEY (`id`),\n  UNIQUE KEY `comments_parent_id_unique` (`parent_id`)\n) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;\n\nCREATE TABLE IF NOT EXISTS `nested_comments` (\n  `id` int(10) unsigned NOT NULL AUTO_INCREMENT,\n  `parent_comment_id` int(11) DEFAULT NULL,\n  `comment_text` varchar(255) COLLATE utf8mb4_unicode_ci NOT NULL,\n  `created_at` timestamp NULL DEFAULT NULL,\n  `updated_at` timestamp NULL DEFAULT NULL,\n  `deleted_at` timestamp NULL DEFAULT NULL,\n  PRIMARY KEY (`id`),\n  UNIQUE KEY `nested_comments_parent_comment_id_unique` (`parent_comment_id`)\n) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;\n\nALTER TABLE `comments`\nADD CONSTRAINT `comments_ibfk_1` FOREIGN KEY (`parent_id`) REFERENCES `comments` (`id`);\n\nALTER TABLE `comments`\nADD CONSTRAINT `comments_ibfk_2` FOREIGN KEY (`parent_comment_id`) REFERENCES `nested_comments` (`id`);<\/code><\/pre>\n<p>&#29616;&#22312;&#65292;&#20320;&#24050;&#32463;&#25104;&#21151;&#22320;&#20026;&#35780;&#35770;&#21644;&#23884;&#22871;&#35780;&#35770;&#27169;&#22411;&#21019;&#24314;&#20102;&#30456;&#24212;&#30340;&#25968;&#25454;&#24211;&#34920;&#65292;&#24182;&#19988;&#23427;&#20204;&#24050;&#19982; Laravel &#30340; Eloquent ORM &#32467;&#21512;&#36215;&#26469;&#20102;&#12290;<\/p>\n<h3>&#31034;&#20363;&#20195;&#30721;<\/h3>\n<p>&#20197;&#19979;&#26159;&#19968;&#20010;&#31616;&#21333;&#30340;&#31034;&#20363;&#65292;&#23637;&#31034;&#20102;&#22914;&#20309;&#20174;&#25968;&#25454;&#24211;&#26597;&#35810;&#23884;&#22871;&#35780;&#35770;&#65306;<\/p>\n<pre><code class=\"language-php\">\/\/ app\/Http\/Controllers\/CommentsController.php\n\nuse AppModelsComment;\nuse AppModelsNestedComment;\nuse IlluminateHttpRequest;\n\npublic function nestedComments(Request $request)\n{\n    $comments = Comment::with(['comments' =&gt; function ($query) {\n        $query-&gt;whereNull('deleted_at')-&gt;orderByDesc('created_at');\n    }])-&gt;get();\n\n    return response()-&gt;json($comments);\n}<\/code><\/pre>\n<p>&#22312;&#36825;&#20010;&#20363;&#23376;&#20013;&#65292;&#25105;&#20204;&#20351;&#29992;&#20102; Laravel &#30340; Eloquent ORM &#26469;&#33719;&#21462;&#23884;&#22871;&#35780;&#35770;&#12290;<code>with<\/code> &#20851;&#38190;&#23383;&#20801;&#35768;&#25105;&#20204;&#22312;&#26597;&#35810;&#26102;&#25351;&#23450;&#29305;&#23450;&#23646;&#24615;&#12290;&#22312;&#36825;&#37324;&#65292;&#25105;&#20204;&#21482;&#36820;&#22238; <code>comments<\/code> &#23646;&#24615;&#65292;&#20294;&#19981;&#21253;&#25324;&#23884;&#22871;&#35780;&#35770;&#12290;&#36825;&#26679;&#65292;&#25105;&#20204;&#23601;&#21487;&#20197;&#36873;&#25321;&#24615;&#22320;&#26174;&#31034;&#23884;&#22871;&#35780;&#35770;&#12290;<\/p>\n<h3>&#24635;&#32467;<\/h3>\n<p>&#20197;&#19978;&#23601;&#26159;&#22312; Laravel &#20013;&#20351;&#29992; Eloquent ORM &#23384;&#20648;&#35780;&#35770;&#19982;&#23884;&#22871;&#35780;&#35770;&#30340;&#22522;&#26412;&#26041;&#27861;&#12290;&#36890;&#36807;&#36825;&#31181;&#26041;&#24335;&#65292;&#20320;&#21487;&#20197;&#36731;&#26494;&#22320;&#31649;&#29702;&#22797;&#26434;&#30340;&#25968;&#25454;&#24211;&#32467;&#26500;&#65292;&#24182;&#19988;&#21487;&#20197;&#36890;&#36807;&#32534;&#20889;&#21333;&#19968;&#30340;&#27169;&#22411;&#26469;&#22788;&#29702;&#25152;&#26377;&#31867;&#22411;&#30340;&#35780;&#35770;&#21644;&#23884;&#22871;&#35780;&#35770;&#12290;<\/p>\r\n<\/div>\r\n<\/div>\r\n<\/div>\r\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>Bug&#32534;&#35793;&#29422; &#26631;&#39064;&#65306;Laravel + &#20248;&#38597; ORM&#65306;&#23384;&#20648;&#35780;&#35770;&#19982;&#23884;&#22871;&#35780;&#038;#3577..<\/p>\n","protected":false},"author":1,"featured_media":0,"menu_order":0,"template":"","meta":{"_acf_changed":false},"tags":[],"my1js2nav":[44],"tuisongtax":[],"class_list":["post-776","my1js","type-my1js","status-publish","hentry","my1js2nav-laravel"],"acf":{"qian_art_seotitle":"","qian_art_seotitle_source":{"label":"SEO\u6807\u9898","type":"text","formatted_value":""},"qian_art_seokws":"","qian_art_seokws_source":{"label":"SEO\u5173\u952e\u8bcd","type":"text","formatted_value":""},"qian_art_stzhong":"","qian_art_stzhong_source":{"label":"\u4e2d | \u77ed\u6807\u9898","type":"text","formatted_value":""}},"_links":{"self":[{"href":"https:\/\/www.zhaozhao123.cn\/php\/wp-json\/wp\/v2\/my1js\/776","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.zhaozhao123.cn\/php\/wp-json\/wp\/v2\/my1js"}],"about":[{"href":"https:\/\/www.zhaozhao123.cn\/php\/wp-json\/wp\/v2\/types\/my1js"}],"author":[{"embeddable":true,"href":"https:\/\/www.zhaozhao123.cn\/php\/wp-json\/wp\/v2\/users\/1"}],"wp:attachment":[{"href":"https:\/\/www.zhaozhao123.cn\/php\/wp-json\/wp\/v2\/media?parent=776"}],"wp:term":[{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.zhaozhao123.cn\/php\/wp-json\/wp\/v2\/tags?post=776"},{"taxonomy":"my1js2nav","embeddable":true,"href":"https:\/\/www.zhaozhao123.cn\/php\/wp-json\/wp\/v2\/my1js2nav?post=776"},{"taxonomy":"tuisongtax","embeddable":true,"href":"https:\/\/www.zhaozhao123.cn\/php\/wp-json\/wp\/v2\/tuisongtax?post=776"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}