Get field from array and work like normal

I have the name of a field in an array and I want to turn it back into a field i can work with.

This wont work obviously because its a string now:


How can i turn $att['field'] into something i can then chain stuff onto like I would directly on a field?

I don’t think you can “turn” a string into a field but you should be able to call it by name.
Assuming you are in a page context (untested):

if($field = $page->$att['field']()->exists()) {
   echo $field->toDate($format);

Thanks for trying, but it doesnt work, it still sees it as a string. I’m doing this in a plugin.

It is difficult to find out what the problem is without more information like error messages and your code.

It’s a big plugin, to much to post, but the problem really is just that - I know the name of the field in an array and i want get it as a proper field so i can then chain field methods on to it, if the field exists.

I got the following error with the code you posted…

**Fatal error** : Uncaught Whoops\Exception\ErrorException: Array to string conversion

I did this in a function…

$att = json_decode(json_encode($tag['attributes']), true);

if($field = page()->$att['field']()->exists()) {
	return $field->toDate($format);

This doesn’t make sense because $field will now be a boolean, not a field object.


if(page()->{$att['field']}()->exists()) {
	return  page()->{$att['field']}()->toDate($format);
1 Like

Thanks :slight_smile: That did the trick… expect an awesome plugin sometime soon :slight_smile: