ogestor / eloquent-composite-primary-keys
在 Eloquent 模型上提供复合主键功能
6.0
2020-11-26 19:01 UTC
Requires
- illuminate/database: ~5.5|~5.6|~5.7|~5.8|~6.0
This package is auto-updated.
Last update: 2024-09-27 02:49:33 UTC
README
一个用于在您的 Eloquent 模型中实现以支持复合主键的单一特质。Laravel Schema 构建器支持创建复合主键,但 Eloquent 模型不支持。
此包主要受 Stack Exchange 上建议的代码(suggested code on Stack Exchange)的启发,并已作为具有更多功能的包发布。我需要一个单独的包来实现这个目的。
安装
通过 Composer 安装此包
composer require ogestor/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');