Return of attributes in Model Laravel

Asked

Viewed 88 times

-2

I have an application where subcategories are associated with a category.

In the Subcategory Model the relationship between both is performed and the category name is returned through getattribute, where the category name is returned. The problem is that in addition to the subcategory name, all category information is returned.

The model of Subcategory is like this:

class Subcategoria extends Model
{

protected $table = 'subcategoria';
protected $keyType = 'integer';
protected $fillable = ['id_categoria', 'nome'];
protected $appends = ['links', 'anexos'];

public function categoria()
{
    return $this->belongsTo(Categoria::class, 'id_categoria');
}

public function produto()
{
    return $this->hasMany(Produto::class, 'id_subcategoria');
}

public function getLinksAttribute () : array
{
    return [
        'self' => '/api/v1/subcategoria/' . $this->id,
        'categoria' => '/api/v1/categoria/' . $this->id_categoria,
    ];
}

public function getAnexosAttribute() : array
{
    return [
        'categoria' => $this->categoria->nome,
    ];
}
}

A Model of Category:

class Categoria extends Model
{
protected $table = 'categoria';
protected $keyType = 'integer';
protected $fillable = ['nome'];
protected $appends = ['links'];

public function produto()
{
    return $this->hasMany(Produto::class, 'id_categoria');
}

public function subcategoria()
{
    return $this->hasMany(Subcategoria::class, 'id_categoria');
}

public function getLinksAttribute()
{
    return [
        'self' => '/api/v1/categoria/' . $this->id,
        'subcategorias' => '/api/v1/categoria/' . $this->id . '/subcategorias'
    ];
}
}

The return is as follows:

{
"data": {
    "id": 1,
    "nome": "Chocolate",
    "id_categoria": 13,
    "created_at": "2019-09-16 17:27:51",
    "updated_at": "2019-09-23 12:59:34",
    "links": {
        "self": "/api/v1/subcategoria/1",
        "categoria": "/api/v1/categoria/13"
    },
    "anexos": {
        "categoria": "Alimentos"
    },
    "categoria": {
        "id": 13,
        "nome": "Alimentos",
        "created_at": "2019-09-19 19:59:06",
        "updated_at": "2019-09-19 19:59:15",
        "links": {
            "self": "/api/v1/categoria/13",
            "subcategorias": "/api/v1/categoria/13/subcategorias"
        }
    }
},
"status": true
}

And I wish it were just:

{
"data": {
    "id": 1,
    "nome": "Chocolate",
    "id_categoria": 13,
    "created_at": "2019-09-16 17:27:51",
    "updated_at": "2019-09-23 12:59:34",
    "links": {
        "self": "/api/v1/subcategoria/1",
        "categoria": "/api/v1/categoria/13"
    },
    "anexos": {
        "categoria": "Alimentos"
    }
},
"status": true
}

Does anyone know what it takes to get the result desired?

  • One option I use a lot is to give a map() after get() and return only the fields I want. It’s like $listaItens = Subcategoria::where(xyz)->get()->map(function() { return ['campo1' => 'valor', 'campo2' => 'valor'] });. There must be other ways to omit fields.

  • Face it you can use Fractal for the answer to be just what you want or use Graphql. Using the fractal: https://fractal.thephpleague.com/transformers/

  • You could edit your question with the code you use to generate the JSON?

1 answer

0


The problem was solved only by adding the Hidden variable to the Model:

protected $hidden = ['categoria'];

Browser other questions tagged

You are not signed in. Login or sign up in order to post.