How can I run following query in Laravel?
Select column1,column2,column3 from table;
I don't want to retrieve all columns records as we do by
Select * from table;
Use this :
DB::table('table')
->select(array('column1', 'column2', 'column3'))
->get();
Basicly and like @Uchiha mentioned in comment you can use :
DB::statement('select column1,column2,column3 from table');
But will be better if you use laravel Eloquent ORM function, so after migration you have to create TableModel for your table and use lists function :
TableModel::lists('column1','column2','column3');
Hope this helps.
it's work for me:
DB::table('table')->get(array('column1', 'column2', 'column3'));
First, we call the table method from the DB class and then use the get method to get the columns we want.
For Example :
i want apple and banana from fruit table.
DB::table('fruit ')->get(array('apple', 'banana'));
You can do it in three ways, essentially by passing an array containing the field names to the select() method:
DB::table('table')
->select(array('column1', 'column2', 'column3'))
->get();
or
DB::table('table')
->select(['column1', 'column2', 'column3'])
->get();
or
DB::table('table')
->select('column1', 'column2', 'column3')
->get();
DB::statement('Your query');and I think you got quick answer if you have googled it once