我想在使用它时播种数据库
public function run()
{
$users = factory(app\User::class, 3)->create();
}
Run Code Online (Sandbox Code Playgroud)
在数据库中添加三个用户,但是当我使用它时
public function run()
{
$Comment= factory(app\Comment::class, 3)->create();
}
Run Code Online (Sandbox Code Playgroud)
告诉我错误
[InvalidArgumentException]
无法找到名称为[default] [app\Comment]的工厂.
Laravel的播种机在我的模型上运行各种模型事件,从Product::saved()模型事件中触发新订单通知电子邮件等.
这显着减慢了数据库播种速度.是否可以检测种子是否正在运行,如果是,请告诉Laravel不要运行模型事件?
我正在尝试运行迁移(见下文)并为数据库播种,但是当我运行时
php artisan migrate --seed
Run Code Online (Sandbox Code Playgroud)
我收到此错误:
Migration table created successfully.
Migrated: 2015_06_17_100000_create_users_table
Migrated: 2015_06_17_200000_create_password_resets_table
Migrated: 2015_06_17_300000_create_vehicles_table
[Illuminate\Database\QueryException]
SQLSTATE[42000]: Syntax error or access violation: 1701 Cannot truncate a table
referenced in a foreign key constraint (`app`.`vehicles`, CONSTRAINT `vehic
les_user_id_foreign` FOREIGN KEY (`user_id`) REFERENCES `app`.`users` (`id`
)) (SQL: truncate `users`)
[PDOException]
SQLSTATE[42000]: Syntax error or access violation: 1701 Cannot truncate a table
referenced in a foreign key constraint (`app`.`vehicles`, CONSTRAINT `vehic
les_user_id_foreign` FOREIGN KEY (`user_id`) REFERENCES `app`.`users` (`id`
))
Run Code Online (Sandbox Code Playgroud)
我查看了这个错误应该是什么意思,并且还找到了遇到同样问题的其他人的 …
我按照书本做了一切:
安装了新的Laravel 5.3.9应用程序(我所有的非新鲜应用程序产生相同的错误)
跑 php artisan make:auth
为新表创建迁移`php artisan make:migration create_quotations_table --create = quotations
Schema::create('quotations', function (Blueprint $table) {
$table->increments('id');
$table->string('text');
// my problem persists even with the below two columns commented out
$table->integer('creator_id')->unsigned()->index('creator_id');
$table->integer('updater_id')->unsigned()->index('updater_id');
$table->softDeletes();
$table->timestamps();
});
Run Code Online (Sandbox Code Playgroud)然后我跑了 php artisan migrate
然后我定义了一个新的种子 php artisan make:seeder QuotationsTableSeeder
添加简单插入后,文件的完整内容:
<?php
use Illuminate\Database\Seeder;
class QuotationsTableSeeder extends Seeder
{
/**
* Run the database seeds.
*
* @return void
*/
public function run()
{
DB::table('quotations')->insert([
'text' => str_random(10),
]);
}
}
Run Code Online (Sandbox Code Playgroud)
php …我正在使用Laravel(4.2)
我正在开发一个带有身份验证系统的项目.我需要将第一个用户插入到我的users表中.我想直接使用sql命令(插入用户....).因为无法使用传统的laravel方法创建第一个用户.
在插入表格后,第一个用户将使用auth :: attempts方法进行识别.
如何将此用户插入mysql表?
就像是?
insert into users (login, password) values ('admin', 'crypted password which can be later identified with laravel')
Run Code Online (Sandbox Code Playgroud) 我有一个users表和一个与businesses表一对一/一的关系(users.user_id => businesses.user_id).在我的users桌子上,我有一个鉴别器,它告诉我用户是否属于商业类型,因此我也需要在businesses表格上有详细信息.
我想用我目前正在工作的工厂创建我的用户,然后只创建业务详细信息,其中鉴别器指向企业帐户.
我脑子里有三个选择:
user_id用户分配给业务工厂. user_id已经创建的用户与业务工厂user_id. users.user_id和business.user_id.但是我使用的是随机生成器,user.user_type即使我businesses填充了表,也可能是那些将鉴别器设置为"个人"的用户.还有另外一种方法吗?我可以将Seeder的论据传递给工厂吗?
Laravel的文档建议使用DatabaseMigrations特征在测试之间迁移和回滚数据库.
use Illuminate\Foundation\Testing\DatabaseMigrations;
class ExampleTest extends TestCase
{
use DatabaseMigrations;
/**
* A basic functional test example.
*
* @return void
*/
public function testBasicExample()
{
$response = $this->get('/');
// ...
}
}
Run Code Online (Sandbox Code Playgroud)
但是,我有一些种子数据,我想用我的测试.如果我跑:
php artisan migrate --seed
Run Code Online (Sandbox Code Playgroud)
然后它适用于第一次测试,但它无法进行后续测试.这是因为特征回滚了迁移,当它再次运行迁移时,它不会为数据库设定种子.如何通过迁移运行数据库种子?
我的用户模型有一个观察者。在我的观察者->创建的事件中我有一些代码。
public function created(User $user)
{
sendEmail();
}
Run Code Online (Sandbox Code Playgroud)
因此,我们的想法是,当创建用户时,系统将向用户发送帐户已创建的电子邮件通知。
问题:当数据库进行播种时,它还会调用此方法“已创建”并向用户(位于种子中)发送电子邮件通知。所以,我的问题是,我如何检查,可能在这个“创建”方法中,目前 Laravel 正在播种数据 -> 不发送电子邮件或不运行“创建”观察者方法。
尝试谷歌,找到了一些东西,但工作不正确。就像是YourModel::flushEventListeners();
我有一张users表和一张roles表有多对多关系的表.这两个表连接到一个名为的联结表role_user.
以下是我的Laravel项目中的模型:
用户
namespace App;
use Illuminate\Database\Eloquent\Model;
class Role extends Model
{
public function roles()
{
return $this->belongsToMany('App\Role');
}
}
Run Code Online (Sandbox Code Playgroud)
角色
namespace App;
use Illuminate\Database\Eloquent\Model;
class Role extends Model
{
public function users()
{
return $this->belongsToMany('App\User');
}
}
Run Code Online (Sandbox Code Playgroud)
以下是Laravel项目中的Factory文件:
$factory->define(App\User::class, function (Faker\Generator $faker) {
return [
'name' => $faker->name,
'email' => $faker->unique()->safeEmail,
'password' => $password ?: $password = bcrypt('secret'),
];
});
$factory->define(App\Role::class, function (Faker\Generator $faker) {
return [
'role' => $faker->realText($maxNbChars …Run Code Online (Sandbox Code Playgroud) 我试图在一些PHPUnit测试用例中的每次测试之前重新创建数据库.我正在使用Laravel 5.3.这是TestCase:
class CourseTypesTest extends TestCase
{
public function setUp()
{
parent::setUp();
Artisan::call('migrate');
Artisan::call('db:seed', ['--class' => 'TestDatabaseSeeder ', '--database' => 'testing']);
}
/**
* A basic functional test example.
*
* @return void
*/
public function test_list_course_types()
{
$httpRequest = $this->json('GET', '/api/course-types');
$httpRequest->assertResponseOk();
$httpRequest->seeJson();
}
public function tearDown()
{
Artisan::call('migrate:reset');
parent::tearDown();
}
}
Run Code Online (Sandbox Code Playgroud)
运行phpunit失败并显示错误:
由Sebastian Bergmann和贡献者提供的$ phpunit PHPUnit 5.7.5.
E 1/1(100%)
时间:2.19秒,内存:12.00MB
有1个错误:
1)CourseTypesTest :: test_list_course_types ReflectionException:类TestDatabaseSeeder不存在
D:\ www\learn-laravel\my-folder-api\vendor\laravel\framework\src\Illuminate\Container\Container.php:749 D:\ www\learn-laravel\my-folder-api\vendor\laravel\framework\src\Illuminate\Container\Container.php:644 D:\ www\learn-laravel\my-folder-api\vendor\laravel\framework\src\Illuminate\Foundation\Application.php:709 D:\ www\learn-laravel\my-folder-api\vendor\laravel\framework\src\Illuminate\Database\Console\Seeds\SeedCommand.php:74 D:\ www\learn-laravel\my-folder-api\vendor\laravel\framework\src\Illuminate\Database\Console\Seeds\SeedCommand.php:63 D:\ …
laravel ×10
laravel-seeding ×10
laravel-5 ×5
php ×5
factory ×2
mysql ×2
database ×1
faker ×1
laravel-4 ×1
laravel-5.2 ×1
laravel-5.3 ×1
observers ×1
phpunit ×1
seeding ×1
testing ×1