Laravel 5.2 - 如何在模型中引用复合键?
Laravel 5.2 - How to reference Composite Key in Models?
我有一个像这样的迁移table-
<?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 = ????;
有人能帮帮我吗?
谷歌搜索 laravel eloquent composite primary key
says,你不能在 Eloquent 中使用复合键,至少在不对某些 Eloquent'方法。
我有一个像这样的迁移table-
<?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 = ????;
有人能帮帮我吗?
谷歌搜索 laravel eloquent composite primary key
says,你不能在 Eloquent 中使用复合键,至少在不对某些 Eloquent'方法。