Laravel 5.1 -Eager Loader gets me the last element of sub-fix

1

This is the code ..

    public function create()
    {

        $data = ComponenteFormacion::with(['campo_disciplinar'])
            ->get();

        return $data;
}

I only get the last element of "field_disciplinary" (in the example the last one I receive is "5"), and I need an arrangement with all the elements "field_disciplinary" that belong to "component_formation"

class ComponenteFormacion extends Model
{
    protected $primaryKey = 'id';
    protected $table = 'COMPONENTE_FORMACION';
    protected $fillable = array('componente_formacion');

    public function campo_disciplinar(){
        return $this->belongsTo('App\Models\CampoDisciplinar','id','componente_formacion_id');
    }

}
class CampoDisciplinar extends Model
{
    protected $primaryKey = 'id';
    protected $table = 'CAMPO_DISCIPLINAR';
    protected $fillable = array(
        'campo_disciplinar',
        'componente_formacion_id'
    );

    public function componente_formacion(){
        return $this->hasMany('App\Models\ComponenteFormacion','id','componente_formacion_id');
    }

    public function disciplina(){
        return $this->belongsTo('App\Models\Disciplina');
    }
}

    
asked by Adrian Moreno Garcia 15.03.2017 в 04:07
source

1 answer

0

You are using inverted relationships, so you only get one item, I do not know how you have designed your application, but for eager to work the way you want, it should be like this:

class ComponenteFormacion extends Model
{
    //...
    public function campo_disciplinar() {
        return $this->hasMany('App\Models\CampoDisciplinar');
    }
}


class CampoDisciplinar extends Model
{
    // ...
    public function componente_formacion() {
        return $this->belongsTo('App\Models\ComponenteFormacion');
    }
}
    
answered by 15.03.2017 в 04:50