无法显示Laravel 5.2中多对多关系中的元素

时间:2016-03-29 02:39:40

标签: php laravel many-to-many laravel-5.2 database-relations

我试图在Laravel 5.2中查询来自多对多关系的数据但是当我想要检索数据时,获取空白信息。我有些疑惑,因为我收到的数据只检测到quick_tags,但是那里没有数据,它应该是quickTags

我在我的控制器中执行此操作

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

我在我的模型中有这个代码

<?php

namespace Knotion;

use Illuminate\Database\Eloquent\Model;

class CTL_Resource extends Model  {
    public $timestamps = false;

    protected $table = "CTL_Resource";
    protected $primaryKey = "idResource";

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

}

这是结果。如您所见,quick_tags及其空

{
  "total": 2,
  "per_page": 10,
  "current_page": 1,
  "last_page": 1,
  "next_page_url": null,
  "prev_page_url": null,
  "from": 1,
  "to": 2,
  "data": [
    {
      "idResource": 0,
      "idResourceType": "49ee39d6-eecd-11e5-b044-4914876a7f3d",
      "idCreatorUser": "04664624-eecd-11e5-b044-4914876a7f3d",
      "idCreationCountry": "b4afa9ae-eecc-11e5-b044-4914876a7f3d",
      "productionKey": "1234567890",
      "title": "ElTitle1",
      "description": "ElDescription1",
      "minimumAge": "5",
      "maximumAge": "10",
      "fileName": "ElFileName1",
      "extension": ".png",
      "URL": "ElURL1",
      "createTime": "2016-03-28 14:07:21",
      "quick_tags": []
    },
    {
      "idResource": 0,
      "idResourceType": "49ee39d6-eecd-11e5-b044-4914876a7f3d",
      "idCreatorUser": "04664624-eecd-11e5-b044-4914876a7f3d",
      "idCreationCountry": "b4afa9ae-eecc-11e5-b044-4914876a7f3d",
      "productionKey": "0987654321",
      "title": "ElTitle2",
      "description": "ElDescription2",
      "minimumAge": "5",
      "maximumAge": "10",
      "fileName": "ElFileName2",
      "extension": ".png",
      "URL": "ElURL2",
      "createTime": "2016-03-28 14:44:37",
      "quick_tags": []
    }
  ]
}

我不知道它是否必要,但这是关系的sql代码。我刚发布这个,但其他人非常相似

DROP TABLE IF EXISTS `CTL_Resource_has_QuickTags`;
CREATE TABLE `CTL_Resource_has_QuickTags` (
  `idResource` varchar(40) NOT NULL COMMENT 'Foreign key to the CTL_Resource table ',
  `idQuickTag` varchar(40) NOT NULL COMMENT 'foreign key to the CTL_QuickTags table.',
  PRIMARY KEY (`idResource`,`idQuickTag`),
  KEY `fk_CTL_Resource_has_QuickTag_QuickTag1_idx` (`idQuickTag`),
  KEY `fk_CTL_Resource_has_QuickTag_CTL_Resource1_idx` (`idResource`),
  CONSTRAINT `fk_CTL_Resource_has_QuickTag_CTL_Resource1_idx` FOREIGN KEY (`idResource`) REFERENCES `CTL_Resource` (`idResource`) ON DELETE NO ACTION ON UPDATE NO ACTION,
  CONSTRAINT `fk_CTL_Resource_has_QuickTag_QuickTag1_idx` FOREIGN KEY (`idQuickTag`) REFERENCES `CTL_QuickTags` (`idQuickTag`) ON DELETE NO ACTION ON UPDATE NO ACTION
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='this table establishes the amount of quicktags that a given tag';

1 个答案:

答案 0 :(得分:0)

考虑到您的idResource显示为0,以及某些类似的字段看起来像GUID,而您的外键字段定义为varchar(40),我将假设您的{{ 1}}字段也定义为CTL_Resource.idResource

在这种情况下,当varchar(40)字段不是自动递增整数时,您需要告诉模型。您可以通过在Model:

上设置$primaryKey属性来完成此操作
$incrementing

此外,关于您的public $incrementing = false; vs quick_tags问题,这与您的模型上的quickTags属性有关。设置为true(默认值)时,转换为json($snakeAttributes)或数组(snake_case)时,模型将toJson()关系对象的键。如果要关闭它,则需要在Model:

上设置toArray()属性
$snakeAttributes

因此,您的模型最终应该看起来像:

public static $snakeAttributes = false;