1

Here is my controller function

public function index()
{
    $currentdate = Carbon::yesterday();
    $totalsales = DB::table('receipts')
        ->whereDate('created_at','=', $currentdate)
        ->where('status','=', 'served')
        ->orderBy('created_at','asc')
        ->select(DB::raw('SUM(amount_due) as totalsales'))
        ->get();
        // ->first();
        // return $totalsales;
    return view('dashboard.index',compact('totalsales'));
}

Here is my View

<div class="panel-body">
<h2>{{$totalsales}}</h2>

insted of the value itself the view returns an array like this

[{"totalsales":"130.00"}]
5
  • With get() you will get collection Commented Aug 3, 2017 at 1:33
  • @SagarGautam -> so what sould i do? Commented Aug 3, 2017 at 1:36
  • If you need only record, you can use ->first() on otherwise use ->get(). In above case you can use foreach on $totalsales. Commented Aug 3, 2017 at 1:39
  • @SagarGautam ok i get i now thank your for the answer :D Commented Aug 3, 2017 at 1:57
  • And donot forget to accept if it really help to solve your problem. Commented Aug 3, 2017 at 2:06

2 Answers 2

1

I think you need just sum of the amount_due column so you can use following query.

$totalsales = DB::table('receipts')
              ->whereDate('created_at','=', $currentdate)
              ->where('status','=', 'served')
              ->orderBy('created_at','asc')
              ->sum('amount_due');

Keep other code same, This will work for you.

Hope you understand

Sign up to request clarification or add additional context in comments.

Comments

0

It doesn't know there's only one field.

You need to either specify

<h2>{{ $totalsales->totalsales }}</h2>

or change your query to:

    $totalsales = DB::table('receipts')
    ->whereDate('created_at','=', $currentdate)
    ->where('status','=', 'served')
    ->orderBy('created_at','asc')
    ->select(DB::raw('SUM(amount_due) as totalsales'))
    ->first()
    ->totalsales;

2 Comments

i tried this one but it showed me this errorCall to undefined method stdClass::value()
Check my edit. Instead of ->value('totalsales'), with Query Builder (DB), you need ->totalsales

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.