vendredi 20 mai 2016

Accessing related Models

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



from Newest questions tagged laravel-5 - Stack Overflow http://ift.tt/20e1cwU
via IFTTT

Aucun commentaire:

Enregistrer un commentaire