I want to call a method of my laravel driver to return a JSON with the data that a DDBB. I have a Produtos table with the fields:
id, name, idCategoria, idGenero, stock
A table Category with:
id, name, created
And a Genre table with:
id, name, created
In the product model I have added the following functions:
public function categoria()
{
return $this->belongsTo(Categoria::class, 'idCategoria');
}
public function genero()
{
return $this->belongsTo(Genero::class, 'idGenero');
}
In the category model and in the gender model I have added:
public function productos()
{
return $this->hasMany(Productos::class);
}
The fact is that I want you to show me all the fields in the product table with the 'idCategoria' and 'idGenero' replaced by the name of their category and corresponding gender. I'm trying this:
public function index_front() {
$products = Productos::with('categoria')->get()->pluck('id', 'nombre','categoria.nombre', 'stock');
return json_encode($products);
}
But I can not make him show me what I need.