【问题标题】:Laravel 5.2 QueryException in Connection.php line 669Connection.php 第 669 行中的 Laravel 5.2 QueryException
【发布时间】:2016-03-28 20:18:14
【问题描述】:

我正在开发一个 API,但是当我创建关系(多对多)并希望在 index 函数中显示时,我收到一个错误 QueryException in Connection.php line 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)

我相信我的错误出在我的模型中,因为当我在该表中的 id 名称为 idTag 时,它正在 CTL_Tags 表中查找 id

这是我的 CTL_Resource 模型

<?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');
    }

}

我只会给你看其中一个关系的代码

<?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');
    }
}

这是我的控制器

<?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
      );

谁能帮助我,我将不胜感激。太感谢了。

【问题讨论】:

    标签: php sql laravel laravel-5.2 relation


    【解决方案1】:

    尝试定义

    $primaryKey
    

    在您的模型中,使用正确的列名

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

    【讨论】:

    • 谢谢先生,成功了!错误消失了,但我看不到关系,它出现了一个空数组,但我认为这是另一个问题。太感谢了。 :)
    猜你喜欢
    • 2016-09-23
    • 1970-01-01
    • 2016-09-11
    • 2017-04-19
    • 1970-01-01
    • 2017-05-11
    • 1970-01-01
    • 2016-06-15
    • 2016-06-20
    相关资源
    最近更新 更多