laravel高级连接5个表,比较2个表

时间:2014-06-06 18:46:39

标签: mysql join laravel-4 eloquent

我甚至不确定这是否可行,我可以让它处理第二个连接函数,然后它只返回null。

$articles = DB::table('courses')
     ->join('version','courses.current_version_id','=','version.id')
     ->join('category as c1', function($join1){
           $join1->on('courses.code','=','c1.title');
     })
     ->join('category as c2', function($join2){
           $join2->on('version.version_number','=','c2.title');
     })
     ->join('articleCategories','c2.id','=','articleCategories.cat_id')
     ->join('articles','articleCategories.article_id','=','articles.id')
     ->where('c2.parent_id','=','c1.id')
     ->where('courses.id','=',$course)
     ->select('courses.code','courses.title','version.version_number','articles.*')
     ->get();

这甚至可能吗?我错了什么?

1 个答案:

答案 0 :(得分:0)

您可以尝试这样的事情:

$fieldsToSelect = array(
    'courses.code',
    'courses.title',
    'courses.current_version_id',
    'version.version_number',
    'version.id',
    'category.id',
    'category.title',
    'articleCategories.cat_id',
    'articleCategories.article_id',
    'articles.*'
);

$articles = DB::table('courses')->join('version', function($join){
    $join->on('courses.current_version_id', '=', 'version.id');
})->join('category', function($join) {
    $join->on('courses.code', '=', 'category.title')
         ->on('version.version_number', '=', 'category.title');
})->join('articleCategories', function($join){
    $join->on('category.id', '=', 'articleCategories.cat_id');
})->join('articles', function($join){
    $join->on('articleCategories.article_id', '=', 'articles.id');
})
->get($fieldsToSelect);