drra6593 2014-12-24 04: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 05: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)

    展开全部

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

报告相同问题?

手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部