如何解决如何使用播种机和控制器的目录生成“make:model -a”并迁移名称? 总计
如何生成“make:model -a”,其中包含种子和控制器的目录以及迁移名称?
Laravel 框架 8.44.0
我正在生成模型 PHP artisan make:model Blog/MyCategory -a
并希望看到以下结构:
Controllers/Blog/MyCategoryController.PHP
Models/Blog/MyCategory.PHP
factories/Blog/MyCategory.PHP
mifrations/2021_06_01_042639_create_blog_my_categories_table.PHP
seeders/Blog/MyCategorySeeder.PHP
执行命令PHP artisan make:model Blog/Category -a
Model created successfully.
Factory created successfully.
Created Migration: 2021_06_01_044253_create_my_categories_table
Seeder created successfully.
Controller created successfully.
但它创造了
Controllers/MyCategoryController.PHP (NO)
Models/Blog/MyCategory.PHP (YES)
factories/Blog/MyCategory.PHP (YES)
mifrations/2021_06_01_042639_create_my_categories_table.PHP (NO)
seeders/MyCategorySeeder.PHP (NO)
这样我就不能生成两个 MyCategory
。
执行命令PHP artisan make:model Shop/MyCategory -a
Model created successfully.
Factory created successfully.
invalidargumentexception
A CreateMyCategoriesTable class already exists.
at vendor/laravel/framework/src/Illuminate/Database/Migrations/MigrationCreator.PHP:102
- 模型
Shop/MyCategory.PHP
, - 工厂
Shop/MyCategoryFactory.PHP
- 迁移文件
2021_06_01_044253_create_my_categories_table.PHP
现在让我们创建正确的迁移文件
PHP artisan make:migration CreateBlogMyCategoryTable
再次执行命令PHP artisan make:model Shop/MyCategory -a
Model created successfully.
Factory created successfully.
Created Migration: 2021_06_01_050039_create_my_categories_table
Seeder already exists!
Controller already exists!
它再次创建了一个 2021_06_01_050039_create_my_categories_table
文件并且没有考虑到 Shop
目录中的模型
- 模型
Shop/MyCategory.PHP
- 工厂
Shop/MyCategoryFactory.PHP
- 迁移文件
2021_06_01_050039_create_my_categories_table.PHP
- 控制器
MyCategoryController.PHP
现在让我们创建正确的迁移和控制器:
PHP artisan make:migration CreateShopMyCategoryTable
PHP artisan make:controller Blog/MyCategoryController
PHP artisan make:controller Shop/MyCategoryController
总计
因此,我们看到“-а”选项不适合这种情况。您需要分别创建模型、控制器和迁移。
PHP artisan make:controller Blog/MyCategoryController -r
PHP artisan make:controller Shop/MyCategoryController -r
PHP artisan make:migration CreateBlogMyCategoryTable
PHP artisan make:migration CreateShopMyCategoryTable
工厂模型
PHP artisan make:model Blog/MyCategory -f
PHP artisan make:model Shop/MyCategory -f
此命令也使正确的 Factory
PHP artisan make:factory Blog\\MyCategoryFactory --model=Blog\\MyCategory
<?PHP
namespace Database\Factories\Blog;
use App\Models\Blog\MyCategory;
use Illuminate\Database\Eloquent\Factories\Factory;
class MyCategoryFactory extends Factory
{
/**
* The name of the factory's corresponding model.
*
* @var string
*/
protected $model = MyCategory::class;
// ....
}
我们需要的迁移文件、模型和控制器位于适当的目录中。
PHP artisan migrate
Migration table created successfully.
...
Migrating: 2021_06_01_055036_create_blog_my_category_table
Migrated: 2021_06_01_055036_create_blog_my_category_table (36.26ms)
Migrating: 2021_06_01_055541_create_shop_my_category_table
Migrated: 2021_06_01_055541_create_shop_my_category_table (39.16ms)
据我所知,所需的结构仍然必须通过单独的命令来完成。
但后来又出现了一个问题:我现在不明白怎么用factory()->create()
Route::get('/',function () {
\App\Models\Blog\MyCategory::factory(1)->create();
return view('welcome');
});
Illuminate\Database\QueryException
sqlSTATE[42S02]: Base table or view not found: 1146 Table 'larablog.my_categories' doesn't exist (sql: insert into `my_categories` (`updated_at`,`created_at`) values (2021-06-01 06:59:52,2021-06-01 06:59:52))
或修补匠
PHP artisan tinker
Psy Shell v0.10.8 (PHP 7.4.18 — cli) by Justin Hileman
>>> MyCategory::factory()->create()
PHP Error: Class 'MyCategory' not found in Psy Shell code on line 1
>>> Blog\MyCategory::factory()->create()
PHP Error: Class 'Blog\MyCategory' not found in Psy Shell code on line 1
>>> \Blog\MyCategory::factory()->create()
PHP Error: Class 'Blog\MyCategory' not found in Psy Shell code on line 1
>>> App\Models\Blog\MyCategory::factory()->create()
Illuminate\Database\QueryException with message 'sqlSTATE[42S02]: Base table or view not found: 1146 Table 'larablog.my_categories' doesn't exist (sql: insert into `my_categories` (`updated_at`,`created_at`) values (2021-06-01 06:48:26,2021-06-01 06:48:26))'
>>> \App\Models\Blog\MyCategory::factory()->create()
Illuminate\Database\QueryException with message 'sqlSTATE[42S02]: Base table or view not found: 1146 Table 'larablog.my_categories' doesn't exist (sql: insert into `my_categories` (`updated_at`,`created_at`) values (2021-06-01 06:48:29,2021-06-01 06:48:29))'
如何使这样的结构起作用?
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。