Is there a command to safely delete a model in Laravel 5? To create a model we use
php artisan make:model modelname
And that will create a mode
search in vendor/composer/autoload_classmap.php Ctrl+F write modelname delete allow edit this folder and delete model path
Here is what I've created for my project to remove controller and model
app/Console/Commands/RemoveController.php
<?php
namespace App\Console\Commands;
use Illuminate\Console\Command;
class RemoveController extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'remove:controller {name}';
/**
* The console command description.
*
* @var string
*/
protected $description = 'Remove the controller class';
/**
* Create a new command instance.
*
* @return void
*/
public function __construct()
{
parent::__construct();
}
/**
* Execute the console command.
*
* @return mixed
*/
public function handle():void
{
$controllerName = $this->argument('name').'.php';
$controllerPath = base_path('app/Http/Controllers/').$controllerName;
if(file_exists($controllerPath)){
unlink($controllerPath);
$this->line('Controller removed successfully.');
}else{
$this->line('No controller found.');
}
}
}
app/Console/Commands/RemoveModel.php
<?php
namespace App\Console\Commands;
use Illuminate\Console\Command;
class RemoveModel extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'remove:model {name}';
/**
* The console command description.
*
* @var string
*/
protected $description = 'Remove the model class';
/**
* Create a new command instance.
*
* @return void
*/
public function __construct()
{
parent::__construct();
}
/**
* Execute the console command.
*
* @return mixed
*/
public function handle():void
{
$modelName = $this->argument('name').'.php';
$modelPath = base_path('app/').$modelName;
if(file_exists($modelPath)){
unlink($modelPath);
$this->line('Model removed successfully.');
}else{
$this->line('No controller found.');
}
}
}
I Hope this helps someone
You can delete model in App folder if you see this error (Model Already Exists!)
Deleting a model: just delete the model under App/
or whatever other folder.
Deleting a migration: if you have migrated it (meaning the database has suffered changes) you have two choices:
The "project starting"/ugly way is to migrate:rollback
until the migration is undone (if it was the last migration you did, one rollback is enough, if not, you're gonna have to rollback a couple of times) then delete the migration file (the one inside the database/migrations
folder. Important thing here: the migration's class will still be autoloader by composer. So you have to remove the migration class loading from vendor/composer/autoload_classmap.php
. Maybe composer dumpautoload
will work, it didn't for me though. If you have no important data in the DB and you can wipe it, delete the migration file, composer dumpautoload
then run php artisan migrate:refresh
. This will rollback every migration then migrate everything back in.
The "this is in production and I messed up" way: create another migration where the up method is dropping the first migration's table, down is creating it (basically the up method from the first migration). Leave the two migration files in there, don't remove them.
If you haven't migrated it, just delete the migration file, composer dumpautoload
and if you have some class/file not found
error, check if vendor/composer/autoload_classmap.php
has the class of the file you just removed and delete the row there.
No command, just do it manually and its safe
...database/migrations
folderphp artisan migrate
, log into your phpmyadmin or SQL(whichever the case is) and in your database, delete the table created by the migrationWorks for me, hope it helps!
The problem can also arise when your database name is different from the one defined in .env
file.
DB_DATABASE=laravel
By default, database structure in .env
sets database name as laravel. You can replace laravel
with the name of your database.