{"id":972,"date":"2025-06-12T03:55:03","date_gmt":"2025-06-11T19:55:03","guid":{"rendered":"https:\/\/www.zhaozhao123.cn\/php\/php-application-manual\/laravel\/972.html"},"modified":"2025-06-12T03:55:03","modified_gmt":"2025-06-11T19:55:03","slug":"%e5%a6%82%e4%bd%95%e5%9c%a8eloquent%e4%b8%ad%e6%81%a2%e5%a4%8d%e8%bd%af%e5%88%a0%e9%99%a4%e7%9a%84%e6%a8%a1%e5%9e%8b","status":"publish","type":"my1js","link":"https:\/\/www.zhaozhao123.cn\/php\/my1js\/972.html","title":{"rendered":"\u5982\u4f55\u5728Eloquent\u4e2d\u6062\u590d\u8f6f\u5220\u9664\u7684\u6a21\u578b"},"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>&#22312;&#20351;&#29992; Laravel &#30340; Eloquent &#26694;&#26550;&#36827;&#34892;&#25968;&#25454;&#24211;&#25805;&#20316;&#26102;&#65292;&#26377;&#26102;&#20250;&#36935;&#21040;&#38656;&#35201;&#24674;&#22797;&#36719;&#21024;&#38500;&#65288;soft delete&#65289;&#30340;&#27169;&#22411;&#30340;&#24773;&#20917;&#12290;&#36719;&#21024;&#38500;&#26159;&#19968;&#31181;&#24120;&#35265;&#30340;&#25968;&#25454;&#20445;&#25252;&#26041;&#27861;&#65292;&#23427;&#20801;&#35768;&#20320;&#20174;&#25968;&#25454;&#24211;&#20013;&#31227;&#38500;&#35760;&#24405;&#32780;&#20445;&#30041;&#23427;&#20204;&#30340;&#21382;&#21490;&#35760;&#24405;&#12290;&#35201;&#24674;&#22797;&#36825;&#20123;&#36719;&#21024;&#38500;&#30340;&#35760;&#24405;&#65292;&#21487;&#20197;&#25353;&#29031;&#20197;&#19979;&#27493;&#39588;&#36827;&#34892;&#25805;&#20316;&#65306;<\/p>\n<h3>1. &#33719;&#21462;&#36719;&#21024;&#38500;&#30340;&#35760;&#24405;<\/h3>\n<p>&#39318;&#20808;&#65292;&#20320;&#38656;&#35201;&#33719;&#21462;&#25152;&#26377;&#34987;&#26631;&#35760;&#20026;&#36719;&#21024;&#38500;&#30340;&#35760;&#24405;&#12290;&#20320;&#21487;&#20197;&#20351;&#29992; <code>withTrashed()<\/code> &#26041;&#27861;&#26469;&#23454;&#29616;&#36825;&#19968;&#28857;&#12290;<\/p>\n<pre><code class=\"language-php\">$trashedRecords = YourModel::withTrashed()-&gt;get();<\/code><\/pre>\n<h3>2. &#24674;&#22797;&#35760;&#24405;<\/h3>\n<p>&#19968;&#26086;&#20320;&#26377;&#20102;&#36719;&#21024;&#38500;&#30340;&#35760;&#24405;&#65292;&#20320;&#21487;&#20197;&#36890;&#36807;&#35843;&#29992; <code>restore()<\/code> &#26041;&#27861;&#26469;&#24674;&#22797;&#23427;&#20204;&#12290;<\/p>\n<pre><code class=\"language-php\">foreach ($trashedRecords as $record) {\n    $record-&gt;restore();\n}<\/code><\/pre>\n<h3>&#31034;&#20363;&#20195;&#30721;<\/h3>\n<p>&#20551;&#35774;&#25105;&#20204;&#26377;&#19968;&#20010;&#21517;&#20026; <code>User<\/code> &#30340;&#27169;&#22411;&#65292;&#24182;&#19988;&#25105;&#20204;&#24076;&#26395;&#24674;&#22797;&#25152;&#26377;&#34987;&#26631;&#35760;&#20026;&#36719;&#21024;&#38500;&#30340;&#29992;&#25143;&#12290;<\/p>\n<pre><code class=\"language-php\">\/\/ &#33719;&#21462;&#25152;&#26377;&#34987;&#26631;&#35760;&#20026;&#36719;&#21024;&#38500;&#30340;&#29992;&#25143;\n$trashedUsers = User::withTrashed()-&gt;get();\n\n\/\/ &#24674;&#22797;&#25152;&#26377;&#34987;&#26631;&#35760;&#20026;&#36719;&#21024;&#38500;&#30340;&#29992;&#25143;\nforeach ($trashedUsers as $user) {\n    $user-&gt;restore();\n}\n\necho \"All soft-deleted users have been restored.\";<\/code><\/pre>\n<h3>&#27880;&#24847;&#20107;&#39033;<\/h3>\n<ul>\n<li><strong>&#22791;&#20221;&#25968;&#25454;<\/strong>&#65306;&#22312;&#25191;&#34892;&#20219;&#20309;&#25968;&#25454;&#24674;&#22797;&#25805;&#20316;&#20043;&#21069;&#65292;&#35831;&#30830;&#20445;&#24050;&#32463;&#22791;&#20221;&#20102;&#37325;&#35201;&#25968;&#25454;&#12290;<\/li>\n<li><strong>&#26435;&#38480;<\/strong>&#65306;&#30830;&#20445;&#20320;&#26377;&#36275;&#22815;&#30340;&#26435;&#38480;&#26469;&#25191;&#34892;&#36719;&#21024;&#38500;&#21644;&#24674;&#22797;&#25805;&#20316;&#12290;<\/li>\n<li><strong>&#39564;&#35777;&#29366;&#24577;<\/strong>&#65306;&#22312;&#23454;&#38469;&#24212;&#29992;&#20013;&#65292;&#24314;&#35758;&#28155;&#21152;&#19968;&#20123;&#39564;&#35777;&#36923;&#36753;&#20197;&#30830;&#20445;&#21482;&#26377;&#39044;&#26399;&#30340;&#29366;&#24577;&#25165;&#33021;&#36827;&#34892;&#24674;&#22797;&#25805;&#20316;&#12290;<\/li>\n<\/ul>\n<p>&#36890;&#36807;&#20197;&#19978;&#27493;&#39588;&#65292;&#20320;&#24212;&#35813;&#33021;&#22815;&#25104;&#21151;&#22320;&#22312; Eloquent &#20013;&#24674;&#22797;&#36719;&#21024;&#38500;&#30340;&#27169;&#22411;&#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>&#20248;&#38597;&#30340;ORM&#65288;&#23545;&#35937;&#20851;&#31995;&#26144;&#23556;&#22120;&#65289;&#65292;&#21363;&#19982;Laravel&#19968;&#36215;&#25552;&#20379;&#30340;ORM&#65292;&#25552;&#20379;&#20102;&#36731;&#26494;&#22788;&#29702;&#25968;&#25454;&#24211;&#34920;&#20132;&#20114;&#30340;&#21151;&#33021;&#12290;&#19968;&#20010;&#24378;&#22823;&#30340;&#29305;&#24615;&#26159;&ldquo;&#36719;&#21024;&#38500;&rdquo;&#65292;&#20801;&#35768;&#24320;&#21457;&#20154;&#21592;&#22312;&#24212;&#29992;&#23618;&#38754;&#21024;&#38500;&#35760;&#24405;&#65292;&#32780;&#36825;&#20123;&#35760;&#24405;&#20173;&#28982;&#20445;&#30041;&#22312;&#25968;&#25454;&#24211;&#20013;&#12290;&#24403;&#20320;&#38656;&#35201;&#24674;&#22797;&#24050;&#21024;&#38500;&#30340;&#27169;&#22411;&#26102;&#65292;&#25105;&#20204;&#23558;&#25506;&#32034;&#22914;&#20309;&#23454;&#29616;&#36825;&#19968;&#28857;&#12290;<\/p><h3>&#36719;&#21024;&#38500;<\/h3><p>&#22312;&#28145;&#20837;&#25506;&#35752;&#24674;&#22797;&#36719;&#21024;&#38500;&#27169;&#22411;&#20043;&#21069;&#65292;&#35753;&#25105;&#20204;&#20808;&#20102;&#35299;&#19968;&#19979;Eloquent&#20013;&#30340;&#36719;&#21024;&#38500;&#26159;&#20160;&#20040;&#24847;&#24605;&#12290;&#36719;&#21024;&#38500;&#26159;&#19968;&#31181;&#20445;&#30041;&#35760;&#24405;&#22312;&#25968;&#25454;&#24211;&#20013;&#30340;&#26041;&#24335;&#65292;&#21482;&#26159;&#26631;&#35760;&#23427;&#20204;&#20026;&ldquo;&#24050;&#21024;&#38500;&rdquo;&#12290;&#36825;&#36890;&#36807;&#22312;&#24744;&#34920;&#30340;&#25351;&#23450;&ldquo;deleted_at&rdquo;&#21015;&#19978;&#20998;&#37197;&#19968;&#20010;&#26102;&#38388;&#25139;&#26469;&#23454;&#29616;&#12290;&#24403;Eloquent&#26597;&#35810;&#27169;&#22411;&#26102;&#65292;&#20219;&#20309;&#20855;&#26377;&#38750;&#31354;&ldquo;deleted_at&rdquo;&#23383;&#27573;&#30340;&#27169;&#22411;&#37117;&#20250;&#33258;&#21160;&#20174;&#26597;&#35810;&#32467;&#26524;&#20013;&#25490;&#38500;&#12290;<\/p><h2>&#35774;&#32622;&#36719;&#21024;&#38500;<\/h2><p>&#20026;&#20102;&#22312;Eloquent&#27169;&#22411;&#20013;&#20351;&#29992;&#36719;&#21024;&#38500;&#21151;&#33021;&#65292;&#24744;&#39318;&#20808;&#24517;&#39035;&#30830;&#20445;&#24744;&#30340;&#34920;&#30340;&#36801;&#31227;&#25991;&#20214;&#20013;&#21253;&#21547;&#36719;&#21024;&#38500;&#21015;&#65288;&#20363;&#22914;&#65306;&#65289;&#22914;&#19978;&#25152;&#31034;&#12290;<\/p><pre><code>&lt;?php \ntable-&gt;softDeletes(); \n?&gt;<\/code><\/pre><p>&#25509;&#19979;&#26469;&#65292;&#22312;&#27169;&#22411;&#20013;&#21551;&#29992;&#36719;&#21024;&#38500;&#21151;&#33021;&#65292;&#20320;&#21487;&#20197;&#20351;&#29992; &#8216;IlluminateDatabaseEloquentSoftDeletes&#8217; &#29305;&#24615;&#65306;<\/p><pre><code>namespace AppModels;\n\nuse IlluminateDatabaseEloquentModel;\nuse IlluminateDatabaseEloquentSoftDeletes;\n\nclass Post extends Model\n{\n    use SoftDeletes;\n}\n<\/code><\/pre><p>&#22312;&#24744;&#30340;&#26700;&#23376;&#20934;&#22791;&#22909;&#20102;&#65292;&#27169;&#22411;&#20063;&#35774;&#32622;&#22909;&#20102;&#20043;&#21518;&#65292;Eloquent &#23558;&#20250;&#33258;&#21160;&#22788;&#29702;&#36719;&#21024;&#38500;&#12290;&#24403;&#20320;&#35843;&#29992;&#26102;&#65292;Eloquent &#20250;&#33258;&#21160;&#22788;&#29702;&#36719;&#21024;&#38500;&#12290;<code>delete()<\/code>&#22312;&#27169;&#22411;&#26041;&#27861;&#19978;&#65292;Eloquent&#20250;&#23558;&ldquo;deleted_at&rdquo;&#26102;&#38388;&#25139;&#35774;&#32622;&#20026;&#30495;&#27491;&#21024;&#38500;&#35760;&#24405;&#30340;&#26367;&#20195;&#26041;&#26696;&#12290;<\/p><h2>&#24674;&#22797;&#24050;&#36719;&#21024;&#38500;&#30340;&#27169;&#22411;<\/h2><p>&#20026;&#20102;&#24674;&#22797;&#36719;&#21024;&#38500;&#35760;&#24405;&#65292;Eloquent &#25552;&#20379;&#20102;&#30456;&#24212;&#30340;&#21151;&#33021;&#12290;<code>restore()<\/code>&#26041;&#27861;&#12290;&#35201;&#23545;&#27169;&#22411;&#23454;&#20363;&#35843;&#29992;&#27492;&#26041;&#27861;&#65292;&#39318;&#20808;&#38656;&#35201;&#26816;&#32034;&#36719;&#21024;&#38500;&#30340;&#27169;&#22411;&#12290;&#20197;&#19979;&#26159;&#20855;&#20307;&#27493;&#39588;&#65306;<\/p><pre><code>use AppModelsPost;\n\n$trashed = Post::onlyTrashed()-&gt;get();\n\nforeach ($trashed as $post) {\n    $post-&gt;restore();\n}\n<\/code><\/pre><p>&#22312;&#19978;&#36848;&#20195;&#30721;&#31034;&#20363;&#20013;&#65292;<code>onlyTrashed()<\/code>&#36825;&#26159;&#19968;&#31181;&#26041;&#27861;&#65292;&#36890;&#36807;&#20462;&#25913;&#26597;&#35810;&#26469;&#33719;&#21462;&#21482;&#21253;&#21547;&#34987;&#36719;&#21024;&#38500;&#30340;&#27169;&#22411;&#30340;&#25968;&#25454;&#12290;&#28982;&#21518;&#20351;&#29992;foreach&#24490;&#29615;&#36941;&#21382;&#38598;&#21512;&#24182;&#24674;&#22797;&#27599;&#20010;&#23545;&#35937;&#12290;<\/p><p>&#36890;&#24120;&#65292;&#20320;&#38656;&#35201;&#23545;&#24050;&#30693;ID&#25110;&#19968;&#32452;ID&#25191;&#34892;&#36824;&#21407;&#25805;&#20316;&#65292;&#20320;&#21487;&#20197;&#20351;&#29992;&#65306;<\/p><pre><code>\/\/ Restore a single post with id 1\nPost::withTrashed()-&gt;where('id', 1)-&gt;restore();\n\n\/\/ Restore multiple posts with ids 1, 2, and 3\nPost::withTrashed()-&gt;whereIn('id', [1, 2, 3])-&gt;restore();\n<\/code><\/pre><p>&#23545;&#19981;&#36215;&#65292;&#24744;&#30340;&#38382;&#39064;&#20284;&#20046;&#27809;&#26377;&#23436;&#25104;&#12290;&#35831;&#25552;&#20379;&#23436;&#25972;&#30340;&#38382;&#39064;&#25110;&#21477;&#23376;&#20197;&#20415;&#25105;&#33021;&#22815;&#20026;&#24744;&#25552;&#20379;&#20934;&#30830;&#30340;&#32763;&#35793;&#12290;<code>withTrashed()<\/code>&#26041;&#27861;&#36820;&#22238;&#20102;&#36719;&#21024;&#38500;&#21644;&#26410;&#21024;&#38500;&#30340;&#27169;&#22411;&#65292;&#20801;&#35768;&#24744;&#29420;&#31435;&#20110;&#23427;&#20204;&#26159;&#21542;&#34987;&#21024;&#38500;&#25191;&#34892;&#25805;&#20316;&#12290;<\/p><h2>&#24674;&#22797;&#19982;&#30456;&#20851;&#36719;&#21024;&#38500;&#27169;&#22411;<\/h2><p>&#26377;&#22330;&#26223;&#19979;&#65292;&#20320;&#21487;&#33021;&#20250;&#36935;&#21040;&#30456;&#20851;&#27169;&#22411;&#20063;&#20351;&#29992;&#36719;&#21024;&#38500;&#30340;&#24773;&#20917;&#12290;&#22914;&#26524;&#20320;&#24819;&#35201;&#24674;&#22797;&#29238;&#27169;&#22411;&#21450;&#20854;&#25152;&#26377;&#30456;&#20851;&#30340;&#23376;&#27169;&#22411;&#65292;&#38656;&#35201;&#20570;&#20123;&#39069;&#22806;&#30340;&#24037;&#20316;&#12290;&#35753;&#25105;&#20204;&#26469;&#30475;&#19968;&#20010;&#20363;&#23376;&#65292;&#28041;&#21450;&#19968;&#23545;&#19968;&#20851;&#31995;&#65306;<\/p><pre><code>use AppModelsUser;\n\n$user = User::withTrashed()-&gt;findOrFail($userId);\n$user-&gt;restore();\n\nforeach ($user-&gt;posts as $post) {\n    $post-&gt;restore();\n}\n<\/code><\/pre><p>&#35831;&#27880;&#24847;&#65292;&#20197;&#19979;&#30340;&#20449;&#24687;&#12290;<code>posts()<\/code>&#36890;&#24120;&#24773;&#20917;&#19979;&#65292;&#26041;&#27861;&#21482;&#20250;&#36820;&#22238;&#26410;&#21024;&#38500;&#30340;&#24086;&#23376;&#12290;&#35201;&#21253;&#25324;&#36719;&#21024;&#38500;&#30340;&#24086;&#23376;&#22312;&#24490;&#29615;&#20013;&#65292;&#35831;&#21153;&#24517;&#26174;&#24335;&#22320;&#35831;&#27714;&#23427;&#20204;&#65306;foreach($user-&gt;posts()-&gt;withTrashed()-&gt;get() as $post) { $post-&gt;restore(); }<\/p><h2>&#20107;&#20214;&#21644;&#35266;&#23519;&#32773;<\/h2><p>&#21644;&#20854;&#20182;&#30340;eloquent&#25805;&#20316;&#19968;&#26679;&#65292;&#24674;&#22797;&#36719;&#21024;&#38500;&#30340;&#27169;&#22411;&#20063;&#20250;&#35302;&#21457;&#27169;&#22411;&#20107;&#20214;&#12290;<code>restoring<\/code>and &#26159;&#20013;&#25991;&#20013;&#30340;&ldquo;&#21644;&rdquo;&#65292;&#29992;&#20110;&#36830;&#25509;&#24182;&#21015;&#30340;&#35789;&#35821;&#12289;&#30701;&#35821;&#25110;&#21477;&#23376;&#65292;&#34920;&#31034;&#24182;&#34892;&#20851;&#31995;&#12290;&#22312;&#20013;&#25991;&#37324;&#65292;&ldquo;and&rdquo;&#36890;&#24120;&#29992;&#26469;&#34920;&#31034;&#24182;&#21015;&#36830;&#35789;&#12290;&#20363;&#22914;&#65306;I like apples and oranges.&#65288;&#25105;&#21916;&#27426;&#33529;&#26524;&#21644;&#27225;&#23376;&#12290;&#65289;<code>restored<\/code>&#20107;&#20214;&#20801;&#35768;&#24744;&#22312;&#21457;&#29983;&#20043;&#21069;&#21644;&#20043;&#21518;&#38057;&#20837;&#25805;&#20316;&#65306;<\/p><pre><code>Post::restoring(function ($post) {\n    \/\/ Called before a soft-deleted model is restored\n});\n\nPost::restored(function ($post) {\n    \/\/ Called after a soft-deleted model is restored\n});\n<\/code><\/pre><p>&#22914;&#26524;&#20320;&#20351;&#29992;&#27169;&#22411;&#35266;&#23519;&#32773;&#65292;&#21516;&#26679;&#30340;&#20107;&#20214;&#21487;&#20197;&#22312;&#35266;&#23519;&#32773;&#31867;&#20013;&#22788;&#29702;&#65306;<\/p><pre><code>class PostObserver\n{\n    public function restoring(Post $post)\n    {\n        \/\/ Logic before restoring\n    }\n\n    public function restored(Post $post)\n    {\n        \/\/ Logic after restoring\n    }\n}\n\n\/\/ Register the observer in an appropriate service provider:\nPost::observe(PostObserver::class);\n<\/code><\/pre><p>&#20351;&#29992;&#35266;&#23519;&#32773;&#27169;&#24335;&#23588;&#20854;&#22312;&#24674;&#22797;&#25805;&#20316;&#38656;&#35201;&#25191;&#34892;&#22823;&#37327;&#36923;&#36753;&#26102;&#29305;&#21035;&#26377;&#29992;&#12290;<\/p><h2>&#32467;&#35770;&#12290;<\/h2><p>&#22312;&#36825;&#20221;&#25351;&#21335;&#20013;&#65292;&#25105;&#20204;&#24050;&#32463;&#28085;&#30422;&#20102;&#22914;&#20309;&#24674;&#22797;Eloquent&#27169;&#22411;&#30340;&#36719;&#21024;&#38500;&#12290;&#26377;&#20102;&#36825;&#20123;&#30693;&#35782;&#65292;&#24744;&#21487;&#20197;&#36731;&#26494;&#22788;&#29702;&#38656;&#35201;&#25764;&#38144;&#21024;&#38500;&#25805;&#20316;&#25110;&#31649;&#29702;&#21487;&#22238;&#25910;&#20869;&#23481;&#30340;&#24773;&#20917;&#12290;Eloquent&#30340;&#20869;&#32622;&#36719;&#21024;&#38500;&#21151;&#33021;&#19982;&#24674;&#22797;&#26041;&#27861;&#30456;&#32467;&#21512;&#65292;&#20026;Laravel&#24212;&#29992;&#31243;&#24207;&#20013;&#30340;&#21024;&#38500;&#25805;&#20316;&#25552;&#20379;&#20102;&#24378;&#22823;&#30340;&#35299;&#20915;&#26041;&#26696;&#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>&#22312;Eloquent&#26694;&#26550;&#20013;&#65292;&#20320;&#21487;&#20197;&#36890;&#36807;&#20351;&#29992;<code>SoftDeletes<\/code> trait&#26469;&#23454;&#29616;&#36719;&#21024;&#38500;&#21151;&#33021;&#12290;&#36825;&#20010;trait&#20026;&#20320;&#30340;&#27169;&#22411;&#25552;&#20379;&#20102;&#19968;&#20010;&#26041;&#27861;<code>forceDelete()<\/code>&#21644;<code>restore()<\/code>&#26041;&#27861;&#12290;<\/p>\n<p>&#39318;&#20808;&#65292;&#20320;&#38656;&#35201;&#30830;&#20445;&#20320;&#24050;&#32463;&#23433;&#35013;&#20102;Eloquent&#26694;&#26550;&#65292;&#24182;&#19988;&#22312;&#20320;&#30340;&#39033;&#30446;&#20013;&#35774;&#32622;&#20102;&#27491;&#30830;&#30340;&#20381;&#36182;&#39033;&#12290;&#28982;&#21518;&#65292;&#20320;&#21487;&#20197;&#36825;&#26679;&#21019;&#24314;&#19968;&#20010;&#26032;&#30340;&#27169;&#22411;&#65306;<\/p>\n<pre><code class=\"language-php\">&lt;?php\n\nnamespace App;\n\nuse IlluminateDatabaseEloquentModel;\nuse SoftDeletes;\n\nclass User extends Model\n{\n    use SoftDeletes;\n\n    protected $table = 'users';\n\n    public function getDeletedAtAttribute($value)\n    {\n        return $this-&gt;attributes['deleted_at'] ? $value : null;\n    }\n}<\/code><\/pre>\n<p>&#22312;&#36825;&#20010;&#20363;&#23376;&#20013;&#65292;&#25105;&#20204;&#23450;&#20041;&#20102;&#19968;&#20010;&#21517;&#20026;<code>User<\/code>&#30340;&#26032;&#27169;&#22411;&#65292;&#23427;&#32487;&#25215;&#33258;<code>Model<\/code>&#31867;&#65292;&#24182;&#20351;&#29992;<code>SoftDeletes<\/code> trait&#12290;<code>softDeletes<\/code>&#23646;&#24615;&#34987;&#35774;&#32622;&#20026;<code>protected<\/code>&#65292;&#36825;&#24847;&#21619;&#30528;&#35813;&#23383;&#27573;&#21487;&#20197;&#34987;&#26597;&#35810;&#21644;&#20462;&#25913;&#65292;&#20294;&#26159;&#19981;&#33021;&#30452;&#25509;&#36827;&#34892;&#25554;&#20837;&#21644;&#26356;&#26032;&#25805;&#20316;&#12290;<code>getDeletedAtAttribute<\/code>&#26041;&#27861;&#29992;&#20110;&#36820;&#22238;<code>deleted_at<\/code>&#23383;&#27573;&#30340;&#20540;&#65292;&#22914;&#26524;<code>deleted_at<\/code>&#19981;&#23384;&#22312;&#65292;&#21017;&#36820;&#22238;null&#12290;<\/p>\n<p>&#29616;&#22312;&#65292;&#24403;&#20320;&#24819;&#35201;&#24674;&#22797;&#36719;&#21024;&#38500;&#30340;&#27169;&#22411;&#26102;&#65292;&#20320;&#21487;&#20197;&#20351;&#29992;&#20197;&#19979;&#30340;&#26041;&#27861;&#65306;<\/p>\n<pre><code class=\"language-php\">$user = User::find(1);\n\nif ($user) {\n    if ($user-&gt;isSoftDeleting()) {\n        $user-&gt;forceDelete();\n    } else {\n        $user-&gt;restore();\n    }\n\n    \/\/ &#20043;&#21518;&#65292;&#20320;&#21487;&#20197;&#20687;&#22788;&#29702;&#20219;&#20309;&#20854;&#20182;&#27169;&#22411;&#19968;&#26679;&#20351;&#29992;&#29992;&#25143;&#23454;&#20363;\n} else {\n    \/\/ &#27169;&#22411;&#26410;&#25214;&#21040;\n}<\/code><\/pre>\n<p>&#22312;&#36825;&#20010;&#20363;&#23376;&#20013;&#65292;<code>forceDelete()<\/code>&#26041;&#27861;&#20250;&#24378;&#21046;&#21024;&#38500;&#24403;&#21069;&#35760;&#24405;&#65292;&#32780;<code>restore()<\/code>&#26041;&#27861;&#21017;&#20250;&#23558;&#35813;&#35760;&#24405;&#20174;&#36719;&#21024;&#38500;&#29366;&#24577;&#24674;&#22797;&#21040;&#27491;&#24120;&#29366;&#24577;&#12290;<\/p>\n<p>&#27880;&#24847;&#65306;&#22312;&#24674;&#22797;&#20043;&#21069;&#65292;&#35831;&#30830;&#20445;&#20320;&#30340;&#25968;&#25454;&#24211;&#24050;&#20934;&#22791;&#22909;&#25509;&#21463;&#36825;&#20123;&#26356;&#25913;&#12290;&#22914;&#26524;&#20320;&#27491;&#22312;&#20351;&#29992;MySQL&#65292;&#20320;&#24212;&#35813;&#30830;&#20445;&#20320;&#26377;&#36275;&#22815;&#30340;&#26102;&#38388;&#35753;&#25968;&#25454;&#23436;&#25972;&#22320;&#20889;&#20837;&#21040;&#26032;&#30340;&#29256;&#26412;&#20013;&#12290;<\/p>\r\n<\/div>\r\n<\/div>\r\n<\/div>\r\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>Bug&#32534;&#35793;&#29422; &#22312;&#20351;&#29992; Laravel &#30340; Eloquent &#26694;&#26550;&#36827;&#34892;&#25968;&#25454;&#24211;&#25805;&#20316;&#26102;&#038;..<\/p>\n","protected":false},"author":1,"featured_media":0,"menu_order":0,"template":"","meta":{"_acf_changed":false},"tags":[],"my1js2nav":[44],"tuisongtax":[],"class_list":["post-972","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\/972","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=972"}],"wp:term":[{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.zhaozhao123.cn\/php\/wp-json\/wp\/v2\/tags?post=972"},{"taxonomy":"my1js2nav","embeddable":true,"href":"https:\/\/www.zhaozhao123.cn\/php\/wp-json\/wp\/v2\/my1js2nav?post=972"},{"taxonomy":"tuisongtax","embeddable":true,"href":"https:\/\/www.zhaozhao123.cn\/php\/wp-json\/wp\/v2\/tuisongtax?post=972"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}