sql >> Databáze >  >> RDS >> Mysql

Vztah MySQL s více tabulkami

Výmluvný způsob:

// Makes that have forsale nested relation
Make::whereHas('models', function ($q) {
    $q->whereHas('trims', function ($q) {
        $q->has('forsales');
    });
})->get(); // returns Eloquent Collection

Modely se správnými vztahy (hasMany lze nahradit hasOne, pokud je to někde skutečný vztah):

// Make model
public function models()
{
    return $this->hasMany('CarModel');
}

// CarModel (as you can't use Model name)
public function trims()
{
    return $this->hasMany('Trim');
}
public function make()
{
    return $this->belongsTo('Make');
}

// Trim model
public function forsales()
{
    return $this->hasMany('Forsale');
}
public function carModel()
{
    return $this->belongsTo('CarModel');
}

// Forsale model
public function trim()
{
    return $this->belongsTo('Trim');
}


  1. Chyba SQL phpMyAdmin Nerozpoznané klíčové slovo poblíž FIELDS

  2. MySQL NOT IN dotaz nefunguje

  3. MySQL vybírá pole obsahující úvodní nebo koncové mezery

  4. Pořadí MySQL podle primárního klíče