Laravel 4: Eager loading

laravel, orm

Solution

The definition of your relation is not correct, you have to return the result of the `belongsTo` method call. Try the following:

class UserWord extends Eloquent {
    public function word(){
        return $this->belongsTo('Word');
    }
}

Problem

I have a table called `UserWords` that has a `word_id` column in it, and I want to use that to get the row from the Words table, and kind of concatenate them together like a join. This way each one will have the info from the row in `UserWords` and `Words`. I defined the relation like so in `UserWord`: ``` class UserWord extends Eloquent{ public function word(){ $this->belongsTo('Word'); } } ``` and then after that, I try to get all the UserWords and Words like so: ``` $words = UserWord::with("word")-> whereRaw("user_id = ".Auth::user()->id. " AND lang1 = '".$lang1. "' AND lang2 = '".$lang2."'") ->get(); ``` This works if I don't have the with() in there. So, what am I doing wrong? Or am I going to have to create a raw `JOIN` to get what I want? I've never done relations before, so maybe I'm thinking of this fundamentally wrong? Either way, please teach me more than just giving the answer if you can! I read the docs and I think it should work, but it doesn't so... I'm asking here. EDIT: The error I get is: ``` Call to a member function addEagerConstraints() on a non-object ```

Original source