Dev.W
Dev.W

Reputation: 2370

Laravel Array Output

I am perfoming a query in my controller and outputting the total cost on my view.

Heres my query:

 $stat = array(
            'TotalCost' => DB::table('ORD_DETAIL')
                        ->select(DB::raw('SUM(OD_QTYORD * OD_QTYUNIT * OD_COSTPRICE) as TotalCost'))
                        ->where('OD_ORDER_NUMBER',$id)->first()
            );

Heres my output:

{{ $stat['TotalCost'] }}

But I am getting the error:

htmlentities() expects parameter 1 to be string, object given (View: F:\view.blade.php)

Upvotes: 1

Views: 35

Answers (2)

oseintow
oseintow

Reputation: 7371

Change it to

{{ $stat['TotalCost']->TotalCost }}

To maintain

{{ $stat['TotalCost'] }}

change your query to

 $stat = array(
        'TotalCost' => DB::table('ORD_DETAIL')
                    ->select(DB::raw('SUM(OD_QTYORD * OD_QTYUNIT * OD_COSTPRICE) as TotalCost'))
                    ->where('OD_ORDER_NUMBER',$id)->lists('TotalCost')->first();
        );

Upvotes: 0

Rwd
Rwd

Reputation: 35180

The reason you're getting the above error is because you're query is returning an Object.

To get around this you can either do:

{{ $stat['TotalCost']->TotalCost }}

Or you could change your query to use Laravel's built-in sum() method:

DB::table('ORD_DETAIL')
    ->where('OD_ORDER_NUMBER',$id)
    ->sum(DB::raw('OD_QTYORD * OD_QTYUNIT * OD_COSTPRICE'));

Hope this helps!

Upvotes: 2

Related Questions