laravelÈíɾ³ý×ֶθijÉÆäËû
laravelÊÇÒ»ÖÖÊ¢ÐеÄphp¿ò¼Ü£¬ËüÌṩÁËÐí¶àÇ¿Ê¢µÄ¹¦Ð§ºÍÒ×ÓÚʹÓõŤ¾ß£¬ÒÔ×ÊÖú¿ª·¢Ö°Ô±¿ª·¢¸ßЧ¡¢¿ÉÀ©Õ¹ºÍÒ×ÓÚά»¤µÄwebÓ¦ÓóÌÐò¡£laravelµÄÈíɾ³ý¹¦Ð§ÊÇÒ»ÖÖºÜÊÇÊÊÓõĹ¦Ð§£¬ËüÔÊÐí¿ª·¢Ö°Ô±ÇáËɵØɾ³ýÊý¾Ý¿âÖеÄÊý¾Ý£¬¶ø²»ÆÆËðÊý¾Ý¿âµÄÍêÕûÐÔ¡£µ«ÓÐЩʱ¼ä£¬¿ª·¢Ö°Ô±¿ÉÄÜÐèÒª½«laravelÈíɾ³ý×ֶθijÉÆäËûÃû³Æ£¬ÒÔÖª×ãËûÃǵÄÌض¨ÐèÇó¡£±¾ÎĽ«ÏÈÈÝÔõÑù½«laravelÈíɾ³ý×ֶθijÉÆäËûÃû³Æ¡£
LaravelÈíɾ³ý
ÔÚLaravelÖУ¬Èíɾ³ýÊÇͨ¹ý½«Êý¾Ý±ê¼ÇΪ¡°É¾³ý¡±¶ø²»ÊÇÏÖʵµØ´ÓÊý¾Ý¿âÖÐɾ³ýÊý¾ÝÀ´ÊµÏֵġ£Èíɾ³ý¹¦Ð§²»»áÆÆËðÊý¾Ý¿âµÄÍêÕûÐÔ£¬ÓÉÓÚÈíɾ³ýµÄÊý¾ÝÈÔÈ»±£´æÓÚÊý¾Ý¿âÖУ¬µ«Òѱ»±ê¼ÇΪ¡°É¾³ý¡±£¬²»»á·ºÆðÔÚÅÌÎÊЧ¹ûÖС£LaravelÈíɾ³ýÖ÷Òª°üÀ¨Á½¸ö°ì·¨£º
ÏòÄ£×ÓÌí¼ÓSoftDeletesÌØÕ÷
ҪʹÓÃLaravelµÄÈíɾ³ý¹¦Ð§£¬ÐèÒªÏòÒª¾ÙÐÐÈíɾ³ýµÄÄ£×ÓÌí¼ÓSoftDeletesÌØÕ÷¡£LaravelµÄÄ£×ÓʹÓÃtraitÀ´Ìí¼ÓÌØÁíÍ⹦Ч£¬SoftDeletesÌØÕ÷ÊÇÒ»¸öLaravelÌṩµÄtrait¡£
use IlluminateDatabaseEloquentModels; use IlluminateDatabaseEloquentSoftDeletes; class Example extends Model{ use SoftDeletes; protected $dates = ['deleted_at']; //... }
µÇ¼ºó¸´ÖÆ
ÔÚÉÏÃæµÄ´úÂëÖУ¬use SoftDeletesÓï¾äÌåÏÖÎÒÃÇҪʹÓÃLaravelÌṩµÄSoftDeletesÌØÕ÷¡£Í¬Ê±£¬»¹ÐèҪʹÓÃ$datesÊôÐÔÀ´½ç˵ÓÃÓÚ±êʶɾ³ýʱ¼äµÄÈíɾ³ýʱ¼ä´Á×Ö¶ÎÃû³Æ¡£Ä¬ÈϵÄÈíɾ³ýʱ¼ä´Á×Ö¶ÎÊÇ”deleted_at”£¬µ«Äú¿ÉÒÔʹÓÃÆäËûÃû³Æ¡£
¸æËßÄ£×ÓÖ´ÐÐÈíɾ³ý
LaravelµÄSoftDeletesÌØÕ÷ÌṩÁËÒ»¸ödeleteÒªÁ죬¸ÃÒªÁ콫ÔÚÖ´ÐÐdeleteʱ²»»áÏÖʵɾ³ýÊý¾Ý£¬¶øÊǻὫÊý¾Ý±ê¼ÇΪ”deleted”¡£ÀýÈ磺
$example = Example::find(1); $example->delete();
µÇ¼ºó¸´ÖÆ µÇ¼ºó¸´ÖÆ
ÉÏÃæµÄ´úÂ뽫°ÑIDΪ1µÄÊý¾Ý±ê¼ÇΪ”deleted”£¬µ«²»»á´ÓÊý¾Ý¿âÖÐɾ³ýËü¡£
ÒªÅÌÎÊÈíɾ³ýµÄÊý¾Ý£¬¿ÉÒÔʹÓÃwithTrashedÒªÁìÀ´¼ìË÷°üÀ¨Èíɾ³ýÊý¾ÝÔÚÄÚµÄËùÓÐÊý¾Ý£¬»òÕßʹÓÃonlyTrashedÒªÁìÀ´¼ìË÷½öÒÑɾ³ýµÄÊý¾Ý¡£ÀýÈ磺
// ÅÌÎÊ°üÀ¨Èíɾ³ýÊý¾ÝÔÚÄÚµÄËùÓÐÊý¾Ý $examples = Example::withTrashed()->get(); // ÅÌÎÊɾ³ýµÄÊý¾Ý $deletedExamples = Example::onlyTrashed()->get();
µÇ¼ºó¸´ÖÆ
ÒÔÉÏ´úÂë¿ÉÒÔ»®·ÖÓÃÀ´ÅÌÎÊ°üÀ¨Èíɾ³ýÊý¾ÝºÍÒÑɾ³ýÊý¾Ý¡£
½«LaravelÈíɾ³ý×ֶθijÉÆäËûÃû³Æ
ĬÈÏÇéÐÎÏ£¬LaravelʹÓÔdeleted_at”×Ö¶ÎÀ´±êʶɾ³ýʱ¼ä´Á×ֶΣ¬ÕâʹµÃÔÚÅÌÎÊÈíɾ³ýÊý¾ÝʱºÜÊÇÀû±ã¡£
¿ÉÊÇ£¬ÔÚijЩÇéÐÎÏ£¬¿ª·¢Ö°Ô±¿ÉÄÜÐèҪʹÓÃÆäËûÊôÐÔÀ´ÌåÏÖɾ³ýʱ¼ä´Á×ֶΡ£ÈôÊÇÐèÒª¸ü¸ÄLaravelÈíɾ³ý×ֶεÄÃû³Æ£¬¿ÉÒÔÔÚÄ£×ÓÖнç˵deletedAtÒªÁì¡£ÀýÈ磺
use IlluminateDatabaseEloquentModel; use IlluminateDatabaseEloquentSoftDeletes; class Example extends Model{ use SoftDeletes; protected $dates = ['my_deleted_at']; public function getDeletedAtColumn() { return 'my_deleted_at'; } // ... }
µÇ¼ºó¸´ÖÆ
ÔÚÉÏÃæµÄ´úÂëÖУ¬ÎÒÃǽ«$datesÊôÐÔÖеĔdeleted_at”¸ÄΪ”my_deleted_at”£¬²¢Ê¹ÓÃgetDeletedAtColumnÒªÁ콫LaravelÈíɾ³ý×ֶθü¸ÄΪ”my_deleted_at”¡£
È»ºó£¬¿ÉÒÔʹÓÃÒÔÏ·½·¨Ö´ÐÐÈíɾ³ý£º
$example = Example::find(1); $example->delete();
µÇ¼ºó¸´ÖÆ µÇ¼ºó¸´ÖÆ
Laravel½«Ê¹ÓÃmy_deleted_at×Ö¶ÎÈ¡´úĬÈϵÄdeleted_at×Ö¶ÎÀ´±ê¼ÇÊý¾ÝÒÑɾ³ý¡£ÔÚÅÌÎÊʱ£¬¿ÉÒÔʹÓÃwithTrashedºÍonlyTrashedÒªÁìÀ´¼ìË÷ÒÑɾ³ýµÄÊý¾Ý¡£
×ܽá
ÔÚLaravelÖУ¬Ê¹ÓÃÈíɾ³ý¿ÉÒÔÇáËɵؽ«Êý¾Ý±ê¼ÇΪ¡°É¾³ý¡±¶ø²»±ØÏÖʵɾ³ýÊý¾Ý¡£LaravelµÄSoftDeletesÌØÕ÷ÌṩÁËÒ»ÖÖ¼òÆÓµÄÒªÁìÀ´Ö´ÐÐÈíɾ³ý¡£Ä¬ÈÏÇéÐÎÏ£¬LaravelʹÓÔdeleted_at”×Ö¶ÎÀ´±êʶɾ³ýʱ¼ä´Á×ֶΣ¬¿ÉÊÇ£¬¿ª·¢Ö°Ô±¿ÉÒÔʹÓÃgetDeletedAtColumnÒªÁ콫×ֶθü¸ÄΪÆäËûÃû³Æ¡£ÎÞÂÛÄÄÖּƻ®£¬Èíɾ³ýÔÚLaravelÖж¼ÊÇÒ»ÖÖºÜÊÇÇ¿Ê¢ºÍÊÊÓõĹ¦Ð§¡£
ÒÔÉϾÍÊÇlaravelÈíɾ³ý×ֶθijÉÆäËûµÄÏêϸÄÚÈÝ£¬¸ü¶àÇë¹Ø×¢±¾ÍøÄÚÆäËüÏà¹ØÎÄÕ£¡