jeudi 4 février 2016

Laravel 5.2 - How to reference Composite Key in Models?

I have a migration table like this-

<?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');
    }
}

And I a model for this file like this-

<?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'
                            ];
}

But My question is what will be my primary key for model here?

So-

protected $primaryKey = ????;

Can anyone help me please?



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire