尊龙凯时人生就是搏

laravel5.2ÅúÁ¿É¾³ý

laravel 5.2 ÊÇÒ»¿îÏÖ´ú»¯µÄ php ¿ò¼Ü £¬Êܵ½ÁËÖڶ࿪·¢ÕßµÄϲ»¶¡£ÔÚ laravel ÖÐ £¬Êý¾ÝµÄÔöɾ²é¸ÄÊǷǾ­³£¼ûµÄ²Ù×÷ £¬¶øÅúÁ¿É¾³ýÒ²²»ÆÆÀý¡£±¾ÎĽ«ÏÈÈÝʹÓà laravel 5.2 ÅúÁ¿É¾³ýÊý¾ÝµÄÒªÁì¡£

×¼±¸ÊÂÇé

ÔÚ×îÏÈ֮ǰ £¬ÎÒÃÇÐèÒªÏȽ¨ÉèÒ»¸öʾÀýÏîÄ¿À´ÑÝʾÅúÁ¿É¾³ýÊý¾ÝµÄ²Ù×÷¡£ÔÚÏÂÁîÐÐÖÐÊäÈëÒÔÏÂÏÂÁ

laravel new batch-delete-example

µÇ¼ºó¸´ÖÆ

È»ºó½øÈëÏîĿĿ¼²¢ÔËÐÐÒÔÏÂÏÂÁîÀ´½¨ÉèÒ»¸öÃûΪ posts µÄÊý¾Ý±í£º

php artisan make:model Post -m

µÇ¼ºó¸´ÖÆ

È»ºóÔÚ Post Ä£×ÓÖÐÌí¼ÓÒÔÏ´úÂ룺

<?php

namespace App;

use IlluminateDatabaseEloquentModel;

class Post extends Model
{
    protected $fillable = ['title', 'content'];
}

µÇ¼ºó¸´ÖÆ

ÔÚÌî³äÎļþ DatabaseSeeder.php ÖÐÌî³äһЩʾÀýÊý¾Ý£º

<?php

use IlluminateDatabaseSeeder;

class DatabaseSeeder extends Seeder
{
    public function run()
    {
        factory(AppPost::class, 10)->create();
    }
}

µÇ¼ºó¸´ÖÆ

×îºóÔËÐÐÒÔÏÂÏÂÁîÀ´Ìî³äÊý¾Ý£º

php artisan migrate --seed

µÇ¼ºó¸´ÖÆ

ÅúÁ¿É¾³ýÊý¾Ý

ÔÚ Laravel ÖÐ £¬ÎÒÃÇ¿ÉÒÔʹÓà Eloquent ÌṩµÄ delete ÒªÁìÀ´É¾³ý¼Í¼¡£

ÈôÊÇÎÒÃÇÏëҪɾ³ýµ¥¸ö¼Í¼ £¬¿ÉÒÔÕâÑù×ö£º

$post = Post::find(1);
$post->delete();

µÇ¼ºó¸´ÖÆ

¿ÉÊÇÈôÊÇÎÒÃÇÒªÅúÁ¿É¾³ý¶àÌõ¼Í¼Ôõô°ìÄØ£¿Õâʱ¼äÎÒÃÇ¿ÉÒÔʹÓà whereIn ÒªÁì £¬Õâ¸öÒªÁì¿ÉÒÔÎüÊÕÒ»¸öÊý×é £¬ÔÚÖ¸¶¨×Ö¶ÎÄÚÅÌÎÊÇкÏÌõ¼þµÄ¼Í¼²¢½«Æäɾ³ý¡£

ÎÒÃÇÏÈÀ´¿´Ò»Ï»ù±¾Óï·¨£º

Post::whereIn('id', $ids)->delete();

µÇ¼ºó¸´ÖÆ

ÆäÖÐ £¬$ids ÊÇÒ»¸ö°üÀ¨¶à¸ö id ÖµµÄÊý×é £¬´ú±íÁËÎÒÃÇҪɾ³ýµÄ¼Í¼µÄ id Öµ¡£

ÀýÈç £¬ÈôÊÇÎÒÃÇҪɾ³ý id Ϊ 1, 3, 5 µÄÈýÌõ¼Í¼ £¬¿ÉÒÔÕâÑù×ö£º

$ids = [1, 3, 5];
Post::whereIn('id', $ids)->delete();

µÇ¼ºó¸´ÖÆ

ËäÈ» £¬ÎÒÃÇÒ²¿ÉÒÔƾ֤ÆäËûÌõ¼þÀ´É¾³ý¼Í¼¡£ÀýÈç £¬ÈôÊÇÎÒÃÇҪɾ³ýËùÓн¨Éèʱ¼äÔçÓÚ 2022 ÄêµÄ¼Í¼ £¬¿ÉÒÔÕâÑù×ö£º

Post::where('created_at', '<', '2022-01-01 00:00:00')->delete();

µÇ¼ºó¸´ÖÆ

ÐèҪעÖصÄÊÇ £¬Ê¹Óà whereIn ÒªÌå»á×Ô¶¯×ª»»ÎªÒ»¸ö delete Óï¾ä £¬Òò´ËËü²¢²»»á¼ìË÷ËùÓÐÇкÏÌõ¼þµÄ¼Í¼²¢»®·Öɾ³ýËüÃÇ £¬¶øÊÇ»áÔÚÊý¾Ý¿â²ãÃæÖ±½Óɾ³ýÕâЩ¼Í¼ £¬Òò´ËÒªÎÈÖØʹÓá£

