如何使用php artisan删除模型? [英] How to delete a model using php artisan?

查看:342
本文介绍了如何使用php artisan删除模型?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Laravel 5中是否存在用于安全删除模型的命令?要创建模型,我们使用

php artisan make:model modelname

这将在app文件夹下创建一个模型,并在database/migrations

中进行迁移

但是我找不到如何删除模型...

解决方案

删除模型:只需删除App/或任何其他文件夹下的模型.

删除迁移:如果已迁移(意味着数据库已更改),则有两种选择:

项目开始"/难看的方式是到migrate:rollback,直到撤消迁移为止(如果这是您进行的最后一次迁移,则回滚就足够了,否则,您将不得不回滚几次) ),然后删除迁移文件(位于database/migrations文件夹中的文件.这里很重要:迁移类仍将由composer自动加载.因此,您必须从vendor/composer/autoload_classmap.php中删除迁移类加载.也许composer dumpautoload可以工作,但这对我来说不是.如果数据库中没有重要数据并且可以擦除它,请删除迁移文件,composer dumpautoload,然后运行php artisan migrate:refresh.这将回滚所有迁移,然后将所有内容迁移回. /p>

这是在生产中,我搞砸了"的方式:创建另一个迁移,其中up​​方法删除第一个迁移的表,down方法创建它(基本上是第一个迁移的up方法).将两个迁移文件留在那里,不要删除它们.

如果尚未迁移,则只需删除迁移文件composer dumpautoload,如果出现某些class/file not found错误,请检查vendor/composer/autoload_classmap.php是否具有刚删除的文件的类,然后在其中删除行.

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...

解决方案

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.

这篇关于如何使用php artisan删除模型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