【问题标题】:How can I set Primary Key Value manually in Laravel如何在 Laravel 中手动设置主键值
【发布时间】:2019-02-01 07:04:55
【问题描述】:

我正在尝试在 Laravel 5.6 中手动设置主键值,但在保存记录时它返回 0。 这是我的控制器功能代码。

   $next = collect(DB::select('SELECT IFNULL(MAX(IFNULL(lvl1,0)),0)+10 AS NEXTVAL FROM coa_lvl1'))->first()->NEXTVAL;

    $lvl1_id = new CoaLvl1();
    $lvl1_id->lvl1 = $next;
    $this->lvl1->create(array_merge($request->all(),['lvl1' => $next , 'lvl1_code' => $request->get('group_id').'/'.$next]));

coa_lvl1 的迁移:

    Schema::create('coa_lvl1', function (Blueprint $table) {
        $table->tinyInteger('group_id');
        $table->integer('lvl1')->unsigned();
        $table->tinyInteger('lvl1_code');
        $table->string('lvl1_name',300);
        $table->integer('created_by')->nullable(false);
        $table->timestamp('created_date')->default(DB::raw('CURRENT_TIMESTAMP'));
        $table->integer('last_update_by');
        $table->timestamp('last_update_date')->default(DB::raw('0 ON UPDATE CURRENT_TIMESTAMP'));
        $table->string('active_flag',1)->default('1');
        $table->primary('lvl1');
        $table->foreign('group_id')
            ->references('group_id')
            ->on('coa_group')
            ->onDelete('cascade');
    });

在我的模型中

  protected $table = 'coa_lvl1';

 protected $primaryKey = 'lvl1';

public $incrementing = false;

protected $blameable  = array('created','updated');

protected $fillable = ['group_id','lvl1_code','lvl1_name','active_flag'];

  public $timestamps = false;

 public function coaGroup()
 {
   return $this->belongsTo(CoaGroup::class, 'group_id');
  }

  public function coaLvl1()
  {
   return $this->hasMany(CoaLvl2::class,'lvl1');
  }

$next 是我要设置的 PrimaryKey 值

【问题讨论】:

  • 您有迁移文件吗?一个表只能有一个主键,因此您可能在列之前的表中已经存在一个主键。
  • $table->tinyInteger('group_id'); $table->integer('lvl1')->unsigned(); $table->tinyInteger('lvl1_code'); $table->string('lvl1_name',300); $table->primary('lvl1');
  • 在我的模型中受保护 $primaryKey = 'lvl1';公共 $incrementing = false;
  • 请更新您的帖子并将所有内容包括在内。从 cmets 几乎看不到谢谢
  • 您声明lvl1 列两次,您只需要primary('lvl1')

标签: laravel


【解决方案1】:
Schema::create('coa_lvl1', function (Blueprint $table) {
    $table->tinyInteger('group_id');
    $table->integer('lvl1')->primary(); //$table->integer('lvl1')->unsigned();
    $table->tinyInteger('lvl1_code');
    $table->string('lvl1_name',300);
    $table->integer('created_by')->nullable(false);
    $table->timestamp('created_date')->default(DB::raw('CURRENT_TIMESTAMP'));
    $table->integer('last_update_by');
    $table->timestamp('last_update_date')->default(DB::raw('0 ON UPDATE CURRENT_TIMESTAMP'));
    $table->string('active_flag',1)->default('1');
    $table->foreign('group_id')
        ->references('group_id')
        ->on('coa_group')
        ->onDelete('cascade');
});

我的模型是这样的

protected $table = 'coa_lvl1';

protected $primaryKey = 'lvl1';

public $incrementing = false;

protected $blameable  = array('created','updated');

protected $fillable = ['group_id','lvl1_code','lvl1_name','active_flag'];

public $timestamps = false;

public function coaGroup()
{
    return $this->belongsTo(CoaGroup::class, 'group_id');
}

public function coaLvl1()
{
    return $this->hasMany(CoaLvl2::class,'lvl1');
}

【讨论】:

  • 还是一样的问题 它在 PrimaryKey 中保存了 0
  • @Almani 你能告诉你的模型你有保护 $fillable = ['col1','col2',...] 吗?
  • @Almani 我已经大致添加了受保护的 $fillable = [];再试一次看看是否有效
  • 我通过在受保护的 $fillable = ['lvl1','group_id','lvl1_code','lvl1_name','active_flag'] 中添加主键来完成此操作;我认为这不是正确的方法,但它对我有用
【解决方案2】:

我通过在 protected 中添加主键来做到这一点

   $fillable = ['lvl1','group_id','lvl1_code','lvl1_name','active_flag']; 

我认为这不是正确的方法,但它对我有用

【讨论】:

    猜你喜欢
    • 2021-09-26
    • 1970-01-01
    • 2021-10-31
    • 2022-01-12
    • 1970-01-01
    • 2015-01-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多