默认情况下,laravel ID生成器包使用表的
为什么总是出现此消息?即使我想做一个有2个点的id,比如=190772.2021.00000001无法添加"。"(点(那样只能加1个点只有1907722021.0000001factory.php
namespace DatabaseFactories;
use AppModelsAnggota;
use IlluminateDatabaseEloquentFactoriesFactory;
use AppModelsUser;
use HaruncpiLaravelIdGeneratorIdGenerator;
class AnggotaFactory extends Factory
{
/**
* The name of the factory's corresponding model.
*
* @var string
*/
protected $model = Anggota::class;
/**
* Define the model's default state.
*
* @return array
*/
public function definition()
{
$tgl = $this->faker->date;
return [
'id_user' => User::factory(),
'no_anggota' => IdGenerator::generate([
'table' => 'anggotas',
'length' => 19,
'prefix' => date('dmy', strtotime($tgl)).date('.Y.')
]),
'nama_lengkap' => $this->faker->name,
'tempat_lahir' => $this->faker->cityPrefix,
'tanggal_lahir' => $tgl,
'nama_instansi' => $this->faker->word,
];
}
}
文件迁移
use IlluminateDatabaseMigrationsMigration;
use IlluminateDatabaseSchemaBlueprint;
use IlluminateSupportFacadesSchema;
class CreateAnggotasTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('anggotas', function (Blueprint $table) {
$table->id();
$table->unsignedBigInteger('id_user')->unique();
$table->string('no_anggota');
$table->string('nama_lengkap');
$table->string('tempat_lahir');
$table->date('tanggal_lahir');
$table->string('nama_instansi');
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('anggotas');
}
}
而列";no_anggota"在模式数据库中;字符串";但是出现";表字段类型为bigint,但前缀为字符串";
如何解决它,使结果=190772.2021.00000001
帮帮我,谢谢
id
列。根据您的需要,文档说(示例4(,您还必须通过field
。
您需要的输出:190772.2021.00000001(长度20,带两个点(
解决方案:只需通过'field' => 'no_anggota'
和'length' => 20
$tgl = $this->faker->date;
$idConfig = [
'table' => 'anggotas',
'field' => 'no_anggota',
'length' => 20,
'prefix' => date('dmy', strtotime($tgl)).date('.Y.')
];
return [
'id_user' => User::factory(),
'no_anggota' => IdGenerator::generate($idConfig),
'nama_lengkap' => $this->faker->name,
'tempat_lahir' => $this->faker->cityPrefix,
'tanggal_lahir' => $tgl,
'nama_instansi' => $this->faker->word,
];