I have a User Model and a User can have many Project
public function project()
{
return $this->hasMany('App\Project');
}
I then have a Project Model and a Project can have many Document
public function profusionUser()
{
return $this->belongsTo('App\User', 'userId');
}
public function document()
{
return $this->hasMany('App\document');
}
A Document can have many DocumentData
public function project()
{
return $this->belongsTo('App\Project', 'projectId');
}
public function documentData()
{
return $this->hasMany('App\DocumentData');
}
I then have my DocumentData Model
public function document()
{
return $this->belongsTo('App\Document', 'documentId');
}
Now I think these relationships are set up correctly in terms of the foreign keys.
Now within one of my controllers I get the select doc for a project and pass it to my view. If I do
I can see something like this
Document {#298 ▼
#table: "document"
#guarded: []
#attributes: array:7 [▼
"id" => "1"
"projectId" => "1"
"name" => "someDoc"
"description" => "someDocdocument"
"deleted_at" => null
"created_at" => "2016-05-20 08:43:06"
"updated_at" => "2016-05-20 08:43:06"
]
}
So my view is getting the correct Document. Now I need to get the Data for this Document. So I have something like this in my view.
$selectedDoc->documentData->where('key', 'whoData')->first()->value
When I do this however, I get the error
Column not found: 1054 Unknown column 'document_data.document_id' in 'where clause'
So for some reason it is using document_id instead of documentId. How can I obtain the data associated to a document from within my view?
Thanks
Database Schema
Schema::create('users', function (Blueprint $table) {
$table->increments('id');
$table->string('userName')->default('');
$table->string('userEmail')->default('');
$table->tinyInteger('active')->default(1);
$table->timestamps();
});
Schema::create('projects', function (Blueprint $table) {
$table->increments('id');
$table->string('projectName')->default('');
$table->string('projectValue')->default('');
$table->integer('userId')->unsigned()->default(0);
$table->foreign('userId')->references('id')->on('users')->onDelete('cascade');
$table->tinyInteger('active')->default(1);
$table->softDeletes();
$table->timestamps();
});
Schema::create('document', function(Blueprint $table)
{
$table->increments('id');
$table->integer('projectId')->unsigned()->default(0);
$table->foreign('projectId')->references('id')->on('projects')->onDelete('cascade');
$table->string('name')->default('')->nullable();
$table->longText('description')->default('')->nullable();
$table->softDeletes();
$table->timestamps();
});
Schema::create('document_data', function(Blueprint $table)
{
$table->increments('id');
$table->integer('documentId')->unsigned()->default(0);
$table->foreign('documentId')->references('id')->on('document')->onDelete('cascade');
$table->string('key')->default('')->nullable();
$table->longText('value')->default('')->nullable();
$table->softDeletes();
$table->timestamps();
});
Update If I change my Document Model to this it works
public function documentData()
{
return $this->hasMany('App\DocumentData', 'documentId');
}
But is this not wrong setting the foreign key in both the document and documentdata models?
via Chebli Mohamed
Aucun commentaire:
Enregistrer un commentaire