Quick command to reset your data with Laravel route

Với project XGallery thường xuyên phải reset data sau khi test một cái gì đó. Mà nhiều thứ phải làm quá … nên build nhanh 1 con clear:all

Artisan::command('clear:all', function () {
    array_map('unlink', array_filter((array) glob(storage_path('logs/*.log'))));
    $this->comment('Logs have been cleared!');
    array_map('unlink', array_filter((array) glob(storage_path('app/*.tmp'))));
    $this->comment('Tmp files have been cleared!');
    $tableNames = Schema::getConnection()->getDoctrineSchemaManager()->listTableNames();
    Schema::disableForeignKeyConstraints();
    foreach ($tableNames as $name) {
        //if you don't want to truncate migrations
        if ($name == 'migrations') {
            continue;
        }
        DB::table($name)->truncate();
        $this->comment('Table ' . $name . ' is truncated');
    }
    Schema::enableForeignKeyConstraints();

    Artisan::call('config:clear');
    Artisan::call('event:clear');
    Artisan::call('optimize:clear');
    Artisan::call('route:clear');
    Artisan::call('view:clear');
    $this->comment('Cleared all Laravel cache types. Except cache type');
})->describe('Clear log files & truncate tables');

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Blog at WordPress.com.

Up ↑

%d bloggers like this: