ORM – laravel 4: eager to load

I have a table called userwords with a word in it_ ID column, which I want to use to get rows from the words table and join them like a join In this way, everyone will get the information in userwords and words I defined this relationship in userword:

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

}

Then, I try to get all userwords and words as follows:

$words = UserWord::with("word")->
                whereRaw("user_id = ".Auth::user()->id.
                " AND lang1 = '".$lang1.
                "' AND lang2 = '".$lang2."'")
                ->get();

If I don't have with (), it can work So, what did I do wrong? Or will I have to create an original join to get what I want? I've never had a relationship before, so maybe I'm thinking it's a fundamental mistake? Either way, ask me not only to give the answer, if you can! I read the document and I think it should work, but it's not... I ask here

Editor: the error I got is:

Call to a member function addEagerConstraints() on a non-object

Solution

The definition of your relationship is incorrect. You must return the result of the belongsto method call Try the following:

class UserWord extends Eloquent {
    public function word(){
        return $this->belongsTo('Word');
    }
}
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>