To begin...
...let me say that my ORM/Eloquent relationships are functioning 100% how I want them to thus far (outside of the query builder issue). I've changed around some naming conventions to make the example clearer, but the code remains the same otherwise.
...让我说我的ORM / Eloquent关系正常运行到目前为止我想要它们(在查询构建器问题之外)。我已经改变了一些命名约定以使示例更清晰,但代码保持不变。
The first example is working fine. The second snippet is where I try to explain what I'm having trouble accomplishing.
第一个例子工作正常。第二个片段是我试图解释我遇到的问题。
This is working fine. Using the $filter_text to query against the Person->title
$people = Person::with('best_friend', 'associates')
->orderBy($order_by, $order_dir)
->where( function($query) use ($active, $filter_text) {
$query->where('active', $active);
if ($filter_text) {
// This is working fine/expected
$query->where('title', 'LIKE', "%$filter_text%");
}
})
->paginate(25);
Looking closer at my where( function($query) ) using the closure...
[...]
->where( function($query) use ($active, $filter_text) {
$query->where('active', $active);
if ($filter_text) {
// This is working fine/expected
$query->where('title', 'LIKE', "%$filter_text%");
/**
* I want to use the same $filter_text to query against the $person->best_friend->name field
* Something like this
*/
$query->orWhere('best_friend.name', 'LIKE', "%$filter_text%");
}
})
[...]
This is the error I'm getting
SQLSTATE[42S22]: Column not found: 1054 Unknown column 'people.best_friend.name' in 'where clause' (SQL: select count(*) as aggregate from people
where (active
= ? and title
LIKE ? or best_friend
.name
LIKE ?)) (Bindings: array ( 0 => '1', 1 => '%g%', 2 => '%g', ))
SQLSTATE [42S22]:找不到列:1054'where子句'中的未知列'people.best_friend.name'(SQL:选择count(*)作为人们聚合的地方(active =?和title LIKE?或best_friend.name LIKE) ?))(绑定:数组(0 =>'1',1 =>'%g%',2 =>'%g',))
Note...
I have tried a few variations of using the Model's names, Model's ORM method names, and table names with variations on plural/singular/capital/etc... They all seem to return a similar error.
我尝试了一些使用Model的名称,Model的ORM方法名称和表名以及复数/单数/大写/等等的变体...它们似乎都返回了类似的错误。
*When the results are output/accessed after the get()/paginate() method, all of the "best_friend" info is there and accessible.* My question boils down to how to access this same data in the query builder.
*当在get()/ paginate()方法之后输出/访问结果时,所有“best_friend”信息都在那里并且可以访问。*我的问题归结为如何在查询构建器中访问这些相同的数据。
Any insight/thoughts are greatly appreciated. I hope the question at least makes sense.
任何见解/想法都非常感谢。我希望这个问题至少是有道理的。
Find me on Twitter: @ErikOnTheWeb
在Twitter上找到我:@ErikOnTheWeb
1 个解决方案
#1
0
If I understand correctly what you want is to filter the main model collection (Person) when a relationship meets certain condition, so:
如果我理解你想要的是在关系满足特定条件时过滤主模型集合(Person),那么:
Model::with($relations)->whereHas('onerelation', function($query)
{
$query->where($onerelation_field, $operator, $value);
})
->where($model_field, $operator, $value)
->get();
This returns a Model
's collection whose relationship Onerelation
meets the specified condition (besides their own ones).
这将返回一个Model的集合,其关系Onerelation满足指定的条件(除了它们自己的条件)。
#1
0
If I understand correctly what you want is to filter the main model collection (Person) when a relationship meets certain condition, so:
如果我理解你想要的是在关系满足特定条件时过滤主模型集合(Person),那么:
Model::with($relations)->whereHas('onerelation', function($query)
{
$query->where($onerelation_field, $operator, $value);
})
->where($model_field, $operator, $value)
->get();
This returns a Model
's collection whose relationship Onerelation
meets the specified condition (besides their own ones).
这将返回一个Model的集合,其关系Onerelation满足指定的条件(除了它们自己的条件)。