如何使用where和关系获取模型集合?

时间:2015-07-28 02:47:19

标签: php laravel eloquent

我有一个SELECT recipeID , SUM([1]) [1] , SUM([2]) [2] , SUM([3]) [3] , SUM([4]) [4] , SUM([11]) [11] , SUM([12]) [12] , SUM([13]) [13] , SUM([14]) [14] FROM ( SELECT [recipeID] ,[componentID] ,[count] ,ROW_NUMBER() over(partition by [recipeID] order by ComponentID) rn ,ROW_NUMBER() over(partition by [recipeID] order by ComponentID)+10 rn10 FROM [Recipe_Ingredients] ri_ ) as ri PIVOT ( sum([componentID]) for rn in ([1],[2],[3],[4])) as pvt PIVOT ( sum([count]) for rn10 in ([11],[12],[13],[14])) as pvt10 GROUP BY recipeID; 表,其中包含一些关系(| recipeID | 1 | 2 | 3 | 4 | 11 | 12 | 13 | 14 | |----------|----|--------|--------|--------|----|--------|--------|--------| | 9 | 21 | (null) | (null) | (null) | 5 | (null) | (null) | (null) | | 12 | 3 | 30 | 34 | 96 | 1 | 1 | 1 | 1 | | 27 | 29 | 43 | (null) | (null) | 1 | 1 | (null) | (null) | | 28 | 29 | 44 | (null) | (null) | 1 | 1 | (null) | (null) | Order),并使用其ID来创建它(StatusUser

我需要使用 Eloquent ORM 获取status_id模型的集合,但我需要按其状态名称进行过滤(在{{1}中仅限表格)和用户名(仅在user_id表中)

当我在查询构建器中使用“join”时。这种关系没有保湿,就像在这种情况下:

Order

如何使用连接进行过滤并为我的模型集合提供水合作用?

1 个答案:

答案 0 :(得分:0)

你可以尝试这样的事情

看看是否有效

$emprestimo = DB::table('emprestimos')
            ->join('status', function ($join) {
                            $join->on('emprestimos.status_id', '=', 'status.id')
                                 ->where('status.nome', 'Solicitado')
                            })
            ->where('emprestimos.dono_id', Auth::user()->id)
            ->get();