1

如何生成带有播种机和控制器目录的“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))'

如何使这样的结构工作?

4

1 回答 1

0

有时需要清除缓存,这个命令在我的项目中工作:

composer dump-autoload

祝你好运!

于 2021-08-26T13:27:45.753 回答