13

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;
1
  • DB::statement('Your query'); and I think you got quick answer if you have googled it once Commented Sep 29, 2015 at 9:05

8 Answers 8

26

Use this :

DB::table('table')
    ->select(array('column1', 'column2', 'column3'))
    ->get();
Sign up to request clarification or add additional context in comments.

1 Comment

I was using square brackets[] but not working, this way solved my problem.
6

The Eloquent way :

DB::table('table')
    ->select('column1', 'column2', 'column3')
    ->get();

$tableObject
    ->select('column1', 'column2', 'column3')
    ->get();

Comments

3

A simple way in Laravel 5.2 using Eloquent

$rows = Table::where('name', 'like', 'John')->get(['id', 'name', 'family']);

$rows = Table::where($id, 'id')->first(['id', 'name', 'family']);

Comments

1

Try this:

$y = Image::select( array( 'saved_path', 'uploaded_date', 'uploaded_time') )

->where('id', 4)

->get();

Comments

0

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.

1 Comment

ya ! I had tried lists but its not working for me . N e way thanks I have got my problem solved!!!
0

I'm surprised nobody thought of the Model-Controller approach.

On your model add:

protected $appends = ['column3'];

public function getColum3Attribute()
{
   return $this->column1.$this->column2;
} 

So you can use it readily from your Controller:

YourModel::pluck('column3', 'id');

Comments

0

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'));

1 Comment

Code only answers are not considered good answers, and are likely to be downvoted and/or deleted because they are less useful to a community of learners. Answers with an explanation are always more helpful. The solution may be obvious to you. To others it may not. Please explain what it does, and how it's different from existing answers. Otherwise this answer may be flagged as Low Quality. Here are some guidelines for How do I write a good answer?.
0

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();

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.