lundi 30 mai 2016

Eloquent relationships query building method returns empty

I have a one-to-many relationship, which works perfectly when using the relationship as a property on a query, but when i try to use it as a method it always return empty.

User Model

public function reports()
{
    return $this->hasMany('App\Report', Report::COL_USER_ID, self::COL_ID);
}

Report Model

public function user()
{
    return $this->hasOne('App\User', User::COL_ID, self::COL_USER_ID);
}

This Works

$reports = User::find($id)->reports;

This Doesn't and i need something like this

$reports = User::find($id)->reports()->orderBy(Report::COL_CREATED_AT, self::ORDER_BY_DESC);

According to the eloquent doc, "since all relationships also serve as query builders, you can add further constraints", so that should totally be retrieving the proper data, am I missing something? Thanks in advance for any help



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire