0

Here when i echo the $TagDatas; from the below query

$TagDatas = TagModel::whereIn('TagId', explode(',', $BlogData->Tagged))->get(); 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}]

But when i try

echo $TagDatas->TagName;

I am getting Undefined property: Error

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

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

So the Query will be

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

2 Answers 2

1

The error is quite obvious.

$TagDatas is a collection of your model . 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.

So you should do this instead to echo out the TagName of each of them.

foreach($TagDatas as $TagData){
    echo $TagData->TagName;
}
Sign up to request clarification or add additional context in comments.

Comments

1

Because $TagDatas will be a collection. Loop over it to get the individual tags:

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.