php - Laravel 5.2 - How to reference Composite Key in Models?
Get the solution ↓↓↓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?
Answer
Solution:
Googlinglaravel eloquent composite primary key
says, that you can't use composite keys in Eloquent, at least not without tiresome and error prone overriding of some of Eloquent's methods.
Share solution ↓
Additional Information:
Link To Answer People are also looking for solutions of the problem: call to a member function store() on null
Didn't find the answer?
Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.
Similar questions
Find the answer in similar questions on our website.
Write quick answer
Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.