Laravel Database Aggregation +join lookup statements.

Source: Internet
Author: User
Tags rtrim sql injection sql injection attack vars

Aggregate

The Query Builder also provides a variety of aggregation methods, such as,, count max min , avg and sum .

Using Aggregation Methods #
$users=Db::Table(' Users ')->Count();$price=Db::Table(' Orders ')->Max(' Price ');$price=Db::Table(' Orders ')->Min(' Price ');$price=Db::Table ( ' orders ' ->avg  ( ' price ' ;  $total = db::table (" Users ' ->sum ( ' votes '           

But for complex situations (such as the result after a join, the result is not a objet). You cannot use the above method directly

Workaround:

Db::raw (")

Raw expressions#

There are times when you need to use raw expression in a query statement, such expressions become strings inserted into the query, so be careful not to establish any SQL injection attack points. To create raw expression, you can use the DB::raw method:

Using Raw expression#
$users=Db::Table(' Users ')->Select(Db::Raw(' Count (*) as User_count, status ') ->where(' status ', ' <> ', 1) ->  GroupBy(' status ') ->get();       

Actual code:

[PHP]View PlainCopy
  1. /**
  2. * Sales Deb Export.
  3. *
  4. * @param
  5. * @return. csv
  6. *
  7. */
  8. Private function _salesdebexport ()
  9. {
  10. $objects = db::table (' orders ')
  11. ->join (' order_products ',' orders.id ',' = ',' order_products.order_id ')
  12. ->join (' products ',' order_products.product_id ',' = ', 'products.id ')
  13. ->leftjoin (' categories ',' products.category_id ',' = ',' categories.id ')
  14. ->select (
  15. ' orders.external_id ',
  16. ' Orders.address_billing_name ',
  17. ' Categories.customs_code ',
  18. ' Orders.external_sale_date ',
  19. ' orders.invoice_external_id ',
  20. ' Orders.payment_method ',
  21. ' Orders.address_shipping_country ',
  22. Db::raw ('
  23. SUM (order_products.amount_base) as Amount_base,
  24. SUM (Order_products.amount_tax) as Amount_tax,
  25. SUM (order_products.amount_total) as Amount_total
  26. ‘)
  27. )
  28. ->groupby (' orders.external_id ',' Categories.customs_code ')
  29. ->wherein (' orders.object_state_id ', Array (2,3))
  30. ->where ('orders.created_at ', ' like ', Input::get (' year '). '-'. Input::get (' month '). '-% ')
  31. ->get ();
  32. if (empty ($objects))
  33. {
  34. Notification::error ("aucune donnée disponible.");
  35. return redirect::to (URL::p revious ());
  36. }else{
  37. $headers = Array (
  38. ' content-type ' = ' text/csv ',
  39. ' content-disposition ' = ' attachment; filename= ' Sales_deb_export_ '. Input::get (' month ').' _‘. Input::get (' year '). CSV "',
  40. );
  41. $output = RTrim (Implode (', ', Array_keys ((array) $objects [0])).  "\ n";
  42. foreach ($objects as $object)
  43. {
  44. $output. = RTrim (Implode (', ', (array) $object)).  "\ n";
  45. }
  46. return Response::make ($output, $headers);
  47. }

Laravel Database Aggregation +join lookup statements.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.