Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

elcuy's avatar

Relationships between 3 tables, how do I do this?

Hello everyone!

I'm trying to set relationships between 3 models in this way (Sorry about the class names being in spanish, I hope they are easy to understand):

Model 1

class RequisicionCabecera extends Eloquent {
    public function lineas()
    {
        return $this->hasMany('RequisicionLinea','requisicion');
    }

}

Model 2

class RequisicionLinea extends Eloquent {

    public function producto()
    {
        return $this->hasOne('Producto','id_producto');
    }

}

Model 3

class Producto extends Eloquent {

}

So what I'm trying to do is something like this:

$cabecera->lineas->producto;

Or in other words: Get all the "Lineas" that belong to a "Cabecera" and then get the "Producto" that belongs to each "Linea". How do I do this in an easy way?

Thanks in advance!

0 likes
4 replies
sebdesign's avatar

You should define the belongsTo relationship of the RequisicionLinea in the Producto model. Then you can do this:

$productos = Producto::whereHas('linea', function($query) use ($cabecera) {
    return $query->where('requisicion', $cabacera->id);
})->get();
elcuy's avatar

Hi @sebdesign, thanks for your answer. I don't think that's quite what i'm looking for. I need a structure like this: (sorry I couldn't figure out how to format that JSON string, but you can use some online JSON viewer to parse it)

{"id":1,"ef_id":"110","origen":"Haciendas","ext_id":"1","fecha_inicio":"2015-11-18 08:09:14","fecha_fin":"0000-00-00 00:00:00","id_solicitante":"erojas","nombre_solicitante":"Enrique Rojas","id_cc":"1234","nombre_cc":"Itulcachi","descripcion":"Requisici\u00f3n de prueba","prioridad":"N","estado":"A","lineas":[{"id":1,"requisicion":1,"ext_id":"1","id_producto":1,"observacion":"Verificar que sean palas anaranjadas","cant_sol":15,"cant_oc":0,"cant_rec":0,"valor_sol":0,"valor_oc":0,"valor_rec":0,"producto":{"id":1,"codigo":"0001","origen":"Haciendas","nombre":"Pala met\u00e1lica","observacion":"","precio_promedio":0,"dias_promedio":0}},{"id":2,"requisicion":1,"ext_id":"2","id_producto":2,"observacion":"Por favor comprar el que sabe a pino","cant_sol":10,"cant_oc":0,"cant_rec":0,"valor_sol":0,"valor_oc":0,"valor_rec":0,"producto":{"id":2,"codigo":"0002","origen":"Haciendas","nombre":"Glifosato 1 litro","observacion":"","precio_promedio":0,"dias_promedio":0}},{"id":3,"requisicion":1,"ext_id":"3","id_producto":3,"observacion":"Para pasar el mal sabor del pino","cant_sol":20,"cant_oc":0,"cant_rec":0,"valor_sol":0,"valor_oc":0,"valor_rec":0,"producto":{"id":3,"codigo":"0003","origen":"Haciendas","nombre":"Bid\u00f3n de agua","observacion":"","precio_promedio":0,"dias_promedio":0}}]}

I think your query is returning only the Products, not the full structure (its similar to an Invoice which has Lines, which have a product each). Do you have any other approach? Playing with it for a while got to this:

$cabecera = self::where("ef_id",$ef_id)->first();

        $cabecera->lineas;

        foreach ($cabecera->lineas as $linea) {
            $linea->producto;
        }
        
        return $cabecera;
sebdesign's avatar
Level 29

Hmmm I'm not sure I fully understand. The last piece of code you posted seems ok. If you have a $cabacera instance then you can eager load the relationships like this:

    return $cabacera->load('lineas.producto');
veve286's avatar

just eager load it.


$cavecera=RequisicionCabecera::with('lineas.producto')->find($id);

Then foreach it like that foreach($cavecera->lineas as $linea){ echo $linea->producto; }

Please or to participate in this conversation.