coenjacobs / eloquent-composite-primary-keys
1.0.0
2017-10-23 09:10 UTC
Requires
- illuminate/database: ^5.5
This package is auto-updated.
Last update: 2019-09-18 20:16:38 UTC
README
一个简单的特质,用于在 Eloquent 模型中实现复合主键。Laravel Schema 构建器支持创建复合主键,但 Eloquent 模型不支持。
此包主要受 Stack Exchange 上建议的代码(链接)的启发,并已作为一个功能更全面的包发布。我需要一个仅为此目的的独立包。
重要提示:此项目现在是只读的
我决定将此项目设置为只读,不再进一步开发。在 Laravel 中这样做会有很多性能相关的问题,这让我不再觉得开发它有趣和有成就感。
如果您仍然想在 Laravel 中使用此功能,可以查看包含类似功能的 LaravelTreats 包。
安装
通过 Composer 安装此包
composer require coenjacobs/eloquent-composite-primary-keys
确保您有一个支持复合主键的数据库架构,例如通过迁移文件实现
Schema::create('products', function (Blueprint $table) { $table->integer('first_key'); $table->integer('another_key'); $table->primary(['first_key', 'another_key']); $table->timestamps(); });
在您希望具有复合主键的 Eloquent 模型上使用此特质
namespace App; use Illuminate\Database\Eloquent\Model; use CoenJacobs\EloquentCompositePrimaryKeys\HasCompositePrimaryKey; class Product extends Model { use HasCompositePrimaryKey;
接下来,您需要将 Eloquent 模型上的 $primaryKey
属性设置为包含组成复合主键的字段名称的数组
protected $primaryKey = array('first_key', 'another_key');