尊龙凯时人生就是搏

Ïê½âLaravel×ÊԴ·ÓɵÄɾ³ý²Ù×÷

laravelÊÇÒ»¿î¹¦Ð§Ç¿Ê¢µÄphp¿ò¼Ü£¬ÔÚweb¿ª·¢ÁìÓò¾ßÓÐÆÕ±éµÄÓ¦Óá£ÆäÖУ¬×ÊԴ·ÓÉÊÇlaravelÖкÜÊÇÖ÷ÒªµÄ¿´·¨Ö®Ò»£¬Ëü¿ÉÒÔ×ÊÖúÎÒÃÇ¿ìËٵع¹½¨restful api¡£ÔÚ×ÊԴ·ÓɵÄʹÓÃÀú³ÌÖУ¬É¾³ý²Ù×÷ÊǷǾ­³£¼ûµÄ£¬Òò´Ë±¾ÆªÎÄÕÂÖ÷ÒªÏÈÈÝlaravel×ÊԴ·ÓɵÄɾ³ý²Ù×÷¡£

Ò»¡¢×ÊԴ·Óɼò½é

ÔÚLaravelÖУ¬×ÊԴ·ÓÉ¿ÉÒÔͨ¹ýRoute::resourceÒªÁìÀ´½ç˵¡£Õâ¸öÒªÌå»á×Ô¶¯½ç˵Æ߸ö³£ÓõÄ·ÓɲÙ×÷ÒªÁ죬»®·ÖΪ£ºindex¡¢create¡¢store¡¢show¡¢edit¡¢updateºÍdestroy¡£

ÀýÈ磬ÎÒÃÇ¿ÉÒÔͨ¹ýÒÔÏ´úÂë½ç˵һ¸ö¼òÆÓµÄ×ÊԴ·ÓÉ£º

Route::resource('posts', 'PostController');

µÇ¼ºó¸´ÖÆ

ÕâÑù£¬Laravel»áΪÎÒÃÇ×Ô¶¯ÌìÉú/posts¡¢/posts/create¡¢/posts/{post}¡¢/posts/{post}/editµÈÆ߸ö·ÓÉ¡£

¶þ¡¢É¾³ý²Ù×÷

ÔÚ×ÊԴ·ÓÉÖУ¬É¾³ý²ÙÞÏÞεóÄÑÓ¦µÄÊÇdestroyÒªÁì¡£µ±Óû§ÔÚWebÓ¦ÓÃÖеã»÷ɾ³ý°´Å¥Ê±£¬ÎÒÃÇÐèÒª½á¹¹Ò»¸öDELETEÇëÇó£¬Laravel¾Í»áŲÓÃdestroyÒªÁìÀ´Íê³É¸Ã²Ù×÷¡£

½ç˵·ÓÉ

ÎÒÃÇÐèҪΪdestroyÒªÁì½ç˵һ¸ö·ÓÉ£¬ÀýÈ磺

Route::delete('posts/{post}', 'PostController@destroy')->name('posts.destroy');

µÇ¼ºó¸´ÖÆ

Õâ¸ö·ÓÉ»áÆ¥ÅäDELETEÇëÇóºÍ/posts/{post}·¾¶£¬²¢½«ÇëÇóת·¢µ½PostControllerµÄdestroyÒªÁì¡£

¿ØÖÆÆ÷ÒªÁì

ÔÚ¿ØÖÆÆ÷ÖУ¬ÎÒÃÇÐèÒª»ñȡɾ³ý²Ù×÷Ëù¶ÔÓ¦µÄʵÌ幤¾ß£¬²¢Å²ÓÃdeleteÒªÁìÀ´´ÓÊý¾Ý¿âÖÐɾ³ýËü¡£ÀýÈ磺

public function destroy($id)
{
    $post = Post::find($id);
    $post->delete();
    return redirect()->route('posts.index');
}

µÇ¼ºó¸´ÖÆ

