May 3, 2017

Database: Select Query: Laravel simple select query with example




On Laravel, you may not always want to select all columns from a database table.

- Here I try to explain with simple example:

Example:

$users = DB::table(<tablename>)->select('field1', 'field2')->get();

- If you want to used and alias for any field name then just used - as

$users = DB::table(<tablename>)->select('field1', 'field2 as newname')->get();

- Used 'Distinct' with Laravel select query

$users = DB::table(<tablename>)->distinct()->get();

- Sometime if you want to used other expression with SQL query

$users = DB::table(<tablename>)->select(DB::raw('count(*) as cnt, field1
                     ->where('field1', '>', 1)
                     ->groupBy('field1')
                     ->get();

Enjoy!

1 comment:

  1. Nossa adorei! Médico decidiu não adotar aparelho.

    ReplyDelete

Integrating Google reCAPTCHA v3 in HTML Form with PHP

  What is Google reCAPTCHA v3? Google reCAPTCHA is a free service that helps protect websites from spam and abuse. reCAPTCHA v3 is the lates...