小编mdk*_*mdk的帖子

在Eloquent中使用预先加载的查询和过滤计算列

我目前有三个模型,其中有一个使用预先加载的Eloquent查询.我的模型有这些关系:

class Template extends Eloquent {
    public function user() {
        return $this->belongsTo('User');
    }
}

class User extends Eloquent implements UserInterface, RemindableInterface {
    public function profiles() {
        return $this->hasMany('Profile');
    }
    public function templates() {
        return $this->hasMany('Template');
    }
}

class Profile extends Eloquent {
    public function user() {
        return $this->belongsTo('User');
    }
}
Run Code Online (Sandbox Code Playgroud)

我的工作查询如下所示:

$templates = Template::with('user', 'user.profiles')
    ->where('public', '=', true)
    ->whereIn('type', $search_types)
    ->where('user_id', '!=', $user->id)
    ->paginate(8);
Run Code Online (Sandbox Code Playgroud)

这似乎工作得很好,但我还需要补充一点,这对我来说很难做到.我需要更改此查询,以使用表中的现有列latlong列来考虑模板用户与当前用户的距离user.我只希望查询返回用户在当前用户25英里范围内的模板(理想情况是按距离排序,但该部分是可选的).

我试图将自定义计算列添加到用户关系中,如下所示:

$templates = Template::with(array('user' => function($query) use($user) {
        $query->select('*')->selectRaw('(3959 …
Run Code Online (Sandbox Code Playgroud)

laravel eloquent

4
推荐指数
1
解决办法
1486
查看次数

标签 统计

eloquent ×1

laravel ×1