Laravel 5.2 - 如何在模型中引用复合键?

时间:2016-02-04 08:41:54

标签: php laravel laravel-5 laravel-5.1 laravel-routing

我有一个像这样的迁移表 -

<?php

use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class CreateFeatureProjectsTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('feature_projects', function (Blueprint $table)
        {
            $table->integer('project_id')       ->unsigned()  ->index();
            $table->integer('feature_id')       ->unsigned()  ->index();
            $table->timestamp('created_at');

            //Foreign Keys
            $table->foreign('project_id')       ->references('id')->on('projects');
            $table->foreign('feature_id')       ->references('id')->on('features');

            //Composite Primary Key
            $table->primary([
                                'project_id',
                                'feature_id'
                            ],'feature_projects_composite_key');
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::drop('feature_projects');
    }
}

我是这个文件的模型 -

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class FeatureProject extends Model
{
    protected $table        = 'feature_projects';   //Declare table name

    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable =   [
                                'project_id',
                                'feature_id'
                            ];
}

但我的问题是,这里的模型主要是什么?

所以 -

protected $primaryKey = ????;

有人可以帮我吗?

1 个答案:

答案 0 :(得分:0)

Google搜索laravel eloquent composite primary key says,您无法在Eloquent中使用复合键,至少不会没有令人厌烦且容易出错的一些Eloquent方法。