Laravel模型主键类型由$keyType属性决定,默认为int,可设为string等类型;若主键非自增需设置$incrementing = false;主键字段非id时需定义$primaryKey属性;Eloquent不直接支持复合主键,可通过唯一索引加查询方法模拟实现。

Laravel模型主键类型主要由
$keyType
int
string
$keyType
解决方案:
要修改Laravel模型的主键类型,你需要在模型类中设置
$keyType
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
class Product extends Model
{
/**
* 指示是否自动递增模型 ID。
*
* @var bool
*/
public $incrementing = false;
/**
* 模型的 "类型" 的自动递增 ID。
*
* @var string
*/
protected $keyType = 'string';
}上面的例子中,
$incrementing
false
$keyType
string
Product
模型主键自增ID如何修改?
如果你的主键是自增的,但不是默认的
int
bigint
$keyType
bigInteger
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
class CreateProductsTable extends Migration
{
/**
* 运行数据库迁移。
*
* @return void
*/
public function up()
{
Schema::create('products', function (Blueprint $table) {
$table->bigIncrements('id'); // 使用 bigIncrements
$table->string('name');
$table->timestamps();
});
}
/**
* 回滚数据库迁移。
*
* @return void
*/
public function down()
{
Schema::dropIfExists('products');
}
}注意,如果你已经有数据表,并且修改了主键类型,可能需要手动迁移数据,或者先清空表再执行迁移。
Laravel模型主键名称不是id怎么办?
Laravel默认主键字段是
id
id
product_id
$primaryKey
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
class Product extends Model
{
/**
* 与模型关联的主键。
*
* @var string
*/
protected $primaryKey = 'product_id';
}这样,Laravel在查询
Product
product_id
复合主键如何处理?
Laravel Eloquent ORM 本身并不直接支持复合主键。虽然可以绕过 ORM,直接使用 DB facade 进行操作,但这会失去 Eloquent 的便利性。
一个常见的替代方案是创建一个自增的
id
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
class CreateOrderItemsTable extends Migration
{
/**
* 运行数据库迁移。
*
* @return void
*/
public function up()
{
Schema::create('order_items', function (Blueprint $table) {
$table->id(); // 自增主键
$table->unsignedBigInteger('order_id');
$table->unsignedBigInteger('product_id');
$table->integer('quantity');
$table->timestamps();
$table->unique(['order_id', 'product_id']); // 复合唯一索引
});
}
/**
* 回滚数据库迁移。
*
* @return void
*/
public function down()
{
Schema::dropIfExists('order_items');
}
}然后在模型中,你可以定义一个方法来模拟复合主键的查询:
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
class OrderItem extends Model
{
/**
* 通过 order_id 和 product_id 查找记录。
*
* @param int $orderId
* @param int $productId
* @return \App\Models\OrderItem|null
*/
public static function findByCompositeKey($orderId, $productId)
{
return static::where('order_id', $orderId)
->where('product_id', $productId)
->first();
}
}这种方法虽然不是真正的复合主键,但通常可以满足大部分需求,并且可以继续使用 Eloquent 的其他功能。 当然,如果你的应用对复合主键有严格的要求,可能需要考虑使用更底层的数据库操作,或者寻找支持复合主键的第三方 Eloquent 扩展包。
以上就是Laravel模型键类型?主键类型怎样设置?的详细内容,更多请关注php中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号