存在Laravel选择案例关系

存在Laravel选择案例关系,laravel,laravel-5,eloquent,laravel-5.1,Laravel,Laravel 5,Eloquent,Laravel 5.1,我在我的应用程序上安装了一个流行的应用程序,但现在如果存在某种关系,我需要调整“相关性”列。我的问题是,我要检查的关系非常复杂,不可能通过连接复制,因此解决方案如下: $results = MyModel::with('relation1', 'relation2') ->someComplexScopeRelation() ->distinct() ->search($string

我在我的应用程序上安装了一个流行的应用程序,但现在如果存在某种关系,我需要调整“相关性”列。我的问题是,我要检查的关系非常复杂,不可能通过连接复制,因此解决方案如下:

$results = MyModel::with('relation1', 'relation2')
                ->someComplexScopeRelation()
                ->distinct()
                ->search($string, $searchableColumns, true)
                ->orderBy('field', 'DESC')
                ->leftjoin('table2', function($q){
                    $q->on('table2.a','=','my_models.b');
                })
                ->select(array('field1','field2', DB:raw('CASE WHEN count(table2.id) > 0 THEN (relevance  + relevance * (count(table2.id) * 0.25)) ELSE relevance END AS relevance')))
                ->get();
不可能,因为与
表2
的关系包括与其他表的一些复杂条件。比如:

public function table2Relationship()
{
    return $this->hasOne('Table2')
                ->where('field', 'value')
                ->where(function($q){
                    $q->where('field', 'value')
                        ->orWhere('field', 'value');
                })
                ->where('field', 'value')
                ->whereHas('AnotherRelation', function($q){
                    $q->where('field', 'value');
                })
                ->whereHas('AnotherRelation2', function($q){
                    $q->where('field', 'value')
                        ->where('field', 'value');
                })
                ->where(function($q){

                    if(CONDITION)
                        $q->where('field', 'value')
                            ->orWhere('field', 'value');
                    else
                        $q->where('field', 'value')
                            ->where(function($q){
                                $q->where('field', 'value')
                                    ->orWhere('field', 'value');
                            })
                });
}
$results = MyModel::with('relation1', 'relation2')
                ->someComplexScopeRelation()
                ->distinct()
                ->search($string, $searchableColumns, true)
                ->orderBy('field', 'DESC')
                ->select(array('field1','field2'))
                ->addCaseSelect(function($q) {
                    $q->addCase(function($q) {
                                    $q->has('table2Relationship', function($q){
                                        $q->where('extra_conditions', 'value');
                                    }, '>=', 1);
                                }, 
                                DB::raw('(relevance + relevance * (count_table2Relationship * 0.25))'), //THEN
                                DB::raw('relevance') //ELSE
                        );
                    $q->addCase(function($q) {
                                    $q->has('table3Relationship', function($q){
                                        $q->where('extra_conditions', 'value');
                                    }, '=', 0);
                                }, 
                                DB::raw('relevance - 1'), //THEN
                                DB::raw('relevance') //ELSE
                        );
                }, DB::raw('relevance')) //AS
                ->get();
我需要的是使用我的关系创建多个案例,如果条件/关系存在,则我会增加或减少列
相关性
的值,该值最初由上述包计算,并且没有声明关系的重复代码

我需要像这样的东西:

public function table2Relationship()
{
    return $this->hasOne('Table2')
                ->where('field', 'value')
                ->where(function($q){
                    $q->where('field', 'value')
                        ->orWhere('field', 'value');
                })
                ->where('field', 'value')
                ->whereHas('AnotherRelation', function($q){
                    $q->where('field', 'value');
                })
                ->whereHas('AnotherRelation2', function($q){
                    $q->where('field', 'value')
                        ->where('field', 'value');
                })
                ->where(function($q){

                    if(CONDITION)
                        $q->where('field', 'value')
                            ->orWhere('field', 'value');
                    else
                        $q->where('field', 'value')
                            ->where(function($q){
                                $q->where('field', 'value')
                                    ->orWhere('field', 'value');
                            })
                });
}
$results = MyModel::with('relation1', 'relation2')
                ->someComplexScopeRelation()
                ->distinct()
                ->search($string, $searchableColumns, true)
                ->orderBy('field', 'DESC')
                ->select(array('field1','field2'))
                ->addCaseSelect(function($q) {
                    $q->addCase(function($q) {
                                    $q->has('table2Relationship', function($q){
                                        $q->where('extra_conditions', 'value');
                                    }, '>=', 1);
                                }, 
                                DB::raw('(relevance + relevance * (count_table2Relationship * 0.25))'), //THEN
                                DB::raw('relevance') //ELSE
                        );
                    $q->addCase(function($q) {
                                    $q->has('table3Relationship', function($q){
                                        $q->where('extra_conditions', 'value');
                                    }, '=', 0);
                                }, 
                                DB::raw('relevance - 1'), //THEN
                                DB::raw('relevance') //ELSE
                        );
                }, DB::raw('relevance')) //AS
                ->get();
应该返回如下内容:

SELECT field1, field2, 
(
    CASE 
        WHEN count(SELECT count(table2.id) FROM table2 WHERE ...) as count_table2Relationship >= 1 THEN (relevance + relevance * (count_table2Relationship * 0.5)) ELSE relevance 
        WHEN count(SELECT count(table3.id) FROM table3 WHERE ...) as count_table3Relationship = 0 THEN relevance - 1 ELSE relevance 
    END
) AS relevance FROM my_models WHERE ...
有人知道这方面的解决方案或满足我需求的软件包吗

经过几个小时的搜索,我没有发现任何带有案例条件(或IF/ELSE)的软件包。在帮助下,我可以为这个创建一个包…也许这会让你大吃一惊


谢谢

你在这里的例子,至少对我来说,缺乏任何真实的背景,让人很难准确理解关系结构是什么。你能提供一个没有那么多意义的(一个严厉的词,但是很贴切的)标识符的例子吗?像
if(CONDITION)
这样的事情是没有帮助的。嗨!谢谢你的回复。如果你明白了,这种关系并不重要。重要的是,尽管拉威尔的连接方法不可能复制我关系的所有条件。如果我需要类似于
selectfield1,field2,CASE WHEN field1>2的内容,那么相关性+1 ELSE相关性从我的_模型中结束,其中这很简单,但我的目标是使用一个或多个关系(没有重复的代码)获得相同的结果。当我们使用whereHas('relationshipName',function(){})
但在
选择CASE
条件中使用时,我们的目标是相同的