Question

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

Was it helpful?

Solution

You may try this:

$formatos = encuesta\Formatos::with('preguntas.alternativas')->find($id);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top