Welcome Guest, Not a member yet? Register   Sign In
"Hidden" Fields for Models
#2

In the HTML template, you yourself determine which fields to display.

For an API, for example, you can use a wrapper class that will return a set of just the fields you need.
PHP Code:
class UserDecorator implements JsonSerializable
{
    protected 
Entity $entity;

    public function 
__construct(Entity $entity)
    {
        
$this->entity $entity;
    }

    protected function 
fields()
    {
        return [
            
'name',
            
'id',
        ];
    }

    public function 
jsonSerialize()
    {
        
$data = [];

        foreach (
$this->fields() as $field) {
            
$data[$field] = $this->entity->__get($field);
        }

        return 
$data;
    }
}
new 
UserDecorator((new Model)->find($id)); 

You can specify only the required fields when working with the model.
PHP Code:
(new Model)->select('name''id')->find($id); 
Or write your own logic for processing the result of the request and call it through model events.
Reply


Messages In This Thread
"Hidden" Fields for Models - by christianitis - 06-14-2022, 08:56 AM
RE: "Hidden" Fields for Models - by iRedds - 06-15-2022, 12:45 AM
RE: "Hidden" Fields for Models - by MGatner - 06-16-2022, 04:39 AM
RE: "Hidden" Fields for Models - by b126 - 06-30-2022, 08:05 AM
RE: "Hidden" Fields for Models - by kilishan - 06-30-2022, 10:44 AM



Theme © iAndrew 2016 - Forum software by © MyBB