È·ÈÑþ³Øý²Ù×÷

µ±ÎÒÃÇʹÓà delete ÒªÁìɾ³ý¼Í¼ʱ £¬Laravel ²¢Ã»ÓÐÌṩȷÈϲÙ×÷ £¬ÕâÒâζ×ÅÒ»µ©Ö´ÐÐɾ³ý²Ù×÷¾ÍÎÞ·¨×÷·Ï¡£

ÈôÊÇÎÒÃÇÏëҪȷÈÏÓû§µÄɾ³ý²Ù×÷ £¬¿ÉÒÔÔÚÇ°¶ËÌí¼ÓÒ»¸öÈ·Èϵ¯´°»òÕßÔÚºó¶ËÌí¼ÓÒ»¸öÈ·ÈÏ¿òÀ´ÈÃÓû§È·ÈÏÊÇ·ñҪɾ³ý¼Í¼¡£

ÔÚ±¾ÎÄÖÐ £¬ÎÒÃÇʹÓà SweetAlert À´½¨ÉèÒ»¸öÈ·Èϵ¯´°¡£

Ê×ÏÈ £¬ÔÚÏÂÁîÐÐÖÐÔËÐÐÒÔÏÂÏÂÁîÀ´×°Öà SweetAlert£º

npm install sweetalert2

µÇ¼ºó¸´ÖÆ

È»ºóÔÚ app.blade.php ÖÐÌí¼Ó SweetAlert CSS ºÍ JS Îļþ£º

<!DOCTYPE html>
<html>
    <head>
        <title>Laravel</title>
        <link rel="stylesheet" type="text/css" href="{{ asset('css/app.css') }}">
        <link rel="stylesheet" type="text/css" href="https://cdn.jsdelivr.net/npm/sweetalert2@10.16.0/dist/sweetalert2.min.css">
        <script src="{{ asset('js/app.js') }}"></script>
        <script src="https://cdn.jsdelivr.net/npm/sweetalert2@10.16.0/dist/sweetalert2.min.js"></script>
    </head>
    <body>
        @yield('content')
    </body>
</html>

µÇ¼ºó¸´ÖÆ

È»ºóÔÚ blade Ä£°åÖÐÌí¼ÓÒÔÏ´úÂ룺

<form method="post" action="{{ route('posts.destroy', $post->id) }}" style="display: inline-block;">
    @csrf
    @method('DELETE')
    <button type="submit" class="btn btn-danger btn-sm"
        onclick="event.preventDefault();
        Swal.fire({
            title: 'È·¶¨É¾³ýÂð£¿',
            icon: 'warning',
            showCancelButton: true,
            confirmButtonText: 'È·ÈÑþ³Øý',
            cancelButtonText: '×÷·Ï'
        }).then((result) => {
            if (result.value) {
                this.parentElement.submit();
            }
        });"
    >ɾ³ý</button>
</form>

µÇ¼ºó¸´ÖÆ

ÔÚÆäÖÐ £¬ÎÒÃǽç˵ÁËÒ»¸öµã»÷ɾ³ý°´Å¥µÄÊÂÎñ £¬Ëü»áÔÚÓû§µã»÷ɾ³ý°´Å¥Ê±µ¯³öÒ»¸öÈ·Èϵ¯´°¡£ÈôÊÇÓû§µã»÷È·ÈÏ°´Å¥ £¬JavasScript »áÌá½»±íµ¥²¢É¾³ýÏìÓ¦¼Í¼¡£

ÐèҪעÖصÄÊÇ £¬ÔÚɾ³ý²Ù×÷µÄ form ±íµ¥ÖÐ £¬ÎÒÃǼÓÉÏÁË @csrf ºÍ @method(‘DELETE’) £¬ÕâÊÇÓÉÓÚ Laravel ÖеÄɾ³ý²Ù×÷ÐèҪͨ¹ý HTTP DELETE ÒªÁìÀ´Ìá½» £¬¶øä¯ÀÀÆ÷Ö»Ö§³Ö GET ºÍ POST ÒªÁì £¬Òò´ËÐèҪʹÓà hidden input À´Ö¸¶¨ÇëÇóÒªÁì¡£

³ý·ÇÐëÒª £¬ÎÒÃÇÓ¦¸ÃÖ»¹Ü×èֹʹÓÃÅúÁ¿É¾³ý²Ù×÷ £¬ÓÉÓÚËü¿ÉÄܻᵼÖÂÊý¾ÝµÄ²»¿ÉÍì»ØµÄɥʧ¡£ÈôÊÇÐèҪɾ³ýµ¥¸ö¼Í¼ £¬¿ÉÒÔʹÓà Eloquent ÌṩµÄ delete ÒªÁì £¬Ëü»áÔÚɾ³ý¼Í¼ǰѯÎÊÓû§ÊÇ·ñÈ·ÈÑþ³Øý¡£ÈôÊÇÐèҪɾ³ý¶à¸ö¼Í¼ £¬¿ÉÒÔʹÓà whereIn ÒªÁì £¬µ«Òª×¢ÖØʹÓÃÇ°Ó¦×ÐϸȷÈÏ¡£

ÒÔÉϾÍÊÇlaravel5.2ÅúÁ¿É¾³ýµÄÏêϸÄÚÈÝ £¬¸ü¶àÇë¹Ø×¢±¾ÍøÄÚÆäËüÏà¹ØÎÄÕ£¡

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

Ïà¹ØÐÂÎÅ

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

18523999891

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

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

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