使用字符串作为laravel模型的主键时需注意
PHP

使用字符串作为laravel模型的主键时需注意

quhe.net
2022-08-04 / 0 评论 / 37 阅读 / 正在检测是否收录...
当使用非自增int类型字段作为model的主键时,需要手动在模型里设置属性:
  • 主键字段名
  • 主键类型
  • 是否自增

代码示例

namespace App\Models;

use Dcat\Admin\Traits\HasDateTimeFormatter;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;

class ProductAttr extends Model
{
    use HasDateTimeFormatter;
    use SoftDeletes;

    //主键类型
    protected $keyType = 'string';
    //自定义主键字段
    protected $primaryKey = 'code';
    //是否自增
    public $incrementing = false;
    //表名
    protected $table = 'product_attrs';
0

评论 (0)

取消