dsj2014 2018-11-22 02:39
浏览 134
已采纳

当我在laravel中编写自定义查询时,别名不起作用

I have the following controller

$result = DB::table('customs_duties')
         ->select(DB::raw('sum(cd_cash) as cd_cash'),DB::raw('sum(cd_creditnote) as cd_creditnote'))
         ->where('fiscalyear', 1)->get();

I get actual result for two field in:

dd($result);

but when i want to get specific field result such as

dd($result->cd_cash);

I have the following error:

Property [cd_cash] does not exist on this collection instance.

  • 写回答

4条回答 默认 最新

  • dongzhenqi2015 2018-11-22 02:43
    关注

    You call ->get(); so you recieve a array.

    $result = DB::table('customs_duties')->select(DB::raw('sum(cd_cash) as cd_cash'),DB::raw('sum(cd_creditnote) as cd_creditnote'))->where('fiscalyear', 1)->get();    
    echo $result[0]->cd_cash;
    

    Will return your value, otherwise you can call ->first().

    $result = DB::table('customs_duties')->select(DB::raw('sum(cd_cash) as cd_cash'),DB::raw('sum(cd_creditnote) as cd_creditnote'))->where('fiscalyear', 1)->first();
    echo $result->cd_cash;
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)
编辑
预览

报告相同问题?

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

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

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

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

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

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

客服 返回
顶部