laravel db ɾ³ý
laravel db ɾ³ý
LaravelÊÇÒ»¸öºÜÊÇÊܽӴýµÄPHP¿ò¼Ü£¬ÓÉÓÚÆä¼òÆÓÒ×ÓÃÇÒ¹¦Ð§Ç¿Ê¢£¬Ô½À´Ô½¶àµÄ¿ª·¢Õß×îÏÈʹÓÃËü¡£ÔÚLaravelÓ¦ÓóÌÐòÖУ¬Ê¹ÓÃÊý¾Ý¿âÊǷǾ³£¼ûµÄ£¬¿ÉÊÇÔÚʹÓÃÖУ¬ÎÒÃÇ¿ÉÄÜ»áÓöµ½Êý¾Ýɾ³ýµÄÇéÐΡ£±¾ÎĽ«ÏÈÈÝLaravelÖÐÔõÑùɾ³ýÊý¾Ý¿âÖеÄÊý¾Ý¡£
LaravelÖеÄɾ³ý²Ù×÷¿ÉÒÔʹÓÃEloquent ORM»òÅÌÎʹ¹½¨Æ÷¡£ÎÒÃÇÏÈÏÈÈÝʹÓÃEloquent ORM¾ÙÐÐɾ³ýµÄÒªÁì¡£
ʹÓÃEloquent ORM¾ÙÐÐɾ³ý
ÔÚLaravelÖУ¬Eloquent ORMÊÇÒ»¸öºÜÊÇÇ¿Ê¢ºÍÒ×ÓÚʹÓõŤ¾ß¡£Ëü¿ÉÒÔ×ÊÖúÎÒÃǸüÇáËɵØÓëÊý¾Ý¿â½»»¥¡£ÎÒÃÇ¿ÉÒÔʹÓÃEloquent ORMɾ³ýÒ»¸ö»ò¶à¸öÊý¾Ý¿â¼Í¼¡£
1.1 µ¥¸ö¼Í¼ɾ³ý
ɾ³ýµ¥¸ö¼Í¼µÄ´úÂëºÜÊǼòÆÓ£¬Ö»ÐèʹÓÃdeleteÒªÁì¼´¿É¡£ÀýÈ磬ÎÒÃÇÓÐÒ»¸öUserÄ£×Ó£¬¸ÃÄ£×Ó¶ÔÓ¦ÁËÒ»¸öusers±í£º
// ɾ³ýidΪ1µÄÓû§ $user = AppUser::find(1); $user->delete();
µÇ¼ºó¸´ÖÆ
ÉÏÃæµÄ´úÂ뽫ɾ³ýusers±íÖÐidΪ1µÄ¼Í¼¡£
1.2 ¶à¸ö¼Í¼ɾ³ý
ÈôÊÇÎÒÃÇҪɾ³ý¶à¸ö¼Í¼£¬Ôò¿ÉÒÔʹÓÃwhereÌõ¼þ¾ÙÐÐɸѡ¡£ÀýÈ磬ÎÒÃÇҪɾ³ýËùÓÐÄêËê´óÓÚ30ËêµÄÓû§£¬¿ÉÒÔʹÓÃÈçÏ´úÂ룺
// ɾ³ýÄêËê´óÓÚ30ËêµÄÓû§ $users = AppUser::where('age', '>', 30)->get(); $users->delete();
µÇ¼ºó¸´ÖÆ
ÉÏÃæµÄ´úÂ뽫ɾ³ýusers±íÖÐËùÓÐÄêËê´óÓÚ30ËêµÄ¼Í¼¡£
ʹÓÃÅÌÎʹ¹½¨Æ÷¾ÙÐÐɾ³ý
³ýÁËʹÓÃEloquent ORM¾ÙÐÐɾ³ýÖ®Í⣬ÎÒÃÇ»¹¿ÉÒÔʹÓÃÅÌÎʹ¹½¨Æ÷¾ÙÐÐɾ³ý¡£ÅÌÎʹ¹½¨Æ÷ÌṩÁ˸üÎÞаµÄ·½·¨À´¹¹½¨SQLÅÌÎÊÓï¾ä¡£
2.1 µ¥¸ö¼Í¼ɾ³ý
ÓëEloquent ORMÒ»Ñù£¬ÎÒÃÇ¿ÉÒÔʹÓÃÅÌÎʹ¹½¨Æ÷ɾ³ýµ¥¸ö¼Í¼¡£ÀýÈ磬ÎÒÃÇÓÐÒ»¸öusers±í£¬ÆäÖаüÀ¨Ò»¸öÃûΪidµÄ×ֶΡ£ÒªÉ¾³ýidΪ1µÄ¼Í¼£¬¿ÉÒÔʹÓÃÈçÏ´úÂ룺
// ɾ³ýidΪ1µÄÓû§ DB::table('users')->where('id', '=', 1)->delete();
µÇ¼ºó¸´ÖÆ
ÉÏÃæµÄ´úÂ뽫ɾ³ýusers±íÖÐidΪ1µÄ¼Í¼¡£
2.2 ¶à¸ö¼Í¼ɾ³ý
ÈôÊÇÎÒÃÇҪɾ³ý¶à¸ö¼Í¼£¬Ôò¿ÉÒÔʹÓÃwhereInÌõ¼þ¾ÙÐÐɸѡ¡£ÀýÈ磬ÎÒÃÇҪɾ³ýidΪ1¡¢2¡¢3µÄÓû§£¬¿ÉÒÔʹÓÃÈçÏ´úÂ룺
// ɾ³ýidΪ1¡¢2¡¢3µÄÓû§ DB::table('users')->whereIn('id', [1, 2, 3])->delete();
µÇ¼ºó¸´ÖÆ
ÉÏÃæµÄ´úÂ뽫ɾ³ýusers±íÖÐidΪ1¡¢2¡¢3µÄ¼Í¼¡£
×ܽá
ÔÚLaravelÖУ¬É¾³ýÊý¾Ý¿â¼Í¼ºÜÊÇÈÝÒס£ÎÒÃÇ¿ÉÒÔʹÓÃEloquent ORM»òÅÌÎʹ¹½¨Æ÷¾ÙÐÐɾ³ý¡£ÔÚɾ³ý¼Í¼ʱ£¬ÇëÈ·±£ÄúÒѾȷÈÑþ³ØýµÄ¹¤¾ßÊÇ׼ȷµÄ£¬²¢×ñÕÕ×î¼Ñʵ¼ùÒÔÈ·±£Ó¦ÓóÌÐòµÄÎȹÌÐÔ¡£
ÒÔÉϾÍÊÇlaravel db ɾ³ýµÄÏêϸÄÚÈÝ£¬¸ü¶àÇë¹Ø×¢±¾ÍøÄÚÆäËüÏà¹ØÎÄÕ£¡