简体   繁体   English

Laravel WhereIn查询问题

[英]Laravel WhereIn Query Issue

Here when i echo the $TagDatas; 在这里,当我回显$TagDatas; from the below query 从下面的查询

$TagDatas = TagModel::whereIn('TagId', explode(',', $BlogData->Tagged))->get(); $ TagDatas = TagModel :: whereIn('TagId',explode(',',$ BlogData-> Tagged))-> get(); echo $TagDatas; echo $ TagDatas;

I am getting 我正进入(状态

[{"AutoId":2,"TagId":2,"TagName":"chrome","TagDescription":null,"CreatedAt":null,"CreatedBy":null,"UpdatedAt":null,"UpdatedBy":null,"IsDeletable":null,"Status":1},{"AutoId":3,"TagId":3,"TagName":"google","TagDescription":null,"CreatedAt":null,"CreatedBy":null,"UpdatedAt":null,"UpdatedBy":null,"IsDeletable":null,"Status":1}] [{“ AutoId”:2,“ TagId”:2,“ TagName”:“ chrome”,“ TagDescription”:null,“ CreatedAt”:null,“ CreatedBy”:null,“ UpdatedAt”:null,“ UpdatedBy”: null,“ IsDeletable”:null,“ Status”:1},{“ AutoId”:3,“ TagId”:3,“ TagName”:“ google”,“ TagDescription”:null,“ CreatedAt”:null,“ CreatedBy “:null,” UpdatedAt“:null,” UpdatedBy“:null,” IsDeletable“:null,” Status“:1}]]

But when i try 但是当我尝试

echo $TagDatas->TagName;

I am getting Undefined property: Error 我正在获取Undefined property:错误

What is the mistake i am doing and How can i do it. 我正在做的错误是什么,我该怎么做。

Note : 注意 :

I am using whereIn where the query will be equal to 我正在使用whereIn哪里查询等于

$BlogData->Tagged i.e., It will be 2,3

So the Query will be 所以查询将是

$TagDatas = TagModel::whereIn('TagId', array(2,3))->get();

Because $TagDatas will be a collection . 因为$TagDatas将是一个collection Loop over it to get the individual tags: 循环获取单个标签:

foreach($TagDatas as $TagData){
    echo $TagData->TagName;
}

The error is quite obvious. 错误非常明显。

$TagDatas is a collection of your model . $ TagDatas是模型的集合。 You need to iterate through that collection to get each item. 您需要遍历该集合以获取每个项目。 The collection does not have the attribute TagName only its elements(Which are of type TagModel) have it. 集合没有属性TagName,只有其元素(属于TagModel类型)才具有它。

So you should do this instead to echo out the TagName of each of them. 因此,您应该这样做,以回显它们每个的TagName。

foreach($TagDatas as $TagData){
    echo $TagData->TagName;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM