mardi 17 mai 2016

Laravel 5 Multi Select not working

I need help with setting this one query in laravel 5.

This is my table example... :

table name: mytable

 user_id  cost   status     created_at 
--------------------------------------
   1      10     Pending   2016-04-21 04:51:01
   1      20     Pending   2016-04-22 04:51:01
   1      100    Approved  2016-04-25 04:51:01 
   1      45     Pending   2016-04-28 04:51:01
   1      60     Approved  2016-05-01 04:51:01
   1      60     Approved  2016-05-01 04:51:01
   1      160    Approved  2016-05-03 04:51:01
   1      360     Pending   2016-05-05 04:51:01
   1      260     Approved  2016-05-12 04:51:01
   1      210     Pending   2016-05-21 04:51:01

basically I need One SQL statement in Laravel that will output something like this or close per user , because I might have different user ids

           Totalpending    Totalapproved  TotalCost

April/2016    3               1            ( add cost for April)
May/2016      1               4             ( add all cost for May)  

I have this code so far , but how to make it all in one query ?

$query = DB::table('my_table')->select(DB::raw('MONTH(created_at) as m, YEAR(created_at) as y'), DB::raw('count(*) as pp'))->where('user_id' ,Auth::id())->where('status','Pending')
  ->groupBy('created_at')
  ->get();  

var_dump($query);

But this outputs only pending like below: ...

 array(1) {
  [0]=>
   object(stdClass)#321 (3) {
     ["m"]=>
      string(1) "4"
      ["y"]=>
      string(4) "2016"
      ["pp"]=>
      string(1) "3"
      }
   }

or I can have an output like this : where "pp" > total pending , "aa" > total approved , "cc" > total cost

 array(1) {
  [0]=>
   object(stdClass)#321 (3) {
     ["m"]=>
      string(1) "4"
      ["y"]=>
      string(4) "2016"
      ["pp"]=>
      string(1) "3"
      ["aa"]=>
      string(1) "1"
      ["cc"]=>
      string(1) "175"
      }
   }

How to show the others approved and total cost??



from Newest questions tagged laravel-5 - Stack Overflow http://ift.tt/1Tml1Ah
via IFTTT

Aucun commentaire:

Enregistrer un commentaire