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','=', 'c.id')
->join('grupos as g', 'productos.id_grupo', '=', 'g.id')
->select('productos.id 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;
}
$atributos=array();
$variable = array();
foreach ($productos as $producto) {
$variable[$producto->id] = Producto::find($producto->id);
foreach($variable[$producto->id]->atributos as $varia){
array_push($atributos, $varia);
}
}
return[
'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