Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
How to get distinct values for non-key column fields in Laravel?
In Laravel, you can retrieve distinct values for non-key column fields using several methods. Let's explore different approaches to get unique values from a database table.
Assume we have a students table created with the following structure ?
CREATE TABLE students( id INTEGER NOT NULL PRIMARY KEY, name VARCHAR(15) NOT NULL, email VARCHAR(20) NOT NULL, created_at VARCHAR(27), updated_at VARCHAR(27), address VARCHAR(30) NOT NULL );
Sample data in the table ?
+----+---------------+------------------+-----------------------------+-----------------------------+---------+ | id | name | email | created_at | updated_at | address | +----+---------------+------------------+-----------------------------+-----------------------------+---------+ | 1 | Siya Khan | siya@gmail.com | 2022-05-01T13:45:55.000000Z | 2022-05-01T13:45:55.000000Z | Xyz | | 2 | Rehan Khan | rehan@gmail.com | 2022-05-01T13:49:50.000000Z | 2022-05-01T13:49:50.000000Z | Xyz | | 3 | Rehan Khan | rehan@gmail.com | NULL | NULL | testing | | 4 | Rehan | rehan@gmail.com | NULL | NULL | abcd | +----+---------------+------------------+-----------------------------+-----------------------------+---------+
Using Eloquent distinct() Method
The distinct() method returns unique values from a specified column using Eloquent ORM ?
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Models\Student;
class StudentController extends Controller {
public function index() {
$student = Student::select('name')->distinct()->get()->toArray();
print_r($student);
}
}
?>
The output of the above code is ?
Array(
[0] => Array(
[name] => Siya Khan
)
[1] => Array(
[name] => Rehan Khan
)
[2] => Array(
[name] => Rehan
)
)
Using groupBy() Method
The groupBy() method groups records by a specific field, effectively providing distinct values ?
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Models\Student;
class StudentController extends Controller {
public function index() {
$student = Student::select('name')->groupBy('name')->get()->toArray();
print_r($student);
}
}
?>
The output of the above code is ?
Array (
[0] => Array (
[name] => Rehan
)
[1] => Array (
[name] => Rehan Khan
)
[2] => Array (
[name] => Siya Khan
)
)
Using unique() Method on Collections
The unique() method filters unique values from a Laravel collection after retrieving all records ?
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Models\Student;
class StudentController extends Controller {
public function index() {
$student = Student::all()->unique('name')->toArray();
print_r($student);
}
}
?>
The output of the above code shows complete records for unique names ?
Array(
[0] => Array(
[id] => 1
[name] => Siya Khan
[email] => siya@gmail.com
[created_at] => 2022-05-01T13:45:55.000000Z
[updated_at] => 2022-05-01T13:45:55.000000Z
[address] => Xyz
)
[1] => Array(
[id] => 2
[name] => Rehan Khan
[email] => rehan@gmail.com
[created_at] => 2022-05-01T13:49:50.000000Z
[updated_at] => 2022-05-01T13:49:50.000000Z
[address] => Xyz
)
[3] => Array(
[id] => 4
[name] => Rehan
[email] => rehan@gmail.com
[created_at] =>
[updated_at] =>
[address] => abcd
)
)
Using DB Facade with distinct()
You can also use Laravel's DB facade to get distinct values ?
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use DB;
class StudentController extends Controller{
public function index() {
$stdDetails = DB::table('students')->distinct()->get(['name'])->toArray();
print_r($stdDetails);
}
}
?>
The output of the above code is ?
Array (
[0] => stdClass Object (
[name] => Siya Khan
)
[1] => stdClass Object(
[name] => Rehan Khan
)
[2] => stdClass Object (
[name] => Rehan
)
)
Using pluck() with groupBy()
For a simpler array output, combine groupBy() with pluck() ?
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use DB;
class StudentController extends Controller{
public function index() {
$stdDetails = DB::table('students')->select('name')->groupBy('name')->get()->pluck('name')->all();
print_r($stdDetails);
}
}
?>
The output of the above code is ?
Array( [0] => Rehan [1] => Rehan Khan [2] => Siya Khan )
Conclusion
Use distinct() or groupBy() for database-level filtering, and unique() for collection-level filtering. The pluck() method provides clean array output when you only need the values.
