Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

multiple join laravel eloquent

Question about Laravel's Eloquent ORM. I've had a look on SO already, apologies if I've missed a similar question.

These are my models:

class Formatos extends Eloquent {

    public function preguntas()
    {
        return $this->hasMany('\encuesta\Preguntas', 'fenc_id');
    }
}
class Preguntas extends Eloquent {

    public function alternativas()
    {
        return $this->hasMany('\encuesta\Alternativas', 'alt_id');
    }
}
class Alternativas extends Eloquent{


}

My controller's method is:

public function show($id)
{
    $formatos = encuesta\Formatos::find($id);
    $preguntas = $formatos->preguntas;
    $preguntas->alternativas;

    return View::make(
        'encuesta.formato.pregunta.show', 
        array('formatos' => $formatos)
    );
}

I want the following result:

FORMATO
    form_id
    form_info
    ...
    PREGUNTAS
        preg_id
        preg_descip
        ...
        ALTERNATIVAS
            alt_id
            alt_alterna
            ....

I found out and I have not had good results, please someone help me

like image 356
Waldo J. Saccaco Avatar asked Mar 20 '23 15:03

Waldo J. Saccaco


1 Answers

You may try this:

$formatos = encuesta\Formatos::with('preguntas.alternativas')->find($id);
like image 114
The Alpha Avatar answered Mar 29 '23 05:03

The Alpha