Laravel 5 Laravel Virgin:设置工厂顺序以确保已创建指定的数据集
在我的应用程序中,我有以下迁移:Laravel 5 Laravel Virgin:设置工厂顺序以确保已创建指定的数据集,laravel-5,eloquent,factory,Laravel 5,Eloquent,Factory,在我的应用程序中,我有以下迁移: <?php use Illuminate\Support\Facades\Schema; use Illuminate\Database\Schema\Blueprint; use Illuminate\Database\Migrations\Migration; class CreateGridTable extends Migration { /** * Run the migrations. * * @ret
<?php
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreateGridTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('grid', function (Blueprint $table) {
$table->bigIncrements('id');
$table->unsignedInteger('width');
$table->unsignedInteger('height');
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('grid');
}
}
显式创建表格网格和表。我想通过工厂填充数据:
/** @var \Illuminate\Database\Eloquent\Factory $factory */
use App\Model\Grid;
use App\Model\Rover;
use App\Constants\RoverConstants;
use Faker\Generator as Faker;
/**
* Random Command Generator based upon:
* https://stackoverflow.com/a/13212994/4706711
* @param integer $length How many characters the wommand will contain.
* @return string
*/
function generateRandomCommand($length = 10): string {
return substr(str_shuffle(str_repeat($x=implode('',RoverConstants::AVAILABLE_COMMANDS), ceil($length/strlen($x)) )),1,$length);
}
$factory->define(Grid::class,function(Faker $faker){
return [
'width'=>rand(1,10),
'height'=>rand(1,10)
];
});
$factory->define(Rover::class, function(Faker $faker) {
$command = generateRandomCommand(rand(0));
$commandLength = strlen($command);
$commandPos = rand(0,$commandLength);
$lastExecutedCommand = substr($command,$commandPos,$commandPos);
$randomGrid=Grid::inRandomOrder();
return [
'grid_id' => $randomGrid->value('id'),
'grid_pos_x' => rand(0,$randomGrid->value('width')),
'grid_pos_y' => rand(0,$randomGrid->value('height')),
'rotation' => RoverConstants::ORIENTATION_EAST,
'command' => $command,
'last_commandPos' => $commandPos,
'last_command' => $lastExecutedCommand,
];
});
但是我如何确保$randomGrid=Grid::inRandomOrder;将始终返回一个网格?换句话说,我想检查的是没有网格,然后打电话给网格工厂,从罗孚工厂制造一个网格
你知道我怎么做吗?回答不够,这里仅供参考
如果您想确保始终存在网格,可以在该位置创建一个称为GridFactory的网格。如果要使用现有栅格,可以替代此属性。像这样:
$factory->define(Rover::class, function(Faker $faker) {
$command = generateRandomCommand(rand(0));
$commandLength = strlen($command);
$commandPos = rand(0,$commandLength);
$lastExecutedCommand = substr($command,$commandPos,$commandPos);
//This will create a second entry in the database.
$randomGrid = factory(Grid::class)->create();
return [
'grid_id' => $randomGrid->id, //no need for the value() method, they are all attributes
'grid_pos_x' => rand(0,$randomGrid->width),
'grid_pos_y' => rand(0,$randomGrid->height),
'rotation' => RoverConstants::ORIENTATION_EAST,
'command' => $command,
'last_commandPos' => $commandPos,
'last_command' => $lastExecutedCommand,
];
});
上面的factory显示了在调用RoverFactory时动态创建的网格。使用factoryRover::class->create创建一个新的漫游者也将创建一个新的网格并保存它。现在,如果要使用现有网格,可以执行以下操作:
factory(Rover::class)->create([
'grid_id' => $existingGrid->id,
'grid_pos_x' => rand(0, $existingGrid->width),
'grid_pos_y' => rand(0, $existingGrid->height),
]);
这将创建一个带有现有网格的漫游者,您以前可能已经使用GridFactory或其他工具创建了该网格。希望这对你有用。享受拉威尔
编辑:
通常你会这样做:
$factory->define(Rover::class, function(Faker $faker) {
$command = generateRandomCommand(rand(0));
$commandLength = strlen($command);
$commandPos = rand(0,$commandLength);
$lastExecutedCommand = substr($command,$commandPos,$commandPos);
return [
'grid_id' => function() {
return factory(Grid::class)->create()->id;
}
'grid_pos_x' => '', //But then i got nothing for this.
'grid_pos_y' => '', //But then i also got nothing for this.
'rotation' => RoverConstants::ORIENTATION_EAST,
'command' => $command,
'last_commandPos' => $commandPos,
'last_command' => $lastExecutedCommand,
];
});
我建议你总是做一个新的网格。如果要使用现有网格,请在生成漫游者时执行此操作,但不在工厂中,例如factoryRover::class->make['grid_id'=>x],但在这种方法中,我的数据库中不是有两个网格吗?在这种情况下,如何在工厂中“禁用”网格的创建?我能把埃克特拉旗和伞送到工厂吗?你说得对。我将编辑我的答案。目前我想不出一个适当的办法来解决你的问题。打扰一下你可能想拒绝我的回答,因为它不能充分解决你的问题。除了覆盖要创建的数量和要覆盖的属性之外,我不知道如何向工厂传递额外的参数和/或标志。也许可以查看Laravel文档。此外,您还可以传递一个回调函数,返回网格id,如中所示。所以如果是ovveride,它就不会被调用。这就是我在我的答案编辑部分添加的内容。在那里,我使用了传递回调函数的正确方法。但是我不知道如何传递grid_pos_x和grid_pos_y变量。如果我让闭包访问闭包范围之外的一些变量,以便临时存储生成的网格,该怎么办?
$factory->define(Rover::class, function(Faker $faker) {
$command = generateRandomCommand(rand(0));
$commandLength = strlen($command);
$commandPos = rand(0,$commandLength);
$lastExecutedCommand = substr($command,$commandPos,$commandPos);
return [
'grid_id' => function() {
return factory(Grid::class)->create()->id;
}
'grid_pos_x' => '', //But then i got nothing for this.
'grid_pos_y' => '', //But then i also got nothing for this.
'rotation' => RoverConstants::ORIENTATION_EAST,
'command' => $command,
'last_commandPos' => $commandPos,
'last_command' => $lastExecutedCommand,
];
});