ÕâÀÎÒÃÇÏÈʹÓÃPost::findÒªÁìÀ´ÕÒµ½ÒªÉ¾³ýµÄʵÌ幤¾ß£¬È»ºóʹÓÃdeleteÒªÁì´ÓÊý¾Ý¿âÖÐɾ³ýËü¡£×îºó£¬ÎÒÃÇʹÓÃredirectÒªÁ콫Óû§Öض¨Ïòµ½Ìû×ÓÁбíÒ³Ãæ¡£

HTML±íµ¥

ÔÚHTML±íµ¥ÖУ¬ÎÒÃÇÐèҪΪɾ³ý°´Å¥ÌìÉúÒ»¸ö±íµ¥£¬²¢ÔÚÆäÖÐÌí¼ÓÒ»¸öÒþ²Ø×Ö¶Î_method£¬ÒÔÌåÏÖÕâÊÇÒ»¸öDELETEÇëÇó¡£

ÀýÈ磬ÎÒÃÇ¿ÉÒÔʹÓÃÒÔÏ´úÂëÌìÉúÒ»¸öɾ³ý°´Å¥£º


µÇ¼ºó¸´ÖÆ id) }}” method=”POST”>     {{ csrf_field() }}     {{ method_field(‘DELETE’) }}      ɾ³ý

ÕâÀÎÒÃÇʹÓÃrouteÒªÁìÌìÉú´ø²ÎÊýµÄ/posts/{post}·¾¶£¬²¢ÉèÖÃ±íµ¥µÄmethodÊôÐÔΪDELETE¡£È»ºó£¬ÎÒÃÇʹÓÃcsrf_fieldÒªÁìÌìÉúCSRFÁîÅƺÍmethod_fieldÒªÁìÌìÉú_method×ֶΡ£

ÃüÃû·ÓÉ

×îºó£¬ÔÚ½ç˵·ÓÉʱ£¬ÎÒÃÇ¿ÉÒÔΪdestroyÒªÁìÉèÖÃÒ»¸öÃüÃû·ÓÉÃû×Öposts.destroy¡£Õâ¸öÃû×Ö¿ÉÒÔ×ÊÖúÎÒÃǸüÀû±ãµØÌìÉúURLºÍÖض¨Ïòµ½É¾³ý²Ù×÷Íê³ÉºóµÄÒ³Ãæ¡£

ÀýÈ磬ÎÒÃÇ¿ÉÒÔʹÓÃÒÔÏ´úÂëÌìÉúÒ»¸öÖض¨ÏòÏìÓ¦£º

return redirect()->route('posts.index');

µÇ¼ºó¸´ÖÆ

ÕâÀÎÒÃÇʹÓÃrouteÒªÁì²¢´«Èëposts.index²ÎÊýÀ´ÌìÉúposts.index·¾¶£¬´Ó¶ø½«Óû§Öض¨Ïòµ½Ìû×ÓÁбíÒ³Ãæ¡£

Èý¡¢×ܽá

ͨ¹ýÒÔÉÏ°ì·¨£¬ÎÒÃÇ¿ÉÒÔÇáËɵØÔÚLaravelÖÐʵÏÖ×ÊԴ·ÓɵÄɾ³ý²Ù×÷¡£µ±Óû§ÔÚWebÓ¦ÓÃÖеã»÷ɾ³ý°´Å¥Ê±£¬Laravel»á½á¹¹Ò»¸öDELETEÇëÇ󣬲¢Å²ÓÿØÖÆÆ÷µÄdestroyÒªÁìÍê³Éɾ³ý¡£Ê¹ÓÃÃüÃû·ÓÉ¿ÉÒÔ¸üÀû±ãµØÌìÉúURLºÍÖض¨ÏòÏìÓ¦¡£

ÒÔÉϾÍÊÇÏê½âLaravel×ÊԴ·ÓɵÄɾ³ý²Ù×÷µÄÏêϸÄÚÈÝ£¬¸ü¶àÇë¹Ø×¢±¾ÍøÄÚÆäËüÏà¹ØÎÄÕ£¡

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

Ïà¹ØÐÂÎÅ

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

18523999891

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

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

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