1

I am trying to process a text field inside a Matrix field.

In the controller I have this to get posted params:

$element->setFieldValuesFromRequest($fieldsLocation);

If I want to process a custom text field, I can do it easily with

$result=$element->getFieldValue('textFieldHandle')

which $result is exactly the text field value.

but if I have this text field in Matrix field, if I do this:

$result=$element->getFieldValue('matrixFieldHandle')
craft::dd($result)

I get a craft\elements\db\MatrixBlockQuery object which I don't know how I can fetch text value from this object.

2

You have to loop the Matrix field first, something like this:

foreach($entry->matrixHandle->all() as $block) {
   echo $block->fieldHandle;
}
|improve this answer|||||
  • thanks @gioppe i should have used one() or all() because it returns craft\elements\db\MatrixBlockQuery and i had to get result first – user677900 Feb 4 at 12:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.