如何使用 php artisan 删除模型?
How to delete a model using php artisan?
在Laravel 5中是否有安全删除模型的命令?我们使用
创建模型
php artisan make:model modelname
这将在 app
文件夹下创建一个模型,并在 database/migrations
中进行迁移
但是我找不到的是如何删除模型...
删除模型:只需删除 App/
或任何其他文件夹下的模型。
删除迁移:如果你已经迁移了它(意味着数据库发生了变化)你有两个选择:
"project starting"/丑陋的方法是 migrate:rollback
直到迁移被撤消(如果这是你做的最后一次迁移,一次回滚就足够了,如果没有,你将不得不回滚几次)然后删除迁移文件(database/migrations
文件夹中的文件。这里重要的是:迁移的 class 仍将由 composer 自动加载。所以你必须删除迁移 class 从 vendor/composer/autoload_classmap.php
加载。也许 composer dumpautoload
会起作用,但它对我不起作用。如果数据库中没有重要数据并且可以擦除它,请删除迁移文件,composer dumpautoload
然后 运行 php artisan migrate:refresh
。这将回滚每次迁移,然后将所有内容迁移回来。
"this is in production and I messed up" 方法:创建另一个迁移,其中 up 方法删除第一个迁移的 table,down 正在创建它(基本上是第一个迁移的 up 方法)。把两个迁移文件放在那里,不要删除它们。
如果你还没有迁移它,就删除迁移文件,composer dumpautoload
如果你有一些class/file not found
错误,检查vendor/composer/autoload_classmap.php
是否有class删除您刚刚删除的文件并删除那里的行。
如果看到此错误,您可以删除 App 文件夹中的模型(模型已经存在!)
在 vendor/composer/autoload_classmap 中搜索。php
Ctrl+F 写入模型名称
删除允许编辑此文件夹并删除模型路径
当您的数据库名称与 .env
文件中定义的名称不同时,也会出现此问题。
DB_DATABASE=laravel
默认情况下,.env
中的数据库结构将数据库名称设置为 laravel。您可以将 laravel
替换为您的数据库名称。
这是我为我的项目创建的用于删除控制器和模型的内容
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.');
}
}
}
我希望这对某人有帮助
无指令,手动操作,安全
- 先删除模型(如果不需要)不再需要模型
- 从
...database/migrations
文件夹中删除迁移
- 如果您已经迁移,即如果您已经 运行
php artisan migrate
,请登录您的 phpmyadmin 或 SQL(无论哪种情况)并在您的数据库中删除 table 由迁移创建
- 仍在您的数据库中,在迁移 table 中,找到具有该迁移文件名的行并删除该行。
对我有用,希望对你有帮助!
没有任何 artisan 命令 it.You 想手动完成。
您想从模型目录中删除您的模型
路径:app\Models\yourmodel.php
下一步您要从迁移文件夹中删除迁移文件
路径:database\migrations\yourmigrationfile.php
注意:已经,如果您已经迁移,您应该希望从 database.you 中删除 table 可以登录到您的 phpmyadmin 面板并且您可以执行此操作。
在Laravel 5中是否有安全删除模型的命令?我们使用
创建模型php artisan make:model modelname
这将在 app
文件夹下创建一个模型,并在 database/migrations
但是我找不到的是如何删除模型...
删除模型:只需删除 App/
或任何其他文件夹下的模型。
删除迁移:如果你已经迁移了它(意味着数据库发生了变化)你有两个选择:
"project starting"/丑陋的方法是 migrate:rollback
直到迁移被撤消(如果这是你做的最后一次迁移,一次回滚就足够了,如果没有,你将不得不回滚几次)然后删除迁移文件(database/migrations
文件夹中的文件。这里重要的是:迁移的 class 仍将由 composer 自动加载。所以你必须删除迁移 class 从 vendor/composer/autoload_classmap.php
加载。也许 composer dumpautoload
会起作用,但它对我不起作用。如果数据库中没有重要数据并且可以擦除它,请删除迁移文件,composer dumpautoload
然后 运行 php artisan migrate:refresh
。这将回滚每次迁移,然后将所有内容迁移回来。
"this is in production and I messed up" 方法:创建另一个迁移,其中 up 方法删除第一个迁移的 table,down 正在创建它(基本上是第一个迁移的 up 方法)。把两个迁移文件放在那里,不要删除它们。
如果你还没有迁移它,就删除迁移文件,composer dumpautoload
如果你有一些class/file not found
错误,检查vendor/composer/autoload_classmap.php
是否有class删除您刚刚删除的文件并删除那里的行。
如果看到此错误,您可以删除 App 文件夹中的模型(模型已经存在!)
在 vendor/composer/autoload_classmap 中搜索。php Ctrl+F 写入模型名称 删除允许编辑此文件夹并删除模型路径
当您的数据库名称与 .env
文件中定义的名称不同时,也会出现此问题。
DB_DATABASE=laravel
默认情况下,.env
中的数据库结构将数据库名称设置为 laravel。您可以将 laravel
替换为您的数据库名称。
这是我为我的项目创建的用于删除控制器和模型的内容
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.');
}
}
}
我希望这对某人有帮助
无指令,手动操作,安全
- 先删除模型(如果不需要)不再需要模型
- 从
...database/migrations
文件夹中删除迁移 - 如果您已经迁移,即如果您已经 运行
php artisan migrate
,请登录您的 phpmyadmin 或 SQL(无论哪种情况)并在您的数据库中删除 table 由迁移创建 - 仍在您的数据库中,在迁移 table 中,找到具有该迁移文件名的行并删除该行。
对我有用,希望对你有帮助!
没有任何 artisan 命令 it.You 想手动完成。
您想从模型目录中删除您的模型
路径:app\Models\yourmodel.php下一步您要从迁移文件夹中删除迁移文件
路径:database\migrations\yourmigrationfile.php
注意:已经,如果您已经迁移,您应该希望从 database.you 中删除 table 可以登录到您的 phpmyadmin 面板并且您可以执行此操作。