samedi 13 octobre 2018

laravel two lavel relation query

I have next DB structure

Labels -> LabelPerson <- People -> Samples

And I have all needed relations in Models. But how can I get all samples, when I know only label_id?

Label:
public function people()
{
    return $this->belongsToMany('App\Person');
}

Person:
public function labels()
{
    return $this->belongsToMany('App\Label')->withTimestamps();
}
public function samples()
{
    return $this->hasMany('App\Sample');
}

Sample:
public function person()
{
    return $this->belongsTo('App\Person');
}

I try to use hasManyThrough relation in Label model

public function samples()
{
    return $this->hasManyThrough('App\Sample', 'App\Person');
}

But no result

Column not found: 1054 Unknown column 'people.label_id' in 'field list' (SQL: select samples.*, people.label_id from samples inner join people on people.id = samples.person_id where people.label_id = 2)",

Laravel 5.7.9



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire