53

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 model under app folder, and also a migration in database/migrations

But what I can't find is how to delete a model...

jh314
  • 27,144
  • 16
  • 62
  • 82
Jonathan Solorzano
  • 6,812
  • 20
  • 70
  • 131
  • You'll have to delete the model manually. – Robin May 28 '15 at 21:20
  • So i just have to remove the model file under app folder and the migration @RobinR? – Jonathan Solorzano May 28 '15 at 21:21
  • Yes if you want to remove the migration file. Else if you want to do an rollback of the migration you'll need to execute an rollback. – Robin May 28 '15 at 21:22
  • Hmmm ok, it would be useful to have a command to do that... Thanks @RobinR, another question, if i want to add or delete a column of a table, do i have create a new migration? – Jonathan Solorzano May 28 '15 at 21:26
  • I know this is not an answer . But check your IDE if it has save delete option .. i use phpstorm and when i delete a model file manualy , it's find all model usage and ask you if i am sure to delete it – Omar May 28 '15 at 21:26
  • I use phpstorm too @Opetmar, and yeah its a great tool the refactor thing – Jonathan Solorzano May 28 '15 at 21:28
  • I'm sorry. I thought you where using the migrate function for the database. You'll have to delete your models manually. If you want to delete them tho. – Robin May 28 '15 at 21:31
  • Then you can use `php artisan migrate:rollback` – Robin May 28 '15 at 22:49
  • 1
    Well, the thing is that i want to delete a model that was created before other three ones. Can i specify wich model to rollback @RobinR? – Jonathan Solorzano May 28 '15 at 22:51
  • A model is just a PHP file. Just delete it if you don’t need it any more. – Martin Bean Feb 03 '21 at 01:31

8 Answers8

57

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.

hfingler
  • 1,931
  • 4
  • 29
  • 36
  • Thanx, in my case the app is still in dev. So the steps would be the same as in production, or do i just delete them? – Jonathan Solorzano May 30 '15 at 04:28
  • If you are in early dev, meaning your DB might still change, rollback until the migration is undone, remove the migration file, `dumpautoload`, and migrate again. I'd also recommend you set up some seeders to make testing easier then `php artisan migrate:refresh --seed`. I'll add the refresh solution to the answer. – hfingler May 30 '15 at 05:03
  • 1
    Deleting the `App/model` class, deleting the migration file and manually removing the entry from autoload_classmap.php, will these steps work? Or should I use `composer dumpautoload` at any cost? – BraveNinja Oct 24 '16 at 18:53
9

No command, just do it manually and its safe

  1. Delete the model first (if you don't) need the model any longer
  2. Delete the migration from ...database/migrations folder
  3. If you have already migrated i.e if you have already run php artisan migrate, log into your phpmyadmin or SQL(whichever the case is) and in your database, delete the table created by the migration
  4. Still within your database, in the migrations table, locate the row with that migration file name and delete the row.

Works for me, hope it helps!

user2806026
  • 787
  • 3
  • 10
  • 24
DAVID AJAYI
  • 1,912
  • 20
  • 13
  • if you're using phpstorm IDE, first do the step 1, 2, and 3 (or instead of step 3, run `php artisan migrate:refresh`) and at the end, press Shift twice to open the search window and search for the model you just deleted, and it shows you if anything left! – mjavadtatari Aug 04 '22 at 18:37
0

search in vendor/composer/autoload_classmap.php Ctrl+F write modelname delete allow edit this folder and delete model path

0

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.

Riyaz
  • 19
  • 1
  • 8
0

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

Vipertecpro
  • 3,056
  • 2
  • 24
  • 43
0

There is no any artisan command do it.You want to do it manually.

  1. You want to delete your model from the Models directory
    Path : app\Models\yourmodel.php

  2. In the next step you want to delete your migration file from migration folder
    Path : database\migrations\yourmigrationfile.php

Note: Already, If you have migrated, you should want to delete table from your database.you can log into your phpmyadmin panel and you can to do it.

-1

You can delete model in App folder if you see this error (Model Already Exists!)

enter image description here

Arun A S
  • 6,421
  • 4
  • 29
  • 43
  • From Review: Hi, this post does not seem to provide an [answer](https://stackoverflow.com/help/how-to-answer) to the question; it is better suited as a comment. – sɐunıɔןɐqɐp Oct 10 '19 at 13:45
-1

I had this problem. with add table name in model file, my problem was solved.

class Company extends Model
{
    
    public $table = 'table_name';
 
}
Jeff Schaller
  • 2,352
  • 5
  • 23
  • 38