简体   繁体   English

Laravel 4 Eloquent-在查询生成器中使用/访问ORM关系属性

[英]Laravel 4 Eloquent - Utilize/Access ORM Relationship Properties in Query Builder

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). ...让我说,到目前为止,我希望ORM /雄辩的关系发挥100%的作用(在查询生成器问题之外)。 I've changed around some naming conventions to make the example clearer, but the code remains the same otherwise. 我更改了一些命名约定以使示例更清楚,但否则代码保持不变。

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 使用$ filter_text查询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))...

[...]
->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未知列'people.best_friend.name'在'where子句'(SQL:SELECT COUNT(*)从总people在哪里( active ?=和title ?LIKE或best_friendname 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. 我尝试了使用模型名称,模型的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

If I understand correctly what you want is to filter the main model collection (Person) when a relationship meets certain condition, so: 如果我正确理解了,您想要的是在关系满足特定条件时过滤主模型集合(人),因此:

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满足指定条件(除了它们自己的条件之外)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM