String to Object?

I have defined a Checkbox field in the blueprint and get this with a foreach loop, but how can I use the Kirby field methods with this as it’s a string now?

$wohnbereiche = $motive->pluck('wohnbereiche', ',', true);

foreach ( $wohnbereiche AS $wohnbereich ) {
  echo $wohnbereich;            // working
  echo $wohnbereich->slug();    // not working cause it's a string
}

Kirby’s Str class to the rescue:

Str::slug($wohnbereich)

If you actually need a field, you could create a new Field instance, but that would probably be overkill.

Very simple but it works :smiley: so there is no method or similar like ->toObject()?

Well, the thing is, to call a method using arrow syntax, you need some kind of object first. A simple string is not an object.

And doing this doesn’t really make sense for your use case.

$field = new Field(null, '', $wohnbereich);
echo $field->slug();

Ahh ok, I understand. But with the attached example I am able to use many other methods like toLower() and a few others :slight_smile:

Yes, but for most use cases, there are also the Str class method or helpers (like kirbytext() etc). Depends on what you need, really.

Great, thanks so much :smiley: