codiliateur / laravel-model-extensions
Eloquent/Model 扩展
v1.0.1
2024-08-30 11:17 UTC
Requires
- php: ^8.0
- laravel/framework: ^11.0
Requires (Dev)
- orchestra/testbench: ^9.4
- phpstan/phpstan: ^1.12
- phpunit/phpunit: ^11.3
- squizlabs/php_codesniffer: ^3.10
This package is not auto-updated.
Last update: 2024-09-27 11:36:15 UTC
README
安装
使用 composer 安装包
composer require codiliateur/laravel-model-extensions
"组合" 主键
如果你的表有组合主键,你可以从 \Codiliateur\LaravelModelExtensions\Database\Eloquent\CompositeKeyModel
创建模型
例如
namespace App\Models\Bookings; use Codiliateur\LaravelModelExtensions\Database\Eloquent\CompositeKeyModel; class BoardingPass extends CompositeKeyModel { protected $primaryKey = [ 'ticket_no', 'flight_id', ]; }
要定义 组合主键,添加属性 $primaryKey
为键列的数组。添加定义 $autoincrementing = false
不需要。
操作组合键模型
现在,要使用 find()
查找任何模型,你必须指定一个组合键值作为参数(键列值的数组)而不是单个标量值。
BoardingPass::find(['0005435189117', 198393])
要使用 "find()" 或 "find Many()" 获取多个模型,指定组合键的数组
BoardingPass::find([["0005435189117", 198393], ["0005435189096", 198393]])
或者
BoardingPass::findMany([["0005435189117", 198393], ["0005435189096", 198393]])
要获取一个模型的组合键,使用 getKey()
$boardingPass = BoardingPass::find(['0005435189117', 198393]);
$boardingPass->getKey();
> ['0005435189117',198393]
模型的其他方法仍然按原样工作。