laravelÔõôÌí¼ÓÊý¾Ý×Ô¶¯É¾³ý¹¦Ð§
Ëæ×Å»¥ÁªÍøÓ¦ÓõÄÒ»Ö±Éú³¤£¬web¿ª·¢¿ò¼ÜÒ²²ã³ö²»Çî¡£ÆäÖÐlaravel×÷Ϊ×îÊ¢ÐеÄphp¿ª·¢¿ò¼ÜÖ®Ò»£¬ÔÚ¸÷Ðи÷Òµ¶¼ÓÐÆÕ±éÓ¦Óᣱ¾ÆªÎÄÕ½«ÏÈÈÝÔÚlaravelÖÐÌí¼ÓÊý¾Ý×Ô¶¯É¾³ý¹¦Ð§µÄʵÏÖÒªÁì¡£
Ò»¡¢ÎÊÌâÅä¾°
ÔÚ¿ª·¢WebÓ¦ÓÃʱ£¬ÎÒÃÇ¿ÉÄÜÐèÒª°´ÆÚɾ³ýһЩÎÞÓõÄÊý¾Ý£¬ÒÔÊÍ·Å¿Õ¼äºÍÌáÉýϵͳЧÂÊ¡£ÀýÈ磬ÎÒÃÇÐèÒªÔÚһ׼ʱ¼ä¹æÄ£ºóɾ³ýÓû§×¢²áµ«Î´¼¤»îÕ˺ţ¬»òÕßɾ³ýһЩÓâÆڵĻỰÐÅÏ¢µÈ¡£
¹ØÓÚÕâÖÖ°´ÆÚɾ³ýÊý¾ÝµÄÐèÇó£¬ÎÒÃÇ¿ÉÒÔÊÖ¶¯±àд׼ʱʹÃü£¬ÔÚÖ¸¶¨µÄʱ¼äµãÖ´ÐÐÊý¾Ýɾ³ý²Ù×÷¡£¿ÉÊÇ£¬ÊÖ¶¯±àд׼ʱʹÃü±£´æÒÔÏÂÎÊÌ⣺
ʱ¼äÖÎÀíδ±ã£¬ÐèҪÿ¸ôÒ»¶Îʱ¼äÊÖ¶¯Ö´ÐÐɾ³ý²Ù×÷¡£
ÈÝÒ×ÍÉ»¯£¬ÌØÊâÊǵ±ÐèҪͬʱɾ³ý¶àÕŹØÁªµÄ±íʱ£¬ÊÖ¶¯Ö´ÐоÍÈÝÒ×·ºÆðÂß¼¹ýʧ¡£
Ϊ´Ë£¬ÎÒÃÇÐèÒª¿ª·¢Ò»ÖÖ×Ô¶¯É¾³ýÊý¾ÝµÄ»úÖÆ£¬ÒÔ¼ò»¯Êý¾Ýɾ³ýµÄÁ÷³Ì£¬²¢ïÔÌÍÉ»¯µÄ¿ÉÄÜÐÔ¡£
¶þ¡¢LaravelµÄ×Ô¶¯É¾³ý»úÖÆ
ÔÚLaravelÖУ¬ÎÒÃÇ¿ÉÒÔͨ¹ýEloquent ORMÌṩµÄ¡°Èíɾ³ý¡±»úÖÆÀ´µÖ´ï×Ô¶¯É¾³ýÊý¾ÝµÄЧ¹û¡£Èíɾ³ýÊÇÖ¸µ±ÎÒÃÇÔÚÊý¾Ý¿âÖÐɾ³ýijÐÐÊý¾Ýʱ£¬²¢²»ÊÇÖ±½Óɾ³ý£¬¶øÊǽ«¸ÃÐÐÊý¾ÝµÄdeleted_at×Ö¶ÎÉèÖÃΪһ¸ö·Ç¿ÕµÄʱ¼ä´ÁÖµ¡£
ͨ¹ýÔÚLaravelµÄÄ£×ÓÖÐʹÓÃÈíɾ³ý»úÖÆ£¬ÎÒÃÇ¿ÉÒÔʵÏÖÒÔϹ¦Ð§£º
µ±ÎÒÃÇŲÓÃdelete()ÒªÁìɾ³ýijÌõ¼Í¼ʱ£¬¸ÃÌõ¼Í¼µÄdeleted_at×ֶλá×Ô¶¯±»¸³Ò»¸öʱ¼ä´ÁÖµ£»
µ±ÎÒÃÇŲÓÃrestore()ÒªÁì»Ö¸´Ä³Ìõ¼Í¼ʱ£¬¸ÃÌõ¼Í¼ÔÀ´µÄdeleted_at×Ö¶ÎÖµ»á±»Çå¿Õ£»
µ±ÎÒÃÇŲÓÃforceDelete()ÒªÁìÓÀÊÀɾ³ýijÌõ¼Í¼ʱ£¬¸ÃÌõ¼Í¼µÄdeleted_at×Ö¶ÎÖµ²»Æð×÷Óá£
Óɴ˿ɼû£¬Èíɾ³ý»úÖÆ¿ÉÒÔºÜÀû±ãµØ½«ÀúÊ·ÉÑþ³ØýµÄÊý¾Ý»¹Ô»ØÀ´£¬´Ó¶øÓÐÓõØÒþ²Øɾ³ýµÄ¼Í¼£¬¶ø²»»áÓ°ÏìÊý¾Ý¿âµÄÍêÕûÐÔ¡£
Èý¡¢ÊµÏÖ×Ô¶¯É¾³ýÊý¾ÝµÄ°ì·¨
ÔÚLaravelÖУ¬ÎÒÃÇ¿ÉÒÔʹÓÃArtisanÏÂÁîÀ´ÊµÏÖ׼ʱɾ³ýÊý¾Ý¡£ÏêϸʵÏÖÒªÁìÈçÏ£º
½¨ÉèÒ»¸öConsoleÏÂÁîÀà
Ê×ÏÈ£¬ÎÒÃÇÔÚapp\Console\CommandsĿ¼Ï½¨ÉèÒ»¸öÏÂÁîÀ࣬²¢¼ÌÐøIlluminate\Console\CommandÀà¡£ÔÚ¿ª·¢Àú³ÌÖУ¬ÎÒÃÇ¿ÉÒÔƾ֤×Ô¼ºµÄÐèÒª¶Ô¸ÃÀà¾ÙÐÐÐ޸ĺÍÀ©Õ¹¡£
±àд×Ô¶¯É¾³ýÊý¾ÝµÄÒªÁì
ÔÚCustomDeleteCommandÀàÖУ¬ÎÒÃÇÐèÒª±àдһ¸öÃûΪdelete()µÄÒªÁ죬²¢ÊµÏÖÐèÒª×Ô¶¯É¾³ýµÄÊý¾ÝµÄÂß¼¡£ÀýÈ磺
/** * Execute the console command. * * @return void */ public function handle() { $now = now(); //Ä¿½ñʱ¼ä $expiredTime = $now->subDay(7); //ÓâÆÚʱ¼äΪһÖÜÇ° //ɾ³ýusers±íÖÐdeleted_at×ֶδóÓÚ$expiredTimeµÄ¼Í¼ DB::table('users') ->where('deleted_at', 'delete(); //ɾ³ýposts±í¼°Æä¹ØÁªµÄcomments±íÖÐdeleted_at×ֶδóÓÚ$expiredTimeµÄ¼Í¼ Post::whereHas('comments', function ($query) use ($expiredTime) { $query->where('deleted_at', 'where('deleted_at', 'delete(); }
µÇ¼ºó¸´ÖÆ
ÉÏÊö´úÂëÖУ¬ÎÒÃǽç˵ÁËÒ»¸öÓâÆÚʱ¼ä$expiredTime£¬È»ºóʹÓÃLaravelµÄDBºÍEloquent ORMÀ´É¾³ýusersºÍposts±íÖÐÇкÏÌõ¼þµÄ¼Í¼¡£
ÐèҪעÖصÄÊÇ£¬ÎÒÃÇʹÓÃÁËEloquent ORMµÄwhereHas()ÒªÁìÀ´É¾³ýposts±í¼°Æä¹ØÁªµÄcomments±íÖÐÇкÏÌõ¼þµÄ¼Í¼¡£
×¢²á×Ô¶¯É¾³ýÊý¾ÝµÄÏÂÁî
×îºó£¬ÎÒÃÇÐèÒª½«CustomDeleteCommandÏÂÁî×¢²áµ½ArtisanµÄÏÂÁîÁбíÖУ¬ÒÔ±ã¿ÉÒÔÔÚÖÕ¶ËÖÐÔËÐиÃÏÂÁî¡£
ÎÒÃÇ¿ÉÒÔÔÚapp\Console\Kernel.phpÎļþÖеÄscheduleÒªÁìÖÐ×¢²á¸ÃÏÂÁî¡£ÀýÈ磺
protected function schedule(Schedule $schedule) { $schedule->command('delete:data')->daily(); }
µÇ¼ºó¸´ÖÆ
ÉÏÊö´úÂëʵÏÖÁËÖðÈÕ×Ô¶¯Ö´ÐÐCustomDeleteCommandµÄ¹¦Ð§¡£ÎÒÃÇÒ²¿ÉÒÔƾ֤ÐèÒªÐ޸ĸÃÒªÁ죬ʵÏÖ×Ô½ç˵׼ʱɾ³ýÊý¾Ý¹¦Ð§¡£
ËÄ¡¢×ܽá
±¾ÎÄÏÈÈÝÁËÔÚLaravelÖÐʵÏÖ×Ô¶¯É¾³ýÊý¾ÝµÄÒªÁ졣ͨ¹ýʹÓÃEloquent ORMµÄÈíɾ³ý»úÖÆ£¬ÎÒÃÇ¿ÉÒÔºÜÀû±ãµØʵÏÖÔÚһ׼ʱ¼ä¹æÄ£ÄÚ×Ô¶¯É¾³ýÎÞÓõÄÊý¾Ý£¬´Ó¶ø¼õÇáÁËÊÖ¶¯É¾³ýÊý¾ÝµÄѹÁ¦£¬²¢Ìá¸ßÁËWebÓ¦ÓõÄЧÂÊ¡£
ÔÚÏÖʵ¿ª·¢ÖУ¬ÎÒÃÇ¿ÉÒÔƾ֤ÐèÒª¶ÔÕâ¸öÒªÁì¾ÙÐиü¸ÄºÍÀ©Õ¹£¬ÒÔÖª×ã²î±ðµÄÓªÒµÐèÇó¡£Ï£ÍûÕâƪÎÄÕ¶ÔLaravel¿ª·¢ÕßÓÐËù×ÊÖú¡£
ÒÔÉϾÍÊÇlaravelÔõôÌí¼ÓÊý¾Ý×Ô¶¯É¾³ý¹¦Ð§µÄÏêϸÄÚÈÝ£¬¸ü¶àÇë¹Ø×¢±¾ÍøÄÚÆäËüÏà¹ØÎÄÕ£¡