当我在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.

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;

You should try this:

$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();

dd($result);

You can try this code which is given below.

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

get gives you Collection. You can either use ->first(); function or use foreach on your $result like: @foreach ($result as $res) $res->cd_cash