5


I got database table like this:

**job_id**
    5
    5
    5
    6
    6
    7
    8
    8

I want to write query, which could select only unique id's. By saying unique I mean select only these values once:
5, 6, 7, 8

Thanks in advance!

bagrat
  • 7,158
  • 6
  • 29
  • 47
Evaldas Butkus
  • 635
  • 4
  • 12
  • 32
  • What code have you written to solve this problem? Please post your code and a (specific) problem. – Damien Jun 08 '15 at 12:43
  • At the moment my query picks all the job_id's. So when I do echo, my values doublicate. I want to echo these job_id's only once. – Evaldas Butkus Jun 08 '15 at 12:49

2 Answers2

13

You could use DISTINCT.

DB::table('table')->select('job_id')->distinct()->get();
aebersold
  • 11,286
  • 2
  • 20
  • 29
11

How about:

$jobs = DB::table('my_job_table')
    ->groupBy('job_id')
    ->get();

Eloquent:

  1. First, you need a model. You could generate this by using php artisan. php artisan make:model jobs (I assume you have done this already) This will create a model in /your_project/app/Job.php
  2. Now you can use Eloquent (here in a route, to see some output):

    Route::get('/jobs', function () { $jobs = \App\Job::groupBy('job_id')->get(); return $jobs->lists('job_id'); });

will return something like: [0,1,3,4] instead of [0, 1, 1, 1, 3, 4, 4, 4].

wiesson
  • 6,544
  • 5
  • 40
  • 68