无法显示多对多关系的元素 Laravel 5.2
Can't show elements from many to many relationship Laravel 5.2
我正在尝试从 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';
考虑到您的 idResource
显示为 0,一些类似的字段看起来像 GUID,并且您的外键字段定义为 varchar(40)
,我将假设您的 CTL_Resource.idResource
字段也定义为 varchar(40)
.
在这种情况下,当 $primaryKey
字段不是自动递增整数时,您需要告诉模型。您可以通过在模型上设置 $incrementing
属性 来做到这一点:
public $incrementing = false;
此外,关于您的 quick_tags
与 quickTags
问题,这与您模型上的 $snakeAttributes
属性 有关。当设置为 true(默认)时,您的模型将 snake_case
转换为 json(toJson()
)或数组(toArray()
)时关系对象的键。如果你想关闭这个,你需要在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 ...
}
我正在尝试从 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';
考虑到您的 idResource
显示为 0,一些类似的字段看起来像 GUID,并且您的外键字段定义为 varchar(40)
,我将假设您的 CTL_Resource.idResource
字段也定义为 varchar(40)
.
在这种情况下,当 $primaryKey
字段不是自动递增整数时,您需要告诉模型。您可以通过在模型上设置 $incrementing
属性 来做到这一点:
public $incrementing = false;
此外,关于您的 quick_tags
与 quickTags
问题,这与您模型上的 $snakeAttributes
属性 有关。当设置为 true(默认)时,您的模型将 snake_case
转换为 json(toJson()
)或数组(toArray()
)时关系对象的键。如果你想关闭这个,你需要在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 ...
}