2016-03-28 3 views
1

i eine API bin zu entwickeln, aber wenn ich die Beziehungen (viele zu viele) erstellen und wollen in der index Funktion zeigen, ich bin immer einen Fehler QueryException in Connection.php line 669 Der Fehler sagt:Laravel 5.2 QueryException in Connection.php Linie 669

SQLSTATE[42S22]: Column not found: 1054 Unknown column 'CTL_Tags.id' in 'on clause' (SQL: select `CTL_Tags`.*, `CTL_Resource_has_Tags`.`idResource` as `pivot_idResource`, `CTL_Resource_has_Tags`.`idTag` as `pivot_idTag` from `CTL_Tags` inner join `CTL_Resource_has_Tags` on `CTL_Tags`.`id` = `CTL_Resource_has_Tags`.`idTag` where `CTL_Resource_has_Tags`.`idResource` is null) 

ich glaube, meine Fehler in meinem Modell, weil es für id in CTL_Tags Tabelle suchen ist, wenn mein Name ID in dieser Tabelle idTag ist.

Das ist mein CTL_Resource Modell

<?php 

namespace Knotion; 

use Illuminate\Database\Eloquent\Model; 

class CTL_Resource extends Model { 
    public $timestamps = false; 

    protected $table = "CTL_Resource"; 

    protected $hidden = [ 
     'coachVisibility', 'thumbnail', 'tags', 'relatedTo', 
     'studentVisibility', 'isHTML','studentIndex', 'coachIndex', 
     'isURL', 'source', 'path', 'status', 'updateTime', 'isfolder', 
     'parentResource', 'idModifierUser' 
    ]; 

    protected $fillable = ['idResourceType','productionKey', 'tags', 'idCreatorUser', 'idModifierUser', 'idCreationCountry', 'title', 'description', 'URL', 'fileName', 'extension', 'quicktag', 'minimumAge', 'maximumAge', 'productionKey']; 

    public function creatorUser() { 
     return $this->belongsTo('Knotion\OPR_User', 'idCreatorUser'); 
    } 
    public function creationCountry() { 
     return $this->belongsTo('Knotion\CTL_Country', 'idCreationCountry'); 
    } 
    public function resourceType() { 
     return $this->belongsTo('Knotion\CTL_ResourceType', 'idResourceType'); 
    } 
    public function quickTags() { 
     return $this->belongsToMany('Knotion\CTL_QuickTag', 'CTL_Resource_has_QuickTags', 'idResource', 'idQuickTag'); 
    } 
    public function tags() { 
     return $this->belongsToMany('Knotion\CTL_Tag','CTL_Resource_has_Tags', 'idResource', 'idTag'); 
    } 
    public function relatedTo() { 
     return $this->belongsToMany('Knotion\CTL_RelatedTo', 'CTL_Resource_has_RelatedTo', 'idResource', 'idRelatedTo'); 
    } 

} 

und ich werde Sie nur eines der Beziehungen

<?php 

namespace Knotion; 

use Illuminate\Database\Eloquent\Model; 

class CTL_QuickTag extends Model { 
    protected $table = "CTL_QuickTags"; 
    protected $fillable = ['name']; 
    protected $hidden = ['status', 'createTime', 'updateTime']; 

    public function resources() { 
     return $this->belongsToMany('Knotion\CTL_Resource', 'CTL_Resource_has_QuickTags', 'idResource', 'idQuickTag'); 
    } 
} 

den Code zeigen, und das ist mein Controller-

<?php 

namespace Knotion\Http\Controllers; 

use Illuminate\Http\Request; 
use Knotion\Http\Requests; 
use Knotion\Http\Requests\ResourcesRequest; 

use Knotion\CTL_Resource; 
use Knotion\CTL_Tag; 
use Knotion\CTL_QuickTag; 
use Knotion\CTL_RelatedTo; 
use Knotion\CTL_ResourceType; 


class ResourcesController extends Controller { 

    public function index(Request $request) { 

     $resources = CTL_Resource::paginate(10); 

     $resources->each(function($resources) { 
      $resources->tags; 
      $resources->quickTags; 
      $resources->relatedTo; 
     }); 

     return response()->json(
      $resources 
    ); 

I‘ Ich bin so dankbar, wer mir helfen kann. Ich danke dir sehr.

Antwort

1

Versuchen

$primaryKey 

in Ihrem Modell, mit dem richtigen Spaltennamen Sie

https://laravel.com/docs/5.2/eloquent#defining-models

+0

Vielen Herr zu definieren, es hat funktioniert! Der Fehler ist weg, aber ich kann die Beziehungen nicht sehen, es scheint ein leeres Array zu sein, aber ich denke, das ist ein anderes Problem. Ich danke dir sehr. :) –