我有两个具有一对多关系的模型。
class User extends ConfideUser {
public function shouts()
{
return $this->hasMany('Shout');
}
}
class Shout extends Eloquent {
public function users()
{
return $this->belongsTo('User');
}
}
这似乎工作正常。但是,我如何让它返回嵌套在喊对象中的用户对象?现在它只返回我所有的 Shouts,但我无法在 JSON 中访问所属的用户模型。
Route::get('api/shout', function() {
return Shout::with('users')->get();
});
这只是返回这个 JSON,每次喊都没有用户对象:
[{"id":"1","user_id":"1","message":"A little test shout!","location":"K","created_at":"2013-05-23 19:51:44","updated_at":"2013-05-23 19:51:44"},{"id":"2","user_id":"1","message":"And here is an other shout that is a little bit longer...","location":"S","created_at":"2013-05-23 19:51:44","updated_at":"2013-05-23 19:51:44"}]