Laravel 5.2:是否可以在雄辩宾语中使用 where 子句条件


Laravel 5.2: Is it possible to use where clause condition in eloquent object

通过连接另一个表获取初始数据

$results = Model::join('joining other table')
                ->where('initial condition')
                ->limit(100)->get();

现在,我需要按一些附加条件过滤数据。

$new = $results->where('column',value); // Additional Condition

我试过这个,但它返回空集合,即使保留在$results集合中。是否可以在后面的部分使用 where 条件?

您可以使用模型关系,这非常强大。如果定义不同模型之间的关系,则可以调用:

User::with(['posts' => function($builder){
  $builder->where('published', true) // this is on the relationship (posts)
}])->get();

您还可以执行以下操作:

Post::whereHas(['user' => function($builder){
    $builder->where('confirmed', true);
}])->get();

这只会返回确认关联用户的帖子......

你试过吗: Model::join('joining other table') ->where('initial condition') ->where('column',value) ->limit(100)->get();