尊龙凯时人生就是搏

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 ɾ³ýд·¨µÄÏêϸÄÚÈÝ £¬¸ü¶àÇë¹Ø×¢±¾ÍøÄÚÆäËüÏà¹ØÎÄÕ£¡

ÃâÔð˵Ã÷£ºÒÔÉÏչʾÄÚÈÝȪԴÓÚÏàÖúýÌå¡¢ÆóÒµ»ú¹¹¡¢ÍøÓÑÌṩ»òÍøÂçÍøÂçÕûÀí £¬°æȨÕùÒéÓë±¾Õ¾ÎÞ¹Ø £¬ÎÄÕÂÉæ¼°¿´·¨Óë¿´·¨²»´ú±í尊龙凯时人生就是搏ÂËÓÍ»úÍø¹Ù·½Ì¬¶È £¬Çë¶ÁÕß½ö×ö²Î¿¼ ¡£±¾ÎĽӴýתÔØ £¬×ªÔØÇë˵Ã÷À´ÓÉ ¡£ÈôÄúÒÔΪ±¾ÎÄÇÖÕ¼ÁËÄúµÄ°æȨÐÅÏ¢ £¬»òÄú·¢Ã÷¸ÃÄÚÈÝÓÐÈκÎÉæ¼°ÓÐÎ¥¹«µÂ¡¢Ã°·¸Ö´·¨µÈÎ¥·¨ÐÅÏ¢ £¬ÇëÄúÁ¬Ã¦ÁªÏµ尊龙凯时人生就是搏ʵʱÐÞÕý»òɾ³ý ¡£

Ïà¹ØÐÂÎÅ

ÁªÏµ尊龙凯时人生就是搏

18523999891

¿É΢ÐÅÔÚÏß×Éѯ

ÊÂÇéʱ¼ä£ºÖÜÒ»ÖÁÖÜÎå £¬9:30-18:30 £¬½ÚãåÈÕÐÝÏ¢

QR code
sitemap¡¢ÍøÕ¾µØͼ