Laravel函数属于模型或控制器 [英] Laravel function belong in model or controller

查看:92
本文介绍了Laravel函数属于模型或控制器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个函数,其中包含查询和查询结果/集合的百分数计算(如下).

I have a function which contains a query and a percentile calculation (below) on the query's results/collection.

通过以下方式需要此功能:

This function is needed in the following way:

我有一个运动员模型,有很多测试.每个测试也有许多结果.对于每个结果(显示在表中),我需要显示计算出的百分位(不存储).此时(表格视图),我需要函数来计算每个结果的百分位数.

I have an Athlete model, with many Tests. Each Test also has many Results. For every result (displayed in a table), I need to display a calculated percentile (which is not stored). It's at this point (the table view) where I need the function to calculate the percentile for every result.

我在此处详细介绍了模型.该帖子的答案还显示了所使用的查询.

I have detailed the models here. The answer of that post also shows the query used.

如果我将该功能粘贴在Result模型中,则可以通过这种方式轻松地从视图中访问它{{$result->getThisRank($test->age, $an_athlete->gender, $result->battery_id, $result->score)}}

If I stick that function in the Result model, it allows me easy access to it from the view in this manner {{$result->getThisRank($test->age, $an_athlete->gender, $result->battery_id, $result->score)}}

如果将其放在Controller中,则需要某种动态且复杂的方法来为表中的每个特定Result调用路由.甚至不确定如何执行此操作.

If I put it in a Controller, I would need some dynamic and complex way to call a route for each specific Result in the table. Not even sure how to do this.

问题:在Result模型中进行此查询和计算是否正确?

Question: Is it correct to have this query and calculation in the Result model?

较早的帖子此处,似乎也与模型搭配在一起.

An earlier post here, seems to also go with model.

对查询集合执行的计算:

The calculation performed on the query collection:

                for ($i = 1; $i < 100; $i++ ){
                $rank = 0.0;
                $p = 0.0;
                $rank = $i/100 * ($count + 1);
                $p = $rank;
                //get integer and decimal for interpolation  http://onlinestatbook.com/lms/introduction/percentiles.html                
                $intpart = floor($p);    
                $fraction = $p - $intpart;
                //dd($intpart . ' '.$fraction);
                if($fraction > 0){ //part of percentile formula - see article
                    //test for min array index to not be out of bound. Test negative, most used case.
                    if($intpart != 0){ 
                        $scoreLow = $all_scores[$intpart - 1];
                        if($intpart == $count){ //test for max array index to not go over bound
                            $scoreHigh = $all_scores[$intpart - 1];
                        } else{
                            $scoreHigh = $all_scores[$intpart];    
                        }                            
                    } else{
                        $scoreLow = $all_scores[0];
                        $scoreHigh = $all_scores[$intpart];
                    }                
                    //scoreLow and scoreHigh has been determined, now final step for decimal rank         
                    $scoreHigh = $scoreHigh * 1.0;
                    $scoreLow = $scoreLow * 1.0;
                    $rank = ($fraction * ($scoreHigh - $scoreLow)) + $scoreLow;
                } else{
                    $rank = ($all_scores[$intpart - 1] * 1.0);//no decimal rank, plain rank calculation
                }

                if($sortorder == 'asc'){
                    $rankings[$i.'th %'] = $rank;                            
                }else{
                    $rankings[100 - $i.'th %'] = $rank;
                }

                  //$rankings->add(['rank'.$i => $rank]);
            }                 
            //dd($rankings);
            //reverse rankings
            $rev_rankings = array_reverse($rankings);
            if ($battery == 111){
                dd($rev_rankings);
            }

            $view_rank = null;
            foreach($rev_rankings as $key => $rank){
                if($athlete_score == $rank){
                    $view_rank = $key;
                    break;
                }
                if($athlete_score > $rank){
                    $view_rank = $key;
                    break;
                }
            }
            return($view_rank);
        }
        else{
            return ('Not available');
        }

推荐答案

您描述的计算类型肯定属于域(即-模型).根据您构建应用程序的方式,您可以将功能放在现有的Eloquent模型中,甚至可以作为不扩展ORM的新实体的一部分.另外,我将标准化它提供的返回值的类型,也许将其限制为数值或可能的数值以及NULL.然后,我将在 view (刀片模板)中将所有NULL值等替换为不可用",正如我在函数底部注意到的那样.

The type of calculations you describe certainly belong in the domain (i.e. - model). Depending on how you're structuring your application you may place the function within an existing Eloquent model or even as part of a new entity which doesn't extend the ORM. Additionally, I would standardize the type of return value it provides, perhaps restricting it to numerical values or possibly numerical values as well as NULL. Then I would replace in the view (blade template) any NULL values, etc. with "Not available" as I notice at the bottom of your function.

这篇关于Laravel函数属于模型或控制器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