laravel ¸üкó±»Èíɾ³ý
laravelÊÇÒ»¿îÊ¢ÐеÄphp¿ª·¢¿ò¼Ü£¬ÆÕ±éÓ¦ÓÃÓÚwebÓ¦ÓóÌÐòµÄ¿ª·¢ÖС£ËüÌṩÁËÐí¶àÀû±ãµÄ¹¦Ð§£¬ÀýÈçorm¡¢Â·ÓÉ¿ØÖÆ¡¢Ä£°åÒýÇæµÈ£¬Ê¹µÃwebÓ¦ÓóÌÐòµÄ¿ª·¢±äµÃÇáËÉÓä¿ì¡£ÆäÖУ¬laravelµÄÈíɾ³ý¹¦Ð§ÊǹãÊܽӴýµÄ¹¦Ð§Ö®Ò»¡£±¾ÎĽ«ÏÈÈÝlaravel¸üкóÈíɾ³ý±¬·¢µÄת±ä¼°Æä½â¾öÒªÁì¡£
LaravelµÄÈíɾ³ý¹¦Ð§
ÔÚLaravelÖУ¬ÈôÊÇÎÒÃÇÏëҪɾ³ýÒ»¸ö¼Í¼£¬¿ÉÒÔʹÓÃdeleteÒªÁ죬Ҳ¿ÉÒÔʹÓÃdestroyÒªÁì¡£¿ÉÊÇ£¬ÈôÊÇÎÒÃÇÏ£Íû¸Ã¼Í¼±»±ê¼ÇΪÒÑɾ³ý¶ø²»ÊÇÍêȫɾ³ý£¬ÎÒÃÇÓ¦¸ÃʹÓÃLaravelµÄÈíɾ³ý¹¦Ð§¡£
ҪʹÓÃÈíɾ³ý¹¦Ð§£¬ÎÒÃÇÐèÒªÏòÄ£×ÓÌí¼ÓSoftDeletestrait£¬²¢ÔÚÊý¾Ý¿âÖÐÌí¼ÓÒ»¸öÃûΪdeleted_atµÄdatetimeÀàÐ͵ÄÁС£µ±Ê¹ÓÃdelete»òdestroyÒªÁì´ÓÊý¾Ý¿âÖÐɾ³ýÒ»¸ö¼Í¼ʱ£¬¸Ã¼Í¼½«±»±ê¼ÇΪÒÑɾ³ý£¬²¢½«deleted_at×Ö¶ÎÉèÖÃΪĿ½ñʱ¼ä´Á¡£±ê¼ÇΪÒÑɾ³ýµÄ¼Í¼²»»áÔÚÅÌÎÊÖзµ»Ø£¬µ«ËüÃÇÈÔÈ»±£´æÓÚÊý¾Ý¿âÖС£
³ýÁËdeleteºÍdestroyÒªÁìÖ®Í⣬Laravel»¹ÌṩÁËÐí¶àÆäËûµÄÈíɾ³ýÒªÁ죬ÀýÈçforceDeleteºÍrestore¡£forceDeleteÒªÁì¿ÉÒÔÓÀÊÀɾ³ýÈíɾ³ýµÄ¼Í¼£¬¶ørestoreÒªÁì¿ÉÒÔ½«Èíɾ³ýµÄ¼Í¼»Ö¸´µ½Êý¾Ý¿âÖС£
Laravel¸üÐÂÈíɾ³ýºóµÄת±ä
µ±Laravel 7¸üе½Laravel 8ʱ£¬Èíɾ³ý¹¦Ð§±¬·¢ÁËһЩת±ä¡£ÔÚLaravel 8ÖУ¬Èíɾ³ýÐÐΪµÄĬÈÏÖµ´Ó¡°±£´æ¡±¸ü¸ÄΪ¡°É¾³ý¡±¡£
»»¾ä»°Ëµ£¬ÔÚLaravel 8ÖУ¬µ±ÄúŲÓÃdelete»òdestroyÒªÁìʱ£¬¼Í¼½«±»ÓÀÊÀɾ³ý£¬¶ø²»ÊÇÏñÒÔÇ°°æ±¾ÖÐÄÇÑù±»±ê¼ÇΪÒÑɾ³ý¡£Õ⽫µ¼ÖÂÄúÎÞ·¨»Ö¸´ÒÑɾ³ýµÄ¼Í¼£¬²¢ÇÒÈôÊÇÄúµÄÓ¦ÓóÌÐòÒÀÀµÓÚÈíɾ³ý¹¦Ð§£¬Ôò¿ÉÄ᷺ܻÆðÒâÍâµÄ¹ýʧ¡£
½â¾öÒªÁì
ÈôÊÇÄúµÄÓ¦ÓóÌÐòÒÀÀµÓÚÈíɾ³ý¹¦Ð§£¬ÄÇôÄú±ØÐèÔÚLaravel 8ÖнÓÄÉһЩÌØÁíÍâ°ì·¨À´È·±£Èíɾ³ý¹¦Ð§°´Ô¤ÆÚÊÂÇé¡£
Ê×ÏÈ£¬ÄúÐèÒªÔÚÄ£×ÓÖÐÏÔʽ½ç˵Èíɾ³ýÐÐΪ¡£ÔÚÄúµÄÄ£×ÓÖУ¬ÄúÓ¦¸Ã½ç˵bootedÒªÁ죬²¢Å²ÓÃstatic::deletedÒªÁìÀ´½ç˵ģ×ÓµÄÈíɾ³ýÐÐΪ£¬ÈçÏÂËùʾ£º
use IlluminateDatabaseEloquentSoftDeletes; class MyModel extends Model { use SoftDeletes; protected static function booted() { static::deleted(function ($model) { // Handle soft deletion }); } }
µÇ¼ºó¸´ÖÆ
ÔÚÉÏÃæµÄÀý×ÓÖУ¬ÎÒÃÇʹÓÃdeletedÒªÁìÀ´½ç˵ģ×ÓµÄÈíɾ³ýÐÐΪ¡£µ±Ä£×Ó±»Èíɾ³ýʱ£¬½«Å²ÓÃת´ï¸ødeletedÒªÁìµÄ±Õ°üº¯Êý¡£
ÔÚÕâ¸öº¯ÊýÖУ¬Äú¿ÉÒÔ±àдÈκÎÄúÏ£ÍûÔÚÈíɾ³ýʱִÐеĴúÂë¡£ÀýÈ磬ÔÚ½¨ÉèÈíɾ³ý¼Í¼ʱ£¬Äú¿ÉÒÔÔÚÄ£×ÓÖÐÉèÖÃÆäËûÊôÐÔ£¬»òÕßÔÚµÚÈý·½Ð§ÀÍÖÐ֪ͨÆäËûÓ¦ÓóÌÐò¡£
Æä´Î£¬ÄúÐèÒªÔÚÄúµÄÓ¦ÓóÌÐòÖÐÈ·±£ËùÓÐʹÓÃÈíɾ³ýµÄµØ·½¶¼ÒÑ׼ȷµ÷½â¡£ÀýÈ磬ÈôÊÇÄúÔÚ´úÂëÖÐʹÓÃÁËdeleted_at×ֶξÙÐÐÅÌÎÊ£¬ÇëÈ·±£ÄúµÄ´úÂë׼ȷ´¦Àí¸Ã×ֶεÄÖµ¡£
×îºó£¬ÈôÊÇÄú֮ǰʹÓõÄÊÇdelete»òdestroyÒªÁ죬²¢ÇÒÏ£Íû¼ÌÐøʹÓÃÈíɾ³ý¹¦Ð§£¬ÇëÐÞ¸ÄÄúµÄ´úÂ룬ÒÔ±ãʹÓÃsoftDelete»òsoftDeletesÒªÁ죬²¢ÒýÓÃSoftDeletestrait£¬ÈçÏÂËùʾ£º
class MyController extends Controller { public function delete($id) { $record = MyModel::find($id); $record->softDelete(); // ʹÓà softDelete ÒªÁì // ... } }
µÇ¼ºó¸´ÖÆ
ÔÚÉÏÃæµÄÀý×ÓÖУ¬ÎÒÃÇʹÓÃÁËsoftDeleteÒªÁìÀ´Èíɾ³ý¼Í¼¡£ÈôÊÇÄúҪɾ³ý¶àÌõ¼Í¼£¬ÇëʹÓÃsoftDeletesÒªÁì¡£
½áÂÛ
Èíɾ³ýÊÇLaravelÖеÄÒ»¸öºÜÊÇÀû±ãµÄ¹¦Ð§£¬µ«ÔÚLaravel 8ÖУ¬ÆäÐÐΪ±¬·¢ÁËת±ä¡£ÎªÁËÈ·±£Èíɾ³ý¹¦Ð§°´Ô¤ÆÚÊÂÇ飬ÄúÐèÒªÔÚÄ£×ÓÖÐÏÔʽ½ç˵Èíɾ³ýÐÐΪ£¬²¢ÔÚÄúµÄÓ¦ÓóÌÐòÖжÔʹÓÃÈíɾ³ýµÄµØ·½¾ÙÐÐÊʵ±µÄµ÷½â¡£
ÔÚ¸üÐÂLaravel°æ±¾Ö®Ç°£¬ÇëÈ·±£ÄúÒѾ×öºÃ³ä·ÖµÄ×¼±¸£¬²¢ÔÚ¸üкó¼ì²éÈíɾ³ý¹¦Ð§ÊÇ·ñÈÔÈ»°´Ô¤ÆÚÊÂÇé¡£
ÒÔÉϾÍÊÇlaravel ¸üкó±»Èíɾ³ýµÄÏêϸÄÚÈÝ£¬¸ü¶àÇë¹Ø×¢±¾ÍøÄÚÆäËüÏà¹ØÎÄÕ£¡