首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >如何访问带where条件的模型hasMany关系?

如何访问带where条件的模型hasMany关系?
EN

Stack Overflow用户
提问于 2013-08-30 04:15:16
回答 8查看 167K关注 0票数 70

我使用关系的条件/约束创建了一个模型游戏,如下所示:

代码语言:javascript
复制
class Game extends Eloquent {
    // many more stuff here

    // relation without any constraints ...works fine 
    public function videos() {
        return $this->hasMany('Video');
    }

    // results in a "problem", se examples below
    public function available_videos() {
        return $this->hasMany('Video')->where('available','=', 1);
    }
}

以某种方式使用时,如下所示:

代码语言:javascript
复制
$game = Game::with('available_videos')->find(1);
$game->available_videos->count();

一切都很好,因为roles是结果集合。

MY PROBLEM:

当我尝试在不加载的情况下访问它时

代码语言:javascript
复制
$game = Game::find(1);
$game->available_videos->count();

一个异常被抛出,因为它说“调用非对象上的成员函数count()”。

使用

代码语言:javascript
复制
$game = Game::find(1);
$game->load('available_videos');
$game->available_videos->count();

运行良好,但对我来说似乎相当复杂,因为如果我不在关系中使用条件,我就不需要加载相关的模型。

我错过了什么吗?我如何确保在不使用立即加载的情况下可以访问available_videos?

对于任何感兴趣的人,我也在http://forums.laravel.io/viewtopic.php?id=10470上发布了这一期

EN

回答 8

Stack Overflow用户

回答已采纳

发布于 2014-04-23 03:30:12

以防其他人遇到同样的问题。

请注意,关系必须是camelcase。所以在我的例子中,available_videos()应该是availableVideos()。

你可以很容易地找到研究Laravel的来源:

代码语言:javascript
复制
// Illuminate\Database\Eloquent\Model.php
...
/**
 * Get an attribute from the model.
 *
 * @param  string  $key
 * @return mixed
 */
public function getAttribute($key)
{
    $inAttributes = array_key_exists($key, $this->attributes);

    // If the key references an attribute, we can just go ahead and return the
    // plain attribute value from the model. This allows every attribute to
    // be dynamically accessed through the _get method without accessors.
    if ($inAttributes || $this->hasGetMutator($key))
    {
        return $this->getAttributeValue($key);
    }

    // If the key already exists in the relationships array, it just means the
    // relationship has already been loaded, so we'll just return it out of
    // here because there is no need to query within the relations twice.
    if (array_key_exists($key, $this->relations))
    {
        return $this->relations[$key];
    }

    // If the "attribute" exists as a method on the model, we will just assume
    // it is a relationship and will load and return results from the query
    // and hydrate the relationship's value on the "relationships" array.
    $camelKey = camel_case($key);

    if (method_exists($this, $camelKey))
    {
        return $this->getRelationshipFromMethod($key, $camelKey);
    }
}

这也解释了每当我以前使用load()方法加载数据时,我的代码为什么会工作。

无论如何,我的示例现在工作得很好,$model->availableVideos总是返回一个Collection。

票数 16
EN

Stack Overflow用户

发布于 2013-09-04 04:13:26

我认为这才是正确的方式:

代码语言:javascript
复制
class Game extends Eloquent {
    // many more stuff here

    // relation without any constraints ...works fine 
    public function videos() {
        return $this->hasMany('Video');
    }

    // results in a "problem", se examples below
    public function available_videos() {
        return $this->videos()->where('available','=', 1);
    }
}

然后你就得

代码语言:javascript
复制
$game = Game::find(1);
var_dump( $game->available_videos()->get() );
票数 97
EN

Stack Overflow用户

发布于 2014-07-16 05:42:54

我想这就是你要找的(Laravel 4,参见http://laravel.com/docs/eloquent#querying-relations)

代码语言:javascript
复制
$games = Game::whereHas('video', function($q)
{
    $q->where('available','=', 1);

})->get();
票数 35
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/18520209

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档