laravel默认主键是id,但有的时候我们建表时可能会需要用到复合主键,那么laravel中使用Eloquent Medel如何定义复合主键呢?直接上代码。
首先在app目录先创建文件 Traits/HasCompositePrimaryKey 内容如下:
// Adjust this to match your model namespace! namespace App\Traits; use Illuminate\Database\Eloquent\Builder; trait HasCompositePrimaryKey { /** * Get the value indicating whether the IDs are incrementing. * * @return bool */ public function getIncrementing() {