How to access model hasMany Relation with where condition?

eloquent, laravel, laravel-4

Solution

Just in case anyone else encounters the same problems.

Note, that relations are required to be camelcase. So in my case available_videos() should have been availableVideos().

You can easily find out investigating the Laravel source:

// 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);
    }
}

This also explains why my code worked, whenever I loaded the data using the load() method before.

Anyway, my example works perfectly okay now, and $model->availableVideos always returns a Collection.

Problem

I created a model Game using a condition / constraint for a relation as follows: ``` 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); } } ``` When using it somehow like this: ``` $game = Game::with('available_videos')->find(1); $game->available_videos->count(); ``` everything works fine, as roles is the resulting collection. MY PROBLEM: when I try to access it without eager loading ``` $game = Game::find(1); $game->available_videos->count(); ``` an Exception is thrown as it says "Call to a member function count() on a non-object". Using ``` $game = Game::find(1); $game->load('available_videos'); $game->available_videos->count(); ``` works fine, but it seems quite complicated to me, as I do not need to load related models, if I do not use conditions within my relation. Have I missed something? How can I ensure, that available_videos are accessible without using eager loading? For anyone interested, I have also posted this issue on http://forums.laravel.io/viewtopic.php?id=10470

Original source

Related problems