无法显示从多到多关系的元素 拉拉维尔 5.2



im试图从Laravel 5.2中的多对多关系中查询数据,但当我想检索数据时,我得到了空白信息。我有一些疑问,因为我收到的数据只检测到quick_tags,但里面没有数据,应该是quickTags

我在我的控制器中这样做

<?php
namespace KnotionHttpControllers;
use IlluminateHttpRequest;
use KnotionHttpRequests;
use KnotionHttpRequestsResourcesRequest;
use KnotionCTL_Resource;
use KnotionCTL_Tag;
use KnotionCTL_QuickTag;
use KnotionCTL_RelatedTo;
use KnotionCTL_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 IlluminateDatabaseEloquentModel;
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('KnotionOPR_User', 'idCreatorUser');
    }
    public function creationCountry() {
      return $this->belongsTo('KnotionCTL_Country', 'idCreationCountry');
    }
    public function resourceType()  {
      return $this->belongsTo('KnotionCTL_ResourceType', 'idResourceType');
    }
    public function quickTags() {
      return $this->belongsToMany('KnotionCTL_QuickTag', 'CTL_Resource_has_QuickTags', 'idResource','idQuickTag');
    }
    public function tags() {
      return $this->belongsToMany('KnotionCTL_Tag','CTL_Resource_has_Tags', 'idResource', 'idTag');
    }
    public function relatedTo() {
      return $this->belongsToMany('KnotionCTL_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';

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

在这种情况下,当$primaryKey字段不是一个自动递增的整数时,您需要告诉Model。您可以通过在Model上设置$incrementing属性来完成此操作:

public $incrementing = false;

此外,关于您的quick_tagsquickTags问题,这与Model上的$snakeAttributes属性有关。当设置为true(默认值)时,当转换为json(toJson())或数组(toArray())时,模型将snake_case关系对象的键。如果要关闭此功能,则需要在Model上设置$snakeAttributes属性:

public static $snakeAttributes = false;

所以,你的模型最终应该看起来像:

class CTL_Resource extends Model  {
    // set the table name since it is not ctl_resources
    protected $table = "CTL_Resource";
    // set the primary key field since it is not id
    protected $primaryKey = "idResource";
    // primary key is not an auto-incrementing int
    public $incrementing = false;
    // no created_at, updated_at fields on this table
    public $timestamps = false;
    // don't snake case the relationship names on json/array conversion
    public static $snakeAttributes = false;
    // code ...
}

相关内容

  • 没有找到相关文章

最新更新