我正在尝试通过Eloquent获取来自特定国家/地区的所有用户。
问题是我得到了所有记录,where子句不起作用。
$res = User::with(array('country' => function($query) {
$query->where('country', '=', 'salope');
}))->get();
遵循laravel文档中的这种模式
$users = User::with(array('posts' => function($query)
{
$query->where('title', 'like', '%first%');
}))->get();
我的模特:
class User extends SentryUserModel {
public function country() {
return $this->belongsTo('country','country_id');
}
}
class Country extends Eloquent {
public function users() {
return $this->hasMany('users');
}
}
我究竟做错了什么 ?
我找到了我想要的laravel 4.1版本。我的问题表达不正确。我想查询一个关系。
$posts = Post::whereHas('comments', function($q)
{
$q->where('content', 'like', 'foo%');
})->get();
因此,以我为例:
$res = User::whereHas('country', function($q) {
$q->where('country', 'salope');
})->get();
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句