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!
Nossa adorei! Médico decidiu não adotar aparelho.
ReplyDelete