dyrynda / eloquent-composite-primary-keys
为 Eloquent 模型提供复合主键支持
2.0.0
2019-09-28 04:32 UTC
Requires
- illuminate/database: ^5.5 || ^6.0
This package is auto-updated.
Last update: 2024-08-28 16:05:44 UTC
README
一个用于在您的 Eloquent 模型中实现以支持复合主键的单个特质。Laravel Schema 构建器支持创建复合主键,但 Eloquent 模型不支持。
此包主要受到 Stack Exchange 上建议的代码(https://stackoverflow.com/a/36995763/526501)的启发,并且也作为一个包含更多功能的包发布。我需要一个仅为此目的的单独包。
由于我们需要继续使用它(Laravel 6.0+),因此此包是从 Coen Jacobs 复制的,但他们已经归档了项目。
安装
通过 Composer 安装此包
composer require dyrynda/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 Dyrynda\EloquentCompositePrimaryKeys\HasCompositePrimaryKey; class Product extends Model { use HasCompositePrimaryKey;
接下来,您将 Eloquent 模型上的 $primaryKey
属性设置为包含形成复合主键的字段名的数组
protected $primaryKey = array('first_key', 'another_key');