Last active
May 28, 2021 15:34
-
-
Save sauron/bcffa9f44eda6c414d71829c3d4d6b20 to your computer and use it in GitHub Desktop.
β’οΈ DANGEROUS command to delete already created twill crops. π
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace App\Console\Commands; | |
use Illuminate\Console\Command; | |
use Illuminate\Support\Facades\DB; | |
class DeleteCrops extends Command | |
{ | |
/** | |
* The name and signature of the console command. | |
* | |
* @var string | |
*/ | |
protected $signature = 'twill:delete-crops | |
{moduleName : The name of the module, or the FQCN of the model if you did not setup morphmaps} | |
{roleName : The name of the image role to delete} | |
{cropName : The name of the image crop to delete}' | |
; | |
// php artisan twill:delete-crops "App\Models\Destination" cover listing | |
/** | |
* The console command description. | |
* | |
* @var string | |
*/ | |
protected $description = 'Delete existing crops in database'; | |
/** | |
* Execute the console command. | |
* | |
* @return integer | |
*/ | |
public function handle(): int | |
{ | |
$this->info("Deleting crops"); | |
$arguments = $this->arguments(); | |
$model = $this->getModelClass($arguments['moduleName']); | |
$mediables = DB::table(config('twill.mediables_table')) | |
->where('mediable_type', $model) | |
->where('role', $arguments['roleName']) | |
->where('crop', $arguments['cropName']) | |
->get(); | |
$this->info("Found {$mediables->count()} mediables to delete."); | |
if ($mediables->count() === 0) { | |
$this->info("Aborted."); | |
return 1; | |
} | |
$this->info("Deleting mediables."); | |
$mediables->each(function ($crop) { | |
// Comment out the delete line for debugging purposes and enable the logging one | |
// $this->info("Media Id:".$crop->media_id); | |
DB::table(config('twill.mediables_table'))->where('id', $crop->id)->forceDelete(); | |
}); | |
return 0; | |
} | |
private function getModelClass($moduleName): string | |
{ | |
if (class_exists($moduleName)) { | |
return $moduleName; | |
} | |
$this->error('Providing module name is not yet supported'); | |
die; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment