Consult many many laravel 5.6


Well as you can see I am learning laravel, that is why I need help from the experts in a clear way if it is not too much trouble not to get lost.

' I want to receive this fix in my vue component. and it does not work with the foreach in the view because it is not php '

I want to get all the related data from my product table with the product_attribute table in order to make my product view (list or view)

in my controller ProductController I generated the following,

          $productos = Producto::join('clases as c', 'productos.id_clase','=', '')
            ->join('grupos as g', 'productos.id_grupo', '=', '')
            ->select(' as id',
                'productos.numero as numero',
                'productos.codigo as codigo',
                'productos.unidad-venta as unidad_venta',
                'productos.precio-unitario as precio_unitario',
                'productos.stock as stock',
                'productos.inventario as inventario',
                'productos.sujeto-retencion as sujeto_retencion',
                'productos.sujeto-impuesto as sujeto_impuesto',
                'productos.palabra-clave as palabra_clave',
                'g.nombre as grupo',
                'c.nombre as clase')
            ->orderBy('id', 'ASC')->paginate(5);

    $rangos = array();
    foreach ($productos as $producto){
       $rangos[$producto->id] = Producto::find($producto->id)->rangos;

    $variable = array();
    foreach ($productos as $producto) {
        $variable[$producto->id] = Producto::find($producto->id);
        foreach($variable[$producto->id]->atributos as $varia){
            array_push($atributos, $varia);

        'pagination' => [
            'total'         => $productos->total(),
            'current-page'  => $productos->currentPage(),
            'per_page'      => $productos->perPage(),
            'last_page'     => $productos->lastPage(),
            'from'          => $productos->firstItem(),
            'to'            => $productos->lastItem()

        'productos'         => $productos,
        'rangos' => $rangos,
        'atributos' => $atributos

In the part where I have my two foreach I would like you to show me all the relationships of my product with its pivot value. But he only sends me a single record

maybe my logic is too little but I would like help to make this query

Note: I already have my two corresponding models:

Product Model

     public function atributos(){
    return $this->belongsToMany('App\Atributo', 'atributo_producto', 'id_atributo', 'id_producto')->withPivot('valor');

Attribute Model

    public function productos(){
    return $this->belongsToMany('App\Producto', 'atributo_producto', 'id_producto', 'id_atributo')->withPivot('valor');

I'm doing it this way because I want to receive the complete fix to be able to get it in my Component Product.vue

asked by Ponse 16.11.2018 в 01:42

1 answer


Your relationships are incorrect, they should look like this:

public function atributos(){
    return $this->belongsToMany('App\Atributo', 'atributo_producto', 'id_producto', 'id_atributo')->withPivot('valor');

public function productos(){
    return $this->belongsToMany('App\Producto', 'atributo_producto', 'id_atributo', 'id_producto')->withPivot('valor');

And to bring all the products with their respective attributes, just enough to do:

$productos = Producto::with('atributos')->orderBy('id', 'ASC')->paginate(5);

foreach($productos as $producto){
   $nombres_atributos = array();
   $pivotes = array();

   //Para acceder a los atributos:
   foreach($producto->atributos as $atributo){
      $nombre_atributo = $atributo->nombre;
      //Para obtener el valor pivote
      $pivote = $atributo->pivot->valor;

      $pivotes[] = $pivote;
      $nombres_atributos[] = $nombre_atributo;
answered by 16.11.2018 / 19:11