php 如何使用php artisan删除模型?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/30517098/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to delete a model using php artisan?
提问by Jonathan Solorzano
Is there a command to safely delete a model in Laravel 5? To create a model we use
Laravel 5 中是否有安全删除模型的命令?要创建我们使用的模型
php artisan make:model modelname
And that will create a model under app
folder, and also a migration in database/migrations
这将在app
文件夹下创建一个模型,并在database/migrations
But what I can't find is how to delete a model...
但我找不到的是如何删除模型...
回答by hfingler
Deleting a model: just delete the model under App/
or whatever other folder.
删除模型:只需删除App/
或任何其他文件夹下的模型。
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.
“项目启动”/丑陋的方法是migrate:rollback
直到迁移完成(如果这是您所做的最后一次迁移,则回滚一次就足够了,否则,您将不得不回滚几次)然后删除迁移文件(database/migrations
文件夹内的那个。重要的是:迁移的类仍将是作曲家的自动加载器。因此,您必须从 中删除迁移类加载vendor/composer/autoload_classmap.php
。也许composer dumpautoload
会起作用,但对我来说却没有。如果您没有重要数据在数据库中,您可以擦除它,删除迁移文件,composer dumpautoload
然后运行php artisan migrate:refresh
。这将回滚每个迁移,然后将所有内容迁移回。
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.
“这是在生产中,我搞砸了”的方式:创建另一个迁移,其中 up 方法删除第一个迁移的表,down 正在创建它(基本上是第一次迁移的 up 方法)。将两个迁移文件留在那里,不要删除它们。
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.
如果您还没有迁移它,只需删除迁移文件,composer dumpautoload
如果您有class/file not found
错误,请检查是否vendor/composer/autoload_classmap.php
有您刚刚删除的文件的类并删除那里的行。
回答by Saad Hassan
You can delete model in App folder if you see this error (Model Already Exists!)
如果您看到此错误,您可以删除 App 文件夹中的模型(模型已存在!)
回答by Anees Al-Ahsab
search in vendor/composer/autoload_classmap.php Ctrl+F write modelname delete allow edit this folder and delete model path
在 vendor/composer/autoload_classmap.php 中搜索 Ctrl+F write modelname delete 允许编辑此文件夹并删除模型路径
回答by Riyaz
The problem can also arise when your database name is different from the one defined in .env
file.
当您的数据库名称与.env
文件中定义的名称不同时,也会出现此问题。
DB_DATABASE=laravel
By default, database structure in .env
sets database name as laravel. You can replace laravel
with the name of your database.
默认情况下,数据库结构中的.env
数据库名称设置为 laravel。您可以替换laravel
为您的数据库的名称。
回答by Vipertecpro
Here is what I've created for my project to remove controller and model
这是我为我的项目创建的删除控制器和模型的内容
app/Console/Commands/RemoveController.php
应用程序/控制台/命令/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
应用程序/控制台/命令/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
我希望这可以帮助别人
回答by DAVID AJAYI
No command, just do it manually and its safe
无需命令,只需手动操作,安全无虞
- Delete the model first (if you don't) need the model any longer
- Delete the migration from
...database/migrations
folder - 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 - Still within your database, in the migrations folder, locate the row with that migration file name and delete the row.
- 先删除模型(如果你不需要)不再需要模型
- 从
...database/migrations
文件夹中删除迁移 - 如果您已经迁移,即如果您已经运行
php artisan migrate
,请登录您的 phpmyadmin 或 SQL(无论哪种情况)并在您的数据库中,删除迁移创建的表 - 仍在您的数据库中的迁移文件夹中,找到具有该迁移文件名的行并删除该行。
Works for me, hope it helps!
对我有用,希望能帮到你!