通过重写newFromBuilder方法并利用type字段,可在Laravel中模拟单表继承。1. 创建含type字段的vehicles表;2. 定义基类Vehicle,根据type返回对应子类实例;3. 子类Car和Motorcycle在creating时自动设置type值;4. 查询时Vehicle::find会自动返回具体子类对象,实现STI模式。

在 Laravel 中,Eloquent 并未原生支持单表继承(Single Table Inheritance, STI),但你可以通过一些技巧来模拟实现。STI 允许你用一张数据库表存储多个类的实例,通过一个字段(如 type)区分不同子类。
创建一个主表用于存储所有继承类的共用数据。例如,我们有一个 vehicles 表,包含汽车和摩托车:
Schema::create('vehicles', function (Blueprint $table) {
$table->id();
$table->string('type'); // 用于区分 Car 或 Motorcycle
$table->string('brand');
$table->string('model');
$table->integer('wheel_count');
$table->timestamps();
});
其中 type 字段将保存模型的实际类型。
创建一个基类模型 Vehicle,并设置 $guarded 或 $fillable 属性:
class Vehicle extends Model
{
protected $guarded = [];
// 指定使用 'type' 字段进行模型解析
public function newFromBuilder($attributes = [], $connection = null)
{
$instance = parent::newFromBuilder($attributes, $connection);
// 根据 type 字段返回具体子类实例
$type = $instance->getAttribute('type');
if ($type && $type !== static::class && is_subclass_of($type, self::class)) {
return new $type($instance->getAttributes());
}
return $instance;
}
}
定义具体的子类模型,如 Car 和 Motorcycle:
class Car extends Vehicle
{
protected static function boot()
{
parent::boot();
// 自动设置 type 值
static::creating(function ($model) {
$model->type = static::class;
});
}
}
class Motorcycle extends Vehicle
{
protected static function boot()
{
parent::boot();
static::creating(function ($model) {
$model->type = static::class;
});
}
}
现在你可以像使用普通模型一样操作:
// 创建记录 Car::create(['brand' => 'Toyota', 'model' => 'Camry', 'wheel_count' => 4]); Motorcycle::create(['brand' => 'Honda', 'model' => 'CBR', 'wheel_count' => 2]); // 查询时自动返回对应类实例 $vehicle = Vehicle::find(1); // 如果 type 是 Car,则返回 Car 实例 var_dump(get_class($vehicle)); // 输出: App\Models\Car
查询结果会根据 type 字段自动实例化为对应的子类。
基本上就这些。通过重写 newFromBuilder 方法,并结合 creating 事件自动设置类型,就能在 Laravel 中实现单表继承模式。虽然不是框架原生功能,但这种方式足够灵活且易于维护。
以上就是laravel如何实现模型的单表继承(STI)_Laravel模型单表继承实现方法的详细内容,更多请关注php中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号