laravel ɾ³ýд·¨
laravel ÊÇÒ»¿îÊ¢ÐÐµÄ php web ¿ò¼Ü£¬ÒòÆäÇ¿Ê¢µÄ¹¦Ð§ºÍÒ×ÓÃÐÔ¶ø±¸ÊÜ¿ª·¢Õß½Ó´ý¡£ÔÚÏÖʵ¿ª·¢ÖУ¬ÎÒÃǾ³£ÐèҪɾ³ýÊý¾Ý¿âÖеÄÊý¾Ý¡£±¾ÎĽ«ÏÈÈÝ laravel µÄɾ³ýд·¨£¬×ÊÖúÄã¸üºÃµØÃ÷È·ºÍʹÓà laravel¡£
ɾ³ýµ¥ÌõÊý¾Ý
Laravel ÌṩÁ˶àÖÖɾ³ýµ¥ÌõÊý¾ÝµÄÒªÁ죬ÒÔÏ»®·Ö¾ÙÐÐÏÈÈÝ¡£
£¨1£©Ê¹ÓÃÄ£×Ó×Ô´øµÄ delete() ÒªÁì
ÔÚ Laravel ÖУ¬Ã¿¸öÄ£×Ó¶¼×Ô´øÁËÒ»¸ö delete() ÒªÁ죬¿ÉÒÔÓÃÓÚɾ³ý¸ÃÄ£×Ó¶ÔÓ¦µÄÊý¾Ý¿â¼Í¼¡£ÏÂÃæÊÇʹÓÃÒªÁìµÄʾÀý´úÂ룺
$user = User::find(1); $user->delete();
µÇ¼ºó¸´ÖÆ µÇ¼ºó¸´ÖÆ
ÉÏÊö´úÂëÖУ¬ÎÒÃÇÊ×ÏÈʹÓà User Ä£× find() ÒªÁì²éÕÒ ID Ϊ 1 µÄ¼Í¼£¬È»ºóŲÓà delete() ÒªÁìɾ³ý¸Ã¼Í¼¡£
ÐèҪעÖصÄÊÇ£¬delete() ÒªÁì²¢²»»á¼ì²éÆäËûÄ£×ÓÊÇ·ñÒÀÀµÓڸüͼ£¬Òò´ËÔÚɾ³ý¼Í¼֮ǰÐèҪȷ±£ËüÓëÆäËûÊý¾ÝûÓйØÁª¡£
£¨2£©Ê¹Óà ORM µÄ destroy() ÒªÁì
ORM£¨Object-Relational Mapping£¬¹¤¾ß¹ØϵӳÉ䣩ÊÇÒ»ÖÖ½«¹¤¾ßºÍ¹ØϵÐÍÊý¾Ý¿âÖеÄÊý¾ÝÓ³ÉäÆðÀ´µÄÊÖÒÕ¡£Laravel ÖÐÄÚÖÃÁË ORM ¹¦Ð§£¬¿ÉÒÔʹÓÃÆä destroy() ÒªÁìɾ³ýÄ£×Ó¶ÔÓ¦µÄÊý¾Ý¿â¼Í¼¡£
ÒÔÏÂÊÇʹÓà ORM µÄ destroy() ÒªÁìɾ³ýµ¥Ìõ¼Í¼µÄʾÀý´úÂ룺
User::destroy(1);
µÇ¼ºó¸´ÖÆ
ÉÏÊö´úÂëÖУ¬ÎÒÃÇÖ±½ÓŲÓà User Ä£× destroy() ÒªÁ죬´«ÈëҪɾ³ýµÄ¼Í¼µÄ ID¡£
ÐèҪעÖصÄÊÇ£¬destroy() ÒªÁìÒ²²»»á¼ì²éÆäËûÄ£×ÓÊÇ·ñÒÀÀµÓڸüͼ£¬Òò´ËÔÚɾ³ý¼Í¼֮ǰÐèҪȷ±£ËüÓëÆäËûÊý¾ÝûÓйØÁª¡£
£¨3£©Ê¹Óà Query Builder µÄ delete() ÒªÁì
Query Builder ÊÇ Laravel ÖеÄÒ»ÖÖ»ùÓÚÁ´Ê½Å²ÓÃµÄ SQL Óï¾ä¹¹½¨Æ÷¡£ËüÌṩÁ˸»ºñµÄ²Ù×÷Êý¾Ý¿âµÄÒªÁ죬°üÀ¨É¾³ýÊý¾ÝµÄÒªÁì delete()¡£
ÒÔÏÂÊÇʹÓà Query Builder µÄ delete() ÒªÁìɾ³ýµ¥Ìõ¼Í¼µÄʾÀý´úÂ룺
DB::table('users')->where('id', 1)->delete();
µÇ¼ºó¸´ÖÆ
ÉÏÊö´úÂëÖУ¬ÎÒÃÇʹÓà DB£¨Database£©ÃÅÃæŲÓà table() ÒªÁìÖ¸¶¨ÒªÉ¾³ýµÄÊý¾Ý±íÃû£¬È»ºóʹÓà where() ÒªÁìÖ¸¶¨ÒªÉ¾³ýµÄÌõ¼þ£¬×îºóŲÓà delete() ÒªÁìÖ´ÐÐɾ³ý²Ù×÷¡£
ÐèҪעÖصÄÊÇ£¬delete() ÒªÁì²»»á´¥·¢Ä£×ÓµÄÊÂÎñºÍ¹³×Ó£¬Òò´ËÈôÊÇÄãʹÓÃÁËÕâЩ¹¦Ð§£¬½¨ÒéʹÓÃÉÏÊöÒªÁìÖ®Ò»¡£
ÅúÁ¿É¾³ýÊý¾Ý
³ýÁËɾ³ýµ¥ÌõÊý¾Ý¿â¼Í¼£¬Laravel »¹ÌṩÁËÀû±ãµÄÅúÁ¿É¾³ýÊý¾ÝµÄÒªÁì¡£ÒÔÏÂÊÇÁ½ÖÖ³£ÓõÄÅúÁ¿É¾³ýÒªÁìµÄÏÈÈÝ¡£
£¨1£©Ê¹Óà ORM µÄ delete() ÒªÁì
ORM µÄ delete() ÒªÁì²»µ«¿ÉÒÔɾ³ýµ¥Ìõ¼Í¼£¬»¹¿ÉÒÔÅúÁ¿É¾³ý¶àÌõ¼Í¼¡£ÒÔÏÂÊÇʹÓøÃÒªÁìÅúÁ¿É¾³ý¼Í¼µÄʾÀý´úÂ룺
User::where('age', '>', 18)->delete();
µÇ¼ºó¸´ÖÆ
ÉÏÊö´úÂëÖУ¬ÎÒÃÇÏÈʹÓà where() ÒªÁìɸѡ³öÄêËê´óÓÚ 18 ËêµÄÓû§£¬È»ºóŲÓà delete() ÒªÁìɾ³ýÕâЩ¼Í¼¡£
ÐèҪעÖصÄÊÇ£¬delete() ÒªÁì²»»á´¥·¢Ä£×ÓµÄÊÂÎñºÍ¹³×Ó£¬Òò´ËÈôÊÇÄãʹÓÃÁËÕâЩ¹¦Ð§£¬½¨ÒéʹÓÃÉÏÊöÒªÁìÖ®Ò»¡£
£¨2£©Ê¹Óà Query Builder µÄ delete() ÒªÁì
Query Builder µÄ delete() ÒªÁìÒ²¿ÉÒÔÓÃÓÚÅúÁ¿É¾³ý¼Í¼¡£ÒÔÏÂÊÇʹÓøÃÒªÁìÅúÁ¿É¾³ý¼Í¼µÄʾÀý´úÂ룺
DB::table('users')->where('age', '>', 18)->delete();
µÇ¼ºó¸´ÖÆ
ÉÏÊö´úÂëÖУ¬ÎÒÃÇʹÓà where() ÒªÁìÖ¸¶¨ÒªÉ¾³ýµÄÌõ¼þ£¬²¢Ê¹Óà delete() ÒªÁìÖ´ÐÐɾ³ý²Ù×÷¡£
ÐèҪעÖصÄÊÇ£¬delete() ÒªÁì²»»á´¥·¢Ä£×ÓµÄÊÂÎñºÍ¹³×Ó£¬Òò´ËÈôÊÇÄãʹÓÃÁËÕâЩ¹¦Ð§£¬½¨ÒéʹÓÃÉÏÊöÒªÁìÖ®Ò»¡£
Èíɾ³ý
ÔÚÏÖʵ¿ª·¢ÖУ¬ÎÒÃÇÓÐʱ¼äÐèÒª±£´æÒÑɾ³ýÊý¾ÝµÄ¼Í¼£¬ÒÔ±ãÈÕºó»Ö¸´¡£´Ëʱ£¬Èíɾ³ý£¨Soft Delete£©¾ÍÅÉÉÏÓó¡ÁË¡£Laravel ÄÚÖÃÁËÈíɾ³ý¹¦Ð§£¬¿ÉÒÔ×ÊÖúÎÒÃÇÒÔÇå¾²µÄ·½·¨É¾³ýÊý¾Ý¡£
Èíɾ³ýʵÏֵĽ¹µãÍ·ÄÔÊÇÔÚÊý¾Ý¿â±íÖÐÌí¼ÓÒ»¸ö deleted_at ×ֶΣ¬ÓÃÓڼͼɾ³ýʱ¼ä¡£µ±ÅÌÎÊÊý¾Ýʱ£¬Laravel »á×Ô¶¯¹ýÂËÒÑÈíɾ³ýµÄ¼Í¼£¬Ê¹Æä²»ÔÚÅÌÎÊЧ¹ûÖÐÏÔʾ¡£
ÒÔÏÂÊÇʹÓà Laravel ʵÏÖÈíɾ³ýµÄʾÀý´úÂ룺
Ê×ÏÈ£¬ÐèÒªÔÚÄ£×ÓÖÐʹÓà SoftDeletes trait£º
<?php namespace App; use IlluminateDatabaseEloquentModel; use IlluminateDatabaseEloquentSoftDeletes; class User extends Model { use SoftDeletes; }
µÇ¼ºó¸´ÖÆ
È»ºó£¬ÔÚÊý¾Ý¿âǨáãÖÐÌí¼Ó deleted_at ×ֶΣº
Schema::table('users', function (Blueprint $table) { $table->softDeletes(); });
µÇ¼ºó¸´ÖÆ
×îºó£¬ÔÚ¿ØÖÆÆ÷ÖÐʹÓà delete() ÒªÁìÒÔÈíɾ³ýÊý¾Ý£º
$user = User::find(1); $user->delete();
µÇ¼ºó¸´ÖÆ µÇ¼ºó¸´ÖÆ
ÈôÊÇÐèÒª»Ö¸´ÒÑɾ³ýµÄÊý¾Ý£¬¿ÉÒÔʹÓà restore() ÒªÁ죺
$user = User::withTrashed()->find(1); $user->restore();
µÇ¼ºó¸´ÖÆ
Èíɾ³ý¿ÉÒÔ×ÊÖúÎÒÃǸüºÃµØÖÎÀíÒÑɾ³ýÊý¾Ý£¬¶ø²»±Øµ£ÐÄÎóɾ³ýÊý¾ÝµÄÎÊÌâ¡£²»¹ýÐèҪעÖصÄÊÇ£¬Èíɾ³ý²¢²»ÊÇÍòÄܵĽâ¾ö¼Æ»®£¬ÔÚһЩӪҵ³¡¾°ÖУ¬ÐèÒª³¹µ×ɾ³ýÊý¾Ý²Å»ª°ü¹ÜÊý¾ÝµÄÍêÕûÐÔºÍÇå¾²ÐÔ¡£
×ܽá
Laravel ÌṩÁ˸»ºñµÄɾ³ýÊý¾ÝµÄÒªÁ죬¿ÉÒÔƾ֤ÏêϸӪҵÐèÇóÑ¡Ôñ²î±ðµÄÒªÁì¡£ÔÚÏÖʵ¿ª·¢ÖУ¬ÎÒÃÇÐèҪƾ֤ÇéÐÎÎÈÖØ˼Á¿ÊÇ·ñʹÓÃÈíɾ³ý»òÅúÁ¿É¾³ýµÈ²Ù×÷£¬ÒÔÈ·±£Êý¾ÝµÄÍêÕûÐÔºÍÇå¾²ÐÔ¡£Í¬Ê±£¬ÐèҪעÖØÔÚɾ³ýÊý¾Ýʱ¿ÉÄÜ»áÓ°ÏìÆäËûÄ£×Ó»ò¹¦Ð§µÄÕý³£ÔËÐУ¬ÐèÒª¾ÙÐгä·ÖµÄ²âÊÔºÍÑéÖ¤¡£
ÒÔÉϾÍÊÇlaravel ɾ³ýд·¨µÄÏêϸÄÚÈÝ£¬¸ü¶àÇë¹Ø×¢±¾ÍøÄÚÆäËüÏà¹ØÎÄÕ£¡