Idgenerator laravel 8,如何修复异常 "table field type is bigint but prefix is string"
Idgenerator laravel 8, how to fix Exception "table field type is bigint but prefix is string"
为什么总是出现这条信息?
即使我想制作一个带有 2 个点的 id = 190772.2021.00000001
无法添加“.” (点)那样只能加1个点 1907722021.00000001
factory.php
namespace Database\Factories;
use App\Models\Anggota;
use Illuminate\Database\Eloquent\Factories\Factory;
use App\Models\User;
use Haruncpi\LaravelIdGenerator\IdGenerator;
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 Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
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”的类型为“字符串”,但出现“table 字段类型为 bigint 但前缀为字符串”
如何求解使得结果=190772.2021.00000001
帮帮我,谢谢
laravel ID generator package by default use table's id
column. According to your need, the documentation says (example 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,
];
为什么总是出现这条信息? 即使我想制作一个带有 2 个点的 id = 190772.2021.00000001 无法添加“.” (点)那样只能加1个点 1907722021.00000001 factory.php
namespace Database\Factories;
use App\Models\Anggota;
use Illuminate\Database\Eloquent\Factories\Factory;
use App\Models\User;
use Haruncpi\LaravelIdGenerator\IdGenerator;
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 Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
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”的类型为“字符串”,但出现“table 字段类型为 bigint 但前缀为字符串”
如何求解使得结果=190772.2021.00000001
帮帮我,谢谢
laravel ID generator package by default use table's id
column. According to your need, the documentation says (example 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,
];