Home > Blockchain >  Laravel hasManyThrough giving error when nesting
Laravel hasManyThrough giving error when nesting

Time:11-16

I essentially have 3 tables that I want to nest, 2 already work but I can't get hasManyThough to work. My tables :

tablename : measurements            
id  
client_id   (not important for now.)
            
tablename : measurement_data            
id  
measurement_id  
measurement_field_id    
data        
    
tablename : measurement_fields          
id         
type_id    (not important for now)
name       
enabled   
                    

Heres my controller function :

    public function index()
    {
        return JsonResource::collection(Measurement::with(['clients', 'measurement_data', 'measurement_fields'])->get());
    }

My Model Measurement functions :

    public function clients() {
        return $this->belongsTo(Client::class, 'client_id');
    }

    public function measurement_field() {
        return $this->hasMany(MeasurementData::class);
    }

    public function measurement_data() {
        return $this->hasManyThrough(MeasurementField::class, MeasurementField::class, 'measurement_field_id', 'measurement_id');
    }

My Model MeasurementData functions :

    public function measurements() {
        return $this->belongsTo(Measurements::class, 'measurement_id'); 
    }

    public function measurement_fields() {
        return $this->hasOne(MeasurementType::class, 'type_id');
    }

My Model MeasurementField functions :

    public function measurements() {
        return $this->belongsTo(MeasurementsData::class, 'measurement_field_id'); 
    }

Whenever I fetch it I get this in console :

"SQLSTATE[42S22]: Column not found: 1054 Unknown column 'measurement_fields.measurement_field_id' in 'field list' (SQL: select `measurement_data`.*, `measurement_fields`.`measurement_field_id` as `laravel_through_key` from `measurement_data` inner join `measurement_fields` on `measurement_fields`.`id` = `measurement_data`.`measurement_id` where `measurement_fields`.`measurement_field_id` in (1, 2, 3))"

CodePudding user response:

In Your Measurement Model Change

public function measurement_data() {
    return $this->hasManyThrough(MeasurementField::class, MeasurementField::class, 'measurement_field_id', 'measurement_id');
}

TO

public function measurement_data() {
        return $this->hasManyThrough(MeasurementField::class, MeasurementData::class, 'measurement_id', 'measurement_field_id');
    }

Refer This

CodePudding user response:

You have some mistakes about the relationships :) In your Measurement model functions you have changed the the measurement_field and measurement_data. Try this:

public function measurement_datas() {
    return $this->hasMany(MeasurementData::class);
}


public function measurement_fields() {
    return $this->hasManyThrough(MeasurementField::class, MeasurementField::class, 'measurement_field_id', 'measurement_id');
}

In your MeasurementField Model functions:

public function measurement_data() {
    return $this->belongsTo(MeasurementsData::class, 'measurement_field_id'); 
}

In your MeasurementData Model functions:

 public function measurement() {
    return $this->belongsTo(Measurements::class, 'measurement_id'); 
}

public function measurement_field() {
    return $this->hasOne(MeasurementType::class, 'type_id');
}

It is important to use the nameing convention. If it is a one-to-many relation, use plural on the 'many' side, and singular on the 'one' side. Also the name of the realtionship should indicate the name of the class that has been on the relationship, to avoid misunderstanding. Also try to give the correct key names which are presented in the db.

For more information you can find really clear and good documentation about this topic: https://laravel.com/docs/8.x/eloquent-relationships

  • Related