Laravel 5 Eloquent - 将计算值作为列添加到集合

Posted

技术标签:

【中文标题】Laravel 5 Eloquent - 将计算值作为列添加到集合【英文标题】:Laravel 5 Eloquent - adding calculated value as column to collection 【发布时间】:2018-03-29 13:33:01 【问题描述】:

我正在计算老师到学校的距离。

我正在查询用户表,然后使用 whereHas() 查询 userable_type 教师。教师表有经度和纬度值。

当我直接查询教师模型但首先通过用户模型查询时,以下对我有用!

        $query = User::where('userable_type', 'App\Teacher');

        $query->with('userable');

        if($school->latitude == '' || $school->longitude == '') 

          \Session::flash('warning', 'Please check your school profile postcode; it appears that the system was unable to geocode your postcode! Results shown do not reflect any distances of staff from your school!');

         else 

            $query->whereHas('teacher', function ($query) use ($school) 

                $haversine = "(3961 * acos(cos(radians($school->latitude)) 
                    * cos(radians(latitude)) 
                    * cos(radians(longitude) 
                    - radians($school->longitude)) 
                    + sin(radians($school->latitude)) 
                    * sin(radians(latitude))))";

                $query->select() //pick the columns you want here.
                    ->selectRaw("$haversine AS distance")
                    ->whereRaw("$haversine < ?", '20');
                    // ->havingRaw('$haversine', '<', '20');

            );

        

        $query->whereDoesntHave('thisSchool');

        $teachers = $query->get();

“距离”字段未创建,但未引发错误!所以我的问题是,在这种情况下,我如何为每个用户添加“距离”值,因为我想按距离排序?

这个是直接查询Teacher模型的地方,有效!

     $query = Teacher::with('user', 'user.schools', 'criterias', 'criterias.stafftype', 'criterias.teachingstage', 'criterias.teachingsubject')
            ->where('public', 1)
            ->where('verified', 1);

            if($school->latitude == '' || $school->longitude == '') 

              \Session::flash('warning', 'Please check your school profile postcode; it appears that the system was unable to geocode your postcode! Results shown do not reflect any distances of staff from your school!');

             else 

              if( $filters['distance'] != '' ) 

                  $haversine = "(3961 * acos(cos(radians($school->latitude)) 
                       * cos(radians(latitude)) 
                       * cos(radians(longitude) 
                       - radians($school->longitude)) 
                       + sin(radians($school->latitude)) 
                       * sin(radians(latitude))))";

                  $query->select() //pick the columns you want here.
                      ->selectRaw("$haversine AS distance")
                      ->whereRaw("$haversine < ?", [$filters['distance']]);

              

            

            $query->orderBy( 'active', 'DESC' );
            $query->orderBy( 'distance', 'ASC' );

            $teachers = $query->paginate( $filters['perpage'] );

谢谢, 克...

【问题讨论】:

你能发布工作查询(“直接教师模型”)吗? @JonasStaudenmeir 感谢您的关注,我已添加到我的原始帖子中... 【参考方案1】:

好的,我找到了解决这个问题的方法,可以在这里找到:

Laravel 5 eloquent - add child field to parent model

【讨论】:

以上是关于Laravel 5 Eloquent - 将计算值作为列添加到集合的主要内容,如果未能解决你的问题,请参考以下文章

Laravel Eloquent 播种机计算每个外键的行数

LARAVEL 5.5 - 使用 Eloquent 模型将数据从 FORM 插入数据库

将 Laravel 5 Eloquent 模型移动到它自己的目录中

Laravel 5 变量构造查询字符串 Eloquent

将 Sql 查询转换为 Eloquent Laravel 5.6

php Laravel 5 Eloquent CheatSheet #laravel #eloquent