Eloquent query in three tables

0

Good afternoon I need to make a query in laravel but the results are not what I hope, hopefully someone can help me.

These are my tables:

And these are the results I want to obtain: that the BRAND be printed only once and then the list of products:

The relationships are the following:

class Marca extends Model {   
public function productos()
{
    return $this->hasMany(Producto::class,'idmarca');
}}
-----------------
class Producto extends Model
public function categoria()
{
    return $this->belongsTo(Categoria::class,'idcategoria');
}

public function marca()
{
    return $this->belongsTo(Marca::class,'idmarca');
}

-----------------
class Categoria extends Model
{

public function productos()
{
    return $this->hasMany(Producto::class,'idcategoria');
}
    
asked by Anddy Cabrera Garcia 05.06.2018 в 00:28
source

1 answer

0

It is best to put the sql code in the poblemas, I can not help much

in the query he is doing it in the following way

$consulta = Producto::with('categoria_pk','Marca_pk')->get();

and in the model perform it in the following way

class Producto extends Model
public function categoria_pk()
{
    return $this->belongsTo('App\Categoria','idcategoria');
}

public function marca_pk()
{
    return $this->belongsTo('App\Marca','idmarca');
}

in the idcategoria and idmarca part corresponds to the foreign key if you have the problem of viewing the data in the view, place var_dump ($ data)

    
answered by 05.06.2018 в 01:33