присоединиться к столу в Laravel - PullRequest
0 голосов
/ 04 июля 2018

у меня 3 стола в laravel:

class General extends Mode
{
 public function populars()
    {
        return this->hasMany('App\Popular');
    }
}

enter code here

и

class Popular extends Model
{
    public function general()
    {
        return  this->belongsTo('App\General');
    }
}

и

class Specific extends Model
{
public function popular(){
        return this->belongsTo('App\Popular');
    }
}
...

как объединить таблицы и вернуть результат списка:

 1. generals
     2.popular
      3.Specific

1 Ответ

0 голосов
/ 04 июля 2018

Я предполагаю, что Popular имеет много Specific, вы можете добавить другое отображение в Popular модель как

class Popular extends Model
{
    public function general()
    {
        return  this->belongsTo('App\General');
    }
    public function specific()
    {
        return this->hasMany('App\Specific');
    }

}

Делая красноречивым образом, вы могли бы написать это как

$generals = General::with('populars.specific')->get();

Используя построитель запросов, вы можете присоединиться к ним как

DB::table('general as g')
    ->join('popular as p', 'g.id','=','p.general_id')
    ->join('specific as s', 'p.id','=','p.popular_id')
    ->get();
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...