Laravel中定义复合主键

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()
    {
        return false;
    }

    /**
     * Set the keys for a save update query.
     *
     * @param  \Illuminate\Database\Eloquent\Builder $query
     * @return \Illuminate\Database\Eloquent\Builder
     */
    protected function setKeysForSaveQuery(Builder $query)
    {
        foreach ($this->getKeyName() as $key) {
            if ($this->$key)
                $query->where($key, '=', $this->$key);
            else
                throw new Exception(__METHOD__ . 'Missing part of the primary key: ' . $key);
        }

        return $query;
    }
}

在model中使用:

<?php
    namespace App;

    use Illuminate\Database\Eloquent\Model;

    class Goods extends Model
    {
        use \App\Traits\HasCompositePrimaryKey;

        protected $primaryKey = ['param1', 'param2']; //设置组合主键

        // coding

    }

这样Eloquent ORM的save()方法就可以使用了。

posted @ 2017-12-13 10:32  wjd0215  阅读(1391)  评论(0编辑  收藏  举报