laravel ¶àÌõ¼þɾ³ý
laravel ÊÇÒ»¿î±ã½Ý¡¢ÓÅÑÅµÄ php web ¿ª·¢¿ò¼Ü£¬ËüΪ¿ª·¢ÕßÌṩÁ˸»ºñµÄ¹¦Ð§ºÍÒ×ÓÚʹÓõÄÓï·¨¡£ÔÚÏÖʵÏîÄ¿ÖУ¬¶àÌõ¼þɾ³ýÊÇÒ»¸ö¾³£·ºÆðµÄÐèÇó¡£±¾ÎĽ«ÏÈÈÝÔõÑùʹÓà laravel À´ÊµÏÖ¶àÌõ¼þɾ³ý¹¦Ð§¡£
Ò»¡¢ÐèÇóÆÊÎö
ÔÚÏÖʵ¿ª·¢ÖУ¬ÎÒÃǾ³£ÐèҪƾ֤¶à¸öÌõ¼þÀ´É¾³ýÊý¾Ý¿âÖеļͼ¡£ºÃ±È£¬ÎÒÃÇÐèҪƾ֤Óû§ ID ºÍ²úÆ· ID ɾ³ýij¸öÓû§¹ºÖõÄij¸ö²úÆ·¼Í¼¡£ÔÚʹÓà Laravel ¾ÙÐпª·¢Ê±£¬¶àÌõ¼þɾ³ý¿ÉÒÔʹÓà Eloquent ÌṩµÄ where() ÒªÁìʵÏÖ¡£´úÂëÈçÏ£º
DB::table('table_name') ->where('column_name_1', '=', $value_1) ->where('column_name_2', '=', $value_2) ->delete();
µÇ¼ºó¸´ÖÆ
¿ÉÊÇ£¬ÈôÊÇÓжà¸öÌõ¼þÐèҪɾ³ý£¬ÄÇô´úÂë¾Í»á±äµÃÈß³¤¡£
¶þ¡¢½â¾ö¼Æ»®
ΪÏàʶ¾öÕâ¸öÎÊÌ⣬ÎÒÃÇ¿ÉÒÔʹÓà Laravel ÌṩµÄÄ£×ÓÅÌÎÊÓï·¨À´ÊµÏÖ¶àÌõ¼þɾ³ý¡£ÕâÖÖÒªÁì¿ÉÒÔÔ½·¢¾«Á·¡¢Ò׶Á£¬²¢ÇÒ¿Éά»¤ÐÔ¸üºÃ¡£
Ê×ÏÈ£¬ÎÒÃÇÐèÒªÔÚÄ£×ÓÀàÖнç˵һ¸ö¸¨ÖúÒªÁ죬¸ÃÒªÁì¿ÉÒÔÎüÊÕ¶à¸ö²ÎÊý£¬²¢½«ÕâЩ²ÎÊýת»¯ÎªÒ»¸öÅÌÎʽṹÆ÷¹¤¾ß£¨QueryBuilder£©¡£´úÂëÈçÏ£º
class ModelName extends Model { // ½ç˵¸¨ÖúÒªÁì public function multiDelete(...$conditions) { // ½á¹¹ÅÌÎʽṹÆ÷¹¤¾ß $queryBuilder = $this->newQuery(); // Ìí¼ÓÅÌÎÊÌõ¼þ foreach ($conditions as $condition) { list($column, $operator, $value) = $condition; $queryBuilder->where($column, $operator, $value); } // Ö´ÐÐɾ³ý²Ù×÷ return $queryBuilder->delete(); } }
µÇ¼ºó¸´ÖÆ
ÔÚÒÔÉÏ´úÂëÖУ¬ÎÒÃǽç˵ÁËÒ»¸ö multiDelete ¸¨ÖúÒªÁ죬¸ÃÒªÁìÎüÊÕδ±ØÊýÄ¿µÄ²ÎÊý¡£Ã¿¸ö²ÎÊý¶¼ÊÇÒ»¸ö°üÀ¨Èý¸öÔªËصÄÊý×飬»®·Ö´ú±íÅÌÎÊÌõ¼þµÄÁÐÃû¡¢²Ù×÷·ûºÍÖµ¡£È»ºó£¬ÎÒÃÇʹÓÃÄ£× newQuery ÒªÁ콨ÉèÒ»¸öÅÌÎʽṹÆ÷¹¤¾ß£¬²¢Í¨¹ý foreach Ñ»·½«´«ÈëµÄÿ¸öÌõ¼þÌí¼Óµ½ÅÌÎʽṹÆ÷¹¤¾ßÖС£×îºó£¬ÎÒÃÇŲÓÃÅÌÎʽṹÆ÷¹¤¾ßµÄ delete ÒªÁ죬ִÐÐɾ³ý²Ù×÷¡£
½ÓÏÂÀ´£¬ÎÒÃÇ¿ÉÒÔÔÚ¿ØÖÆÆ÷ÖÐʹÓÃÉÏÊöµÄÄ£×ÓÀàµÄ multiDelete ¸¨ÖúÒªÁìÀ´ÊµÏÖ¶àÌõ¼þɾ³ý¹¦Ð§¡£´úÂëÈçÏ£º
class ExampleController extends Controller { public function delete(Request $request) { $userId = $request->input('user_id'); $productId = $request->input('product_id'); $result = ModelName::multiDelete( ['user_id', '=', $userId], ['product_id', '=', $productId] ); if ($result) { return 'ɾ³ýÀÖ³É'; } else { return 'ɾ³ýʧ°Ü'; } } }
µÇ¼ºó¸´ÖÆ
ÔÚÒÔÉÏ´úÂëÖУ¬ÎÒÃÇʹÓÃÄ£×ÓÀàµÄ multiDelete ¸¨ÖúÒªÁ죬´«ÈëÁ½¸öÌõ¼þ£¬´ú±íҪɾ³ýÓû§ ID Ϊ $userId ÇÒ²úÆ· ID Ϊ $productId µÄ¼Í¼¡£×îºó£¬ÎÒÃǼì²éɾ³ý²Ù×÷µÄЧ¹û£¬²¢Æ¾Ö¤Ð§¹û·µ»Ø²î±ðµÄÐÅÏ¢¡£
Èý¡¢×ܽá
ÔÚ±¾ÎÄÖУ¬ÎÒÃÇÏÈÈÝÁËÔõÑùʹÓà Laravel ʵÏÖ¶àÌõ¼þɾ³ý¹¦Ð§¡£Í¨¹ýÌí¼ÓÒ»¸ö¸¨ÖúÒªÁ죬ÎÒÃÇ¿ÉÒÔÔ½·¢ÓÅÑŵØÖÎÀí¶à¸öÅÌÎÊÌõ¼þ£¬²¢ÇÒÄܹ»Ô½·¢Àû±ãµØά»¤´úÂ롣ͬʱ£¬ÎÒÃÇҲѧϰÁËÔõÑùʹÓà Laravel µÄÄ£×ÓÅÌÎÊÓï·¨À´¹¹½¨ÅÌÎʽṹÆ÷¹¤¾ß£¬ÒÔ¼°ÔõÑùʹÓÃÅÌÎʽṹÆ÷¹¤¾ßÀ´Ö´ÐÐɾ³ý²Ù×÷¡£ÎÒÐÅÍУ¬ÕâЩ¼¼ÇÉÄܹ»×ÊÖúÄãÔÚÏÖʵÏîÄ¿ÖÐÔ½·¢¸ßЧµØ¾ÙÐпª·¢¡£
ÒÔÉϾÍÊÇlaravel ¶àÌõ¼þɾ³ýµÄÏêϸÄÚÈÝ£¬¸ü¶àÇë¹Ø×¢±¾ÍøÄÚÆäËüÏà¹ØÎÄÕ£¡