drra6593 2014-12-24 12:19
浏览 156
已采纳

如何在Laravel查询生成器中注入自定义列

I got a query with many joins, wheres, ect. And what I need to do is insert some maths into each result set as it will be feeding either a csv export or be displayed on page. Can later on even be sent back as API, so what I really want to do is prepare the data once, and then use it where ever.

$result = DB::table('a')
->join('b')
->where('c')
->orderBy('d')
->select('e');

if ($paginate) {
    $query->paginate();
} else {
    $query->get();
}

So the question is, can I somehow iterate through my results and do some maths as I get them? Like maybe a callback on each result?

For example get difference between some values retrieved in each row, or add in additional row signifying pass/fail. Basically I was wondering if there was a better way of doing things then later on doing a foreach() on the results to go through them, do the maths and add in additional columns, thereby destroying the pagination support and having to convert the result into an ugly array?

  • 写回答

1条回答 默认 最新

  • doudu2515 2014-12-24 13:15
    关注

    I can think of three methods. For example, let's say you want to get the difference between the columns c and e.

    Select with raw expressions

    With raw expressions you can use all available SQL functions and ordinary math operators as well. Basically you can do everything what you could in a normal SQL select because Laravel will insert the string directly into the query.

    $result = DB::table('a')
        ->join('b')
        ->where('c')
        ->orderBy('d')
        ->select('e', DB::raw('c - e AS differenceCE'));
    

    Now the result will have a differenceCE property containing the result of the division.

    Attribute accessors

    This only works with Eloquent Models!

    You can create a new dynamic attribute in your model, that will be calculated the moment you access it

    class MyModel extends Eloquent {
        protected $appends = array('difference');
    
        public function getDifferenceAttribute(){
            return $this->attributes['c'] - $this->attributes['e'];
        }
    }
    

    Access the property:

    $mymodel->difference;
    

    for(each)

    You can also use a simple loop like:

    foreach($result as $model){
        // do math
    }
    

    Or if you're using Eloquent there's the each method you can call on a collection

    $result->each(function($model){
        // do math
    });
    

    Just be aware that method 1 and 2 may result in (slightly) better performance. SQL just because it has to go through every record anyways and the method with the attribute accessor has the advantage that it will be lazy loaded. Meaning the calculation only happens when you use it (when you access the property)

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog