how to make a join a pivot table?

0

That such a friend can support me like making a join from a normal table to a pivot table the tables are: * orders * products * OrderPorduct (pivot table)

What I have to do is a join of orders toProduct Order where the id of the order and the id of the product is inProduct Order. I hope I have explained well.

    
asked by roman madrigal 04.06.2016 в 06:44
source

1 answer

0

Well I hope this helps you ...

First in your model for your pivot Product Order you must create 2 functions: one for order and another for product

public function orden()
{
    return $this->belongsTo('App\Orden', 'foreign_key');
}

public function producto()
{
    return $this->belongsTo('App\Producto', 'foreign_key');
}

After this in your controller OrderProductController

We call our model Product Order

Use OrdenProducto

And in your role you have to do the following

public function index(){
 $orden_prod = OrdenProducto::with('orden','producto');
 return view('tuvista',compact('$orden_prod'));
}

Then in your sight

@foreach($orden_prod as $op)
 //Si quieres obtener el nombre de un producto
    $op->producto->name;
 //De la misma forma si quieres obtener la fecha de la orden
    $op->orden->fecha 
@endforeach
    
answered by 06.06.2016 / 18:33
source